1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "util/i18n/locale.h" 18 19 #include "util/strings/split.h" 20 21 namespace libtextclassifier2 { 22 23 namespace { 24 25 bool CheckLanguage(StringPiece language) { 26 if (language.size() != 2 && language.size() != 3) { 27 return false; 28 } 29 30 // Needs to be all lowercase. 31 for (int i = 0; i < language.size(); ++i) { 32 if (!std::islower(language[i])) { 33 return false; 34 } 35 } 36 37 return true; 38 } 39 40 bool CheckScript(StringPiece script) { 41 if (script.size() != 4) { 42 return false; 43 } 44 45 if (!std::isupper(script[0])) { 46 return false; 47 } 48 49 // Needs to be all lowercase. 50 for (int i = 1; i < script.size(); ++i) { 51 if (!std::islower(script[i])) { 52 return false; 53 } 54 } 55 56 return true; 57 } 58 59 bool CheckRegion(StringPiece region) { 60 if (region.size() == 2) { 61 return std::isupper(region[0]) && std::isupper(region[1]); 62 } else if (region.size() == 3) { 63 return std::isdigit(region[0]) && std::isdigit(region[1]) && 64 std::isdigit(region[2]); 65 } else { 66 return false; 67 } 68 } 69 70 } // namespace 71 72 Locale Locale::FromBCP47(const std::string& locale_tag) { 73 std::vector<StringPiece> parts = strings::Split(locale_tag, '-'); 74 if (parts.empty()) { 75 return Locale::Invalid(); 76 } 77 78 auto parts_it = parts.begin(); 79 StringPiece language = *parts_it; 80 if (!CheckLanguage(language)) { 81 return Locale::Invalid(); 82 } 83 ++parts_it; 84 85 StringPiece script; 86 if (parts_it != parts.end()) { 87 script = *parts_it; 88 if (!CheckScript(script)) { 89 script = ""; 90 } else { 91 ++parts_it; 92 } 93 } 94 95 StringPiece region; 96 if (parts_it != parts.end()) { 97 region = *parts_it; 98 if (!CheckRegion(region)) { 99 region = ""; 100 } else { 101 ++parts_it; 102 } 103 } 104 105 // NOTE: We don't parse the rest of the BCP47 tag here even if specified. 106 107 return Locale(language.ToString(), script.ToString(), region.ToString()); 108 } 109 110 } // namespace libtextclassifier2 111