Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Mårten Kongstad | e0930d3 | 2018-10-18 14:50:15 +0200 | [diff] [blame] | 17 | #include "android-base/macros.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 18 | #include "androidfw/Locale.h" |
| 19 | #include "androidfw/Util.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include <ctype.h> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 27 | using ::android::ResTable_config; |
| 28 | using ::android::StringPiece; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 30 | namespace android { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | void LocaleValue::set_language(const char* language_chars) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | size_t i = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | while ((*language_chars) != '\0') { |
| 35 | language[i++] = ::tolower(*language_chars); |
| 36 | language_chars++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | void LocaleValue::set_region(const char* region_chars) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | size_t i = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | while ((*region_chars) != '\0') { |
| 43 | region[i++] = ::toupper(*region_chars); |
| 44 | region_chars++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | void LocaleValue::set_script(const char* script_chars) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | size_t i = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | while ((*script_chars) != '\0') { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | if (i == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | script[i++] = ::toupper(*script_chars); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | script[i++] = ::tolower(*script_chars); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | script_chars++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | void LocaleValue::set_variant(const char* variant_chars) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | size_t i = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | while ((*variant_chars) != '\0') { |
| 63 | variant[i++] = *variant_chars; |
| 64 | variant_chars++; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | static inline bool is_alpha(const std::string& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | return std::all_of(std::begin(str), std::end(str), ::isalpha); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | static inline bool is_number(const std::string& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | return std::all_of(std::begin(str), std::end(str), ::isdigit); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 76 | bool LocaleValue::InitFromFilterString(const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | // A locale (as specified in the filter) is an underscore separated name such |
| 78 | // as "en_US", "en_Latn_US", or "en_US_POSIX". |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | std::vector<std::string> parts = util::SplitAndLowercase(str, '_'); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | const int num_tags = parts.size(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | bool valid = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | if (num_tags >= 1) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | const std::string& lang = parts[0]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | if (is_alpha(lang) && (lang.length() == 2 || lang.length() == 3)) { |
| 86 | set_language(lang.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | valid = true; |
| 88 | } |
| 89 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | if (!valid || num_tags == 1) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | return valid; |
| 93 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | // At this point, valid == true && numTags > 1. |
| 96 | const std::string& part2 = parts[1]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | if ((part2.length() == 2 && is_alpha(part2)) || |
| 98 | (part2.length() == 3 && is_number(part2))) { |
| 99 | set_region(part2.c_str()); |
| 100 | } else if (part2.length() == 4 && is_alpha(part2)) { |
| 101 | set_script(part2.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | } else if (part2.length() >= 4 && part2.length() <= 8) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | set_variant(part2.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | } else { |
| 105 | valid = false; |
| 106 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 107 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | if (!valid || num_tags == 2) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | return valid; |
| 110 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | // At this point, valid == true && numTags > 1. |
| 113 | const std::string& part3 = parts[2]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | if (((part3.length() == 2 && is_alpha(part3)) || |
| 115 | (part3.length() == 3 && is_number(part3))) && |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | script[0]) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | set_region(part3.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | } else if (part3.length() >= 4 && part3.length() <= 8) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | set_variant(part3.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | } else { |
| 121 | valid = false; |
| 122 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | if (!valid || num_tags == 3) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | return valid; |
| 126 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 127 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | const std::string& part4 = parts[3]; |
| 129 | if (part4.length() >= 4 && part4.length() <= 8) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | set_variant(part4.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | } else { |
| 132 | valid = false; |
| 133 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | if (!valid || num_tags > 4) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 142 | bool LocaleValue::InitFromBcp47Tag(const StringPiece& bcp47tag) { |
| 143 | return InitFromBcp47TagImpl(bcp47tag, '-'); |
| 144 | } |
| 145 | |
| 146 | bool LocaleValue::InitFromBcp47TagImpl(const StringPiece& bcp47tag, const char separator) { |
| 147 | std::vector<std::string> subtags = util::SplitAndLowercase(bcp47tag, separator); |
| 148 | if (subtags.size() == 1) { |
| 149 | set_language(subtags[0].c_str()); |
| 150 | } else if (subtags.size() == 2) { |
| 151 | set_language(subtags[0].c_str()); |
| 152 | |
| 153 | // The second tag can either be a region, a variant or a script. |
| 154 | switch (subtags[1].size()) { |
| 155 | case 2: |
| 156 | case 3: |
| 157 | set_region(subtags[1].c_str()); |
| 158 | break; |
| 159 | case 4: |
| 160 | if ('0' <= subtags[1][0] && subtags[1][0] <= '9') { |
| 161 | // This is a variant: fall through |
| 162 | } else { |
| 163 | set_script(subtags[1].c_str()); |
| 164 | break; |
| 165 | } |
Mårten Kongstad | e0930d3 | 2018-10-18 14:50:15 +0200 | [diff] [blame] | 166 | FALLTHROUGH_INTENDED; |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 167 | case 5: |
| 168 | case 6: |
| 169 | case 7: |
| 170 | case 8: |
| 171 | set_variant(subtags[1].c_str()); |
| 172 | break; |
| 173 | default: |
| 174 | return false; |
| 175 | } |
| 176 | } else if (subtags.size() == 3) { |
| 177 | // The language is always the first subtag. |
| 178 | set_language(subtags[0].c_str()); |
| 179 | |
| 180 | // The second subtag can either be a script or a region code. |
| 181 | // If its size is 4, it's a script code, else it's a region code. |
| 182 | if (subtags[1].size() == 4) { |
| 183 | set_script(subtags[1].c_str()); |
| 184 | } else if (subtags[1].size() == 2 || subtags[1].size() == 3) { |
| 185 | set_region(subtags[1].c_str()); |
| 186 | } else { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | // The third tag can either be a region code (if the second tag was |
| 191 | // a script), else a variant code. |
| 192 | if (subtags[2].size() >= 4) { |
| 193 | set_variant(subtags[2].c_str()); |
| 194 | } else { |
| 195 | set_region(subtags[2].c_str()); |
| 196 | } |
| 197 | } else if (subtags.size() == 4) { |
| 198 | set_language(subtags[0].c_str()); |
| 199 | set_script(subtags[1].c_str()); |
| 200 | set_region(subtags[2].c_str()); |
| 201 | set_variant(subtags[3].c_str()); |
| 202 | } else { |
| 203 | return false; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 208 | ssize_t LocaleValue::InitFromParts(std::vector<std::string>::iterator iter, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | std::vector<std::string>::iterator end) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | const std::vector<std::string>::iterator start_iter = iter; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 211 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | std::string& part = *iter; |
| 213 | if (part[0] == 'b' && part[1] == '+') { |
| 214 | // This is a "modified" BCP 47 language tag. Same semantics as BCP 47 tags, |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 215 | // except that the separator is "+" and not "-". Skip the prefix 'b+'. |
| 216 | if (!InitFromBcp47TagImpl(StringPiece(part).substr(2), '+')) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | return -1; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 218 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | ++iter; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | } else { |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame] | 221 | if ((part.length() == 2 || part.length() == 3) && is_alpha(part) && part != "car") { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | set_language(part.c_str()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | ++iter; |
| 224 | |
| 225 | if (iter != end) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | const std::string& region_part = *iter; |
| 227 | if (region_part.c_str()[0] == 'r' && region_part.length() == 3) { |
| 228 | set_region(region_part.c_str() + 1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | ++iter; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | return static_cast<ssize_t>(iter - start_iter); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | void LocaleValue::InitFromResTable(const ResTable_config& config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | config.unpackLanguage(language); |
| 239 | config.unpackRegion(region); |
| 240 | if (config.localeScript[0] && !config.localeScriptWasComputed) { |
| 241 | memcpy(script, config.localeScript, sizeof(config.localeScript)); |
| 242 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 243 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | if (config.localeVariant[0]) { |
| 245 | memcpy(variant, config.localeVariant, sizeof(config.localeVariant)); |
| 246 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | void LocaleValue::WriteTo(ResTable_config* out) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | out->packLanguage(language); |
| 251 | out->packRegion(region); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 252 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | if (script[0]) { |
| 254 | memcpy(out->localeScript, script, sizeof(out->localeScript)); |
| 255 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 256 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 257 | if (variant[0]) { |
| 258 | memcpy(out->localeVariant, variant, sizeof(out->localeVariant)); |
| 259 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 262 | } // namespace android |