| 1 | // © 2016 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | /* |
| 4 | ********************************************************************** |
| 5 | * Copyright (C) 1997-2016, International Business Machines |
| 6 | * Corporation and others. All Rights Reserved. |
| 7 | ********************************************************************** |
| 8 | * |
| 9 | * File ULOC.CPP |
| 10 | * |
| 11 | * Modification History: |
| 12 | * |
| 13 | * Date Name Description |
| 14 | * 04/01/97 aliu Creation. |
| 15 | * 08/21/98 stephen JDK 1.2 sync |
| 16 | * 12/08/98 rtg New Locale implementation and C API |
| 17 | * 03/15/99 damiba overhaul. |
| 18 | * 04/06/99 stephen changed setDefault() to realloc and copy |
| 19 | * 06/14/99 stephen Changed calls to ures_open for new params |
| 20 | * 07/21/99 stephen Modified setDefault() to propagate to C++ |
| 21 | * 05/14/04 alan 7 years later: refactored, cleaned up, fixed bugs, |
| 22 | * brought canonicalization code into line with spec |
| 23 | *****************************************************************************/ |
| 24 | |
| 25 | /* |
| 26 | POSIX's locale format, from putil.c: [no spaces] |
| 27 | |
| 28 | ll [ _CC ] [ . MM ] [ @ VV] |
| 29 | |
| 30 | l = lang, C = ctry, M = charmap, V = variant |
| 31 | */ |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <optional> |
| 35 | #include <string_view> |
| 36 | |
| 37 | #include "unicode/bytestream.h" |
| 38 | #include "unicode/errorcode.h" |
| 39 | #include "unicode/stringpiece.h" |
| 40 | #include "unicode/utypes.h" |
| 41 | #include "unicode/ustring.h" |
| 42 | #include "unicode/uloc.h" |
| 43 | |
| 44 | #include "bytesinkutil.h" |
| 45 | #include "putilimp.h" |
| 46 | #include "ustr_imp.h" |
| 47 | #include "ulocimp.h" |
| 48 | #include "umutex.h" |
| 49 | #include "cstring.h" |
| 50 | #include "cmemory.h" |
| 51 | #include "locmap.h" |
| 52 | #include "uarrsort.h" |
| 53 | #include "uenumimp.h" |
| 54 | #include "uassert.h" |
| 55 | #include "charstr.h" |
| 56 | |
| 57 | U_NAMESPACE_USE |
| 58 | |
| 59 | /* ### Declarations **************************************************/ |
| 60 | |
| 61 | /* Locale stuff from locid.cpp */ |
| 62 | U_CFUNC void locale_set_default(const char *id); |
| 63 | U_CFUNC const char *locale_get_default(); |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | /* ### Data tables **************************************************/ |
| 68 | |
| 69 | /** |
| 70 | * Table of language codes, both 2- and 3-letter, with preference |
| 71 | * given to 2-letter codes where possible. Includes 3-letter codes |
| 72 | * that lack a 2-letter equivalent. |
| 73 | * |
| 74 | * This list must be in sorted order. This list is returned directly |
| 75 | * to the user by some API. |
| 76 | * |
| 77 | * This list must be kept in sync with LANGUAGES_3, with corresponding |
| 78 | * entries matched. |
| 79 | * |
| 80 | * This table should be terminated with a nullptr entry, followed by a |
| 81 | * second list, and another nullptr entry. The first list is visible to |
| 82 | * user code when this array is returned by API. The second list |
| 83 | * contains codes we support, but do not expose through user API. |
| 84 | * |
| 85 | * Notes |
| 86 | * |
| 87 | * Tables updated per http://lcweb.loc.gov/standards/iso639-2/ to |
| 88 | * include the revisions up to 2001/7/27 *CWB* |
| 89 | * |
| 90 | * The 3 character codes are the terminology codes like RFC 3066. This |
| 91 | * is compatible with prior ICU codes |
| 92 | * |
| 93 | * "in" "iw" "ji" "jw" & "sh" have been withdrawn but are still in the |
| 94 | * table but now at the end of the table because 3 character codes are |
| 95 | * duplicates. This avoids bad searches going from 3 to 2 character |
| 96 | * codes. |
| 97 | * |
| 98 | * The range qaa-qtz is reserved for local use |
| 99 | */ |
| 100 | /* Generated using org.unicode.cldr.icu.GenerateISO639LanguageTables */ |
| 101 | /* ISO639 table version is 20150505 */ |
| 102 | /* Subsequent hand addition of selected languages */ |
| 103 | constexpr const char* LANGUAGES[] = { |
| 104 | "aa", "ab", "ace", "ach", "ada", "ady", "ae", "aeb", |
| 105 | "af", "afh", "agq", "ain", "ak", "akk", "akz", "ale", |
| 106 | "aln", "alt", "am", "an", "ang", "anp", "ar", "arc", |
| 107 | "arn", "aro", "arp", "arq", "ars", "arw", "ary", "arz", "as", |
| 108 | "asa", "ase", "ast", "av", "avk", "awa", "ay", "az", |
| 109 | "ba", "bal", "ban", "bar", "bas", "bax", "bbc", "bbj", |
| 110 | "be", "bej", "bem", "bew", "bez", "bfd", "bfq", "bg", |
| 111 | "bgc", "bgn", "bho", "bi", "bik", "bin", "bjn", "bkm", "bla", |
| 112 | "blo", "bm", "bn", "bo", "bpy", "bqi", "br", "bra", "brh", |
| 113 | "brx", "bs", "bss", "bua", "bug", "bum", "byn", "byv", |
| 114 | "ca", "cad", "car", "cay", "cch", "ccp", "ce", "ceb", "cgg", |
| 115 | "ch", "chb", "chg", "chk", "chm", "chn", "cho", "chp", |
| 116 | "chr", "chy", "ckb", "co", "cop", "cps", "cr", "crh", |
| 117 | "cs", "csb", "csw", "cu", "cv", "cy", |
| 118 | "da", "dak", "dar", "dav", "de", "del", "den", "dgr", |
| 119 | "din", "dje", "doi", "dsb", "dtp", "dua", "dum", "dv", |
| 120 | "dyo", "dyu", "dz", "dzg", |
| 121 | "ebu", "ee", "efi", "egl", "egy", "eka", "el", "elx", |
| 122 | "en", "enm", "eo", "es", "esu", "et", "eu", "ewo", |
| 123 | "ext", |
| 124 | "fa", "fan", "fat", "ff", "fi", "fil", "fit", "fj", |
| 125 | "fo", "fon", "fr", "frc", "frm", "fro", "frp", "frr", |
| 126 | "frs", "fur", "fy", |
| 127 | "ga", "gaa", "gag", "gan", "gay", "gba", "gbz", "gd", |
| 128 | "gez", "gil", "gl", "glk", "gmh", "gn", "goh", "gom", |
| 129 | "gon", "gor", "got", "grb", "grc", "gsw", "gu", "guc", |
| 130 | "gur", "guz", "gv", "gwi", |
| 131 | "ha", "hai", "hak", "haw", "he", "hi", "hif", "hil", |
| 132 | "hit", "hmn", "ho", "hr", "hsb", "hsn", "ht", "hu", |
| 133 | "hup", "hy", "hz", |
| 134 | "ia", "iba", "ibb", "id", "ie", "ig", "ii", "ik", |
| 135 | "ilo", "inh", "io", "is", "it", "iu", "izh", |
| 136 | "ja", "jam", "jbo", "jgo", "jmc", "jpr", "jrb", "jut", |
| 137 | "jv", |
| 138 | "ka", "kaa", "kab", "kac", "kaj", "kam", "kaw", "kbd", |
| 139 | "kbl", "kcg", "kde", "kea", "ken", "kfo", "kg", "kgp", |
| 140 | "kha", "kho", "khq", "khw", "ki", "kiu", "kj", "kk", |
| 141 | "kkj", "kl", "kln", "km", "kmb", "kn", "ko", "koi", |
| 142 | "kok", "kos", "kpe", "kr", "krc", "kri", "krj", "krl", |
| 143 | "kru", "ks", "ksb", "ksf", "ksh", "ku", "kum", "kut", |
| 144 | "kv", "kw", "kxv", "ky", |
| 145 | "la", "lad", "lag", "lah", "lam", "lb", "lez", "lfn", |
| 146 | "lg", "li", "lij", "liv", "lkt", "lmo", "ln", "lo", |
| 147 | "lol", "loz", "lrc", "lt", "ltg", "lu", "lua", "lui", |
| 148 | "lun", "luo", "lus", "luy", "lv", "lzh", "lzz", |
| 149 | "mad", "maf", "mag", "mai", "mak", "man", "mas", "mde", |
| 150 | "mdf", "mdh", "mdr", "men", "mer", "mfe", "mg", "mga", |
| 151 | "mgh", "mgo", "mh", "mi", "mic", "min", "mis", "mk", |
| 152 | "ml", "mn", "mnc", "mni", |
| 153 | "moh", "mos", "mr", "mrj", |
| 154 | "ms", "mt", "mua", "mul", "mus", "mwl", "mwr", "mwv", |
| 155 | "my", "mye", "myv", "mzn", |
| 156 | "na", "nan", "nap", "naq", "nb", "nd", "nds", "ne", |
| 157 | "new", "ng", "nia", "niu", "njo", "nl", "nmg", "nn", |
| 158 | "nnh", "no", "nog", "non", "nov", "nqo", "nr", "nso", |
| 159 | "nus", "nv", "nwc", "ny", "nym", "nyn", "nyo", "nzi", |
| 160 | "oc", "oj", "om", "or", "os", "osa", "ota", |
| 161 | "pa", "pag", "pal", "pam", "pap", "pau", "pcd", "pcm", "pdc", |
| 162 | "pdt", "peo", "pfl", "phn", "pi", "pl", "pms", "pnt", |
| 163 | "pon", "prg", "pro", "ps", "pt", |
| 164 | "qu", "quc", "qug", |
| 165 | "raj", "rap", "rar", "rgn", "rif", "rm", "rn", "ro", |
| 166 | "rof", "rom", "rtm", "ru", "rue", "rug", "rup", |
| 167 | "rw", "rwk", |
| 168 | "sa", "sad", "sah", "sam", "saq", "sas", "sat", "saz", |
| 169 | "sba", "sbp", "sc", "scn", "sco", "sd", "sdc", "sdh", |
| 170 | "se", "see", "seh", "sei", "sel", "ses", "sg", "sga", |
| 171 | "sgs", "shi", "shn", "shu", "si", "sid", "sk", |
| 172 | "sl", "sli", "sly", "sm", "sma", "smj", "smn", "sms", |
| 173 | "sn", "snk", "so", "sog", "sq", "sr", "srn", "srr", |
| 174 | "ss", "ssy", "st", "stq", "su", "suk", "sus", "sux", |
| 175 | "sv", "sw", "swb", "syc", "syr", "szl", |
| 176 | "ta", "tcy", "te", "tem", "teo", "ter", "tet", "tg", |
| 177 | "th", "ti", "tig", "tiv", "tk", "tkl", "tkr", |
| 178 | "tlh", "tli", "tly", "tmh", "tn", "to", "tog", "tok", "tpi", |
| 179 | "tr", "tru", "trv", "ts", "tsd", "tsi", "tt", "ttt", |
| 180 | "tum", "tvl", "tw", "twq", "ty", "tyv", "tzm", |
| 181 | "udm", "ug", "uga", "uk", "umb", "und", "ur", "uz", |
| 182 | "vai", "ve", "vec", "vep", "vi", "vls", "vmf", "vmw", |
| 183 | "vo", "vot", "vro", "vun", |
| 184 | "wa", "wae", "wal", "war", "was", "wbp", "wo", "wuu", |
| 185 | "xal", "xh", "xmf", "xnr", "xog", |
| 186 | "yao", "yap", "yav", "ybb", "yi", "yo", "yrl", "yue", |
| 187 | "za", "zap", "zbl", "zea", "zen", "zgh", "zh", "zu", |
| 188 | "zun", "zxx", "zza", |
| 189 | nullptr, |
| 190 | "in", "iw", "ji", "jw", "mo", "sh", "swc", "tl", /* obsolete language codes */ |
| 191 | nullptr |
| 192 | }; |
| 193 | |
| 194 | constexpr const char* DEPRECATED_LANGUAGES[]={ |
| 195 | "in", "iw", "ji", "jw", "mo", nullptr, nullptr |
| 196 | }; |
| 197 | constexpr const char* REPLACEMENT_LANGUAGES[]={ |
| 198 | "id", "he", "yi", "jv", "ro", nullptr, nullptr |
| 199 | }; |
| 200 | |
| 201 | /** |
| 202 | * Table of 3-letter language codes. |
| 203 | * |
| 204 | * This is a lookup table used to convert 3-letter language codes to |
| 205 | * their 2-letter equivalent, where possible. It must be kept in sync |
| 206 | * with LANGUAGES. For all valid i, LANGUAGES[i] must refer to the |
| 207 | * same language as LANGUAGES_3[i]. The commented-out lines are |
| 208 | * copied from LANGUAGES to make eyeballing this baby easier. |
| 209 | * |
| 210 | * Where a 3-letter language code has no 2-letter equivalent, the |
| 211 | * 3-letter code occupies both LANGUAGES[i] and LANGUAGES_3[i]. |
| 212 | * |
| 213 | * This table should be terminated with a nullptr entry, followed by a |
| 214 | * second list, and another nullptr entry. The two lists correspond to |
| 215 | * the two lists in LANGUAGES. |
| 216 | */ |
| 217 | /* Generated using org.unicode.cldr.icu.GenerateISO639LanguageTables */ |
| 218 | /* ISO639 table version is 20150505 */ |
| 219 | /* Subsequent hand addition of selected languages */ |
| 220 | constexpr const char* LANGUAGES_3[] = { |
| 221 | "aar", "abk", "ace", "ach", "ada", "ady", "ave", "aeb", |
| 222 | "afr", "afh", "agq", "ain", "aka", "akk", "akz", "ale", |
| 223 | "aln", "alt", "amh", "arg", "ang", "anp", "ara", "arc", |
| 224 | "arn", "aro", "arp", "arq", "ars", "arw", "ary", "arz", "asm", |
| 225 | "asa", "ase", "ast", "ava", "avk", "awa", "aym", "aze", |
| 226 | "bak", "bal", "ban", "bar", "bas", "bax", "bbc", "bbj", |
| 227 | "bel", "bej", "bem", "bew", "bez", "bfd", "bfq", "bul", |
| 228 | "bgc", "bgn", "bho", "bis", "bik", "bin", "bjn", "bkm", "bla", |
| 229 | "blo", "bam", "ben", "bod", "bpy", "bqi", "bre", "bra", "brh", |
| 230 | "brx", "bos", "bss", "bua", "bug", "bum", "byn", "byv", |
| 231 | "cat", "cad", "car", "cay", "cch", "ccp", "che", "ceb", "cgg", |
| 232 | "cha", "chb", "chg", "chk", "chm", "chn", "cho", "chp", |
| 233 | "chr", "chy", "ckb", "cos", "cop", "cps", "cre", "crh", |
| 234 | "ces", "csb", "csw", "chu", "chv", "cym", |
| 235 | "dan", "dak", "dar", "dav", "deu", "del", "den", "dgr", |
| 236 | "din", "dje", "doi", "dsb", "dtp", "dua", "dum", "div", |
| 237 | "dyo", "dyu", "dzo", "dzg", |
| 238 | "ebu", "ewe", "efi", "egl", "egy", "eka", "ell", "elx", |
| 239 | "eng", "enm", "epo", "spa", "esu", "est", "eus", "ewo", |
| 240 | "ext", |
| 241 | "fas", "fan", "fat", "ful", "fin", "fil", "fit", "fij", |
| 242 | "fao", "fon", "fra", "frc", "frm", "fro", "frp", "frr", |
| 243 | "frs", "fur", "fry", |
| 244 | "gle", "gaa", "gag", "gan", "gay", "gba", "gbz", "gla", |
| 245 | "gez", "gil", "glg", "glk", "gmh", "grn", "goh", "gom", |
| 246 | "gon", "gor", "got", "grb", "grc", "gsw", "guj", "guc", |
| 247 | "gur", "guz", "glv", "gwi", |
| 248 | "hau", "hai", "hak", "haw", "heb", "hin", "hif", "hil", |
| 249 | "hit", "hmn", "hmo", "hrv", "hsb", "hsn", "hat", "hun", |
| 250 | "hup", "hye", "her", |
| 251 | "ina", "iba", "ibb", "ind", "ile", "ibo", "iii", "ipk", |
| 252 | "ilo", "inh", "ido", "isl", "ita", "iku", "izh", |
| 253 | "jpn", "jam", "jbo", "jgo", "jmc", "jpr", "jrb", "jut", |
| 254 | "jav", |
| 255 | "kat", "kaa", "kab", "kac", "kaj", "kam", "kaw", "kbd", |
| 256 | "kbl", "kcg", "kde", "kea", "ken", "kfo", "kon", "kgp", |
| 257 | "kha", "kho", "khq", "khw", "kik", "kiu", "kua", "kaz", |
| 258 | "kkj", "kal", "kln", "khm", "kmb", "kan", "kor", "koi", |
| 259 | "kok", "kos", "kpe", "kau", "krc", "kri", "krj", "krl", |
| 260 | "kru", "kas", "ksb", "ksf", "ksh", "kur", "kum", "kut", |
| 261 | "kom", "cor", "kxv", "kir", |
| 262 | "lat", "lad", "lag", "lah", "lam", "ltz", "lez", "lfn", |
| 263 | "lug", "lim", "lij", "liv", "lkt", "lmo", "lin", "lao", |
| 264 | "lol", "loz", "lrc", "lit", "ltg", "lub", "lua", "lui", |
| 265 | "lun", "luo", "lus", "luy", "lav", "lzh", "lzz", |
| 266 | "mad", "maf", "mag", "mai", "mak", "man", "mas", "mde", |
| 267 | "mdf", "mdh", "mdr", "men", "mer", "mfe", "mlg", "mga", |
| 268 | "mgh", "mgo", "mah", "mri", "mic", "min", "mis", "mkd", |
| 269 | "mal", "mon", "mnc", "mni", |
| 270 | "moh", "mos", "mar", "mrj", |
| 271 | "msa", "mlt", "mua", "mul", "mus", "mwl", "mwr", "mwv", |
| 272 | "mya", "mye", "myv", "mzn", |
| 273 | "nau", "nan", "nap", "naq", "nob", "nde", "nds", "nep", |
| 274 | "new", "ndo", "nia", "niu", "njo", "nld", "nmg", "nno", |
| 275 | "nnh", "nor", "nog", "non", "nov", "nqo", "nbl", "nso", |
| 276 | "nus", "nav", "nwc", "nya", "nym", "nyn", "nyo", "nzi", |
| 277 | "oci", "oji", "orm", "ori", "oss", "osa", "ota", |
| 278 | "pan", "pag", "pal", "pam", "pap", "pau", "pcd", "pcm", "pdc", |
| 279 | "pdt", "peo", "pfl", "phn", "pli", "pol", "pms", "pnt", |
| 280 | "pon", "prg", "pro", "pus", "por", |
| 281 | "que", "quc", "qug", |
| 282 | "raj", "rap", "rar", "rgn", "rif", "roh", "run", "ron", |
| 283 | "rof", "rom", "rtm", "rus", "rue", "rug", "rup", |
| 284 | "kin", "rwk", |
| 285 | "san", "sad", "sah", "sam", "saq", "sas", "sat", "saz", |
| 286 | "sba", "sbp", "srd", "scn", "sco", "snd", "sdc", "sdh", |
| 287 | "sme", "see", "seh", "sei", "sel", "ses", "sag", "sga", |
| 288 | "sgs", "shi", "shn", "shu", "sin", "sid", "slk", |
| 289 | "slv", "sli", "sly", "smo", "sma", "smj", "smn", "sms", |
| 290 | "sna", "snk", "som", "sog", "sqi", "srp", "srn", "srr", |
| 291 | "ssw", "ssy", "sot", "stq", "sun", "suk", "sus", "sux", |
| 292 | "swe", "swa", "swb", "syc", "syr", "szl", |
| 293 | "tam", "tcy", "tel", "tem", "teo", "ter", "tet", "tgk", |
| 294 | "tha", "tir", "tig", "tiv", "tuk", "tkl", "tkr", |
| 295 | "tlh", "tli", "tly", "tmh", "tsn", "ton", "tog", "tok", "tpi", |
| 296 | "tur", "tru", "trv", "tso", "tsd", "tsi", "tat", "ttt", |
| 297 | "tum", "tvl", "twi", "twq", "tah", "tyv", "tzm", |
| 298 | "udm", "uig", "uga", "ukr", "umb", "und", "urd", "uzb", |
| 299 | "vai", "ven", "vec", "vep", "vie", "vls", "vmf", "vmw", |
| 300 | "vol", "vot", "vro", "vun", |
| 301 | "wln", "wae", "wal", "war", "was", "wbp", "wol", "wuu", |
| 302 | "xal", "xho", "xmf", "xnr", "xog", |
| 303 | "yao", "yap", "yav", "ybb", "yid", "yor", "yrl", "yue", |
| 304 | "zha", "zap", "zbl", "zea", "zen", "zgh", "zho", "zul", |
| 305 | "zun", "zxx", "zza", |
| 306 | nullptr, |
| 307 | /* "in", "iw", "ji", "jw", "mo", "sh", "swc", "tl", */ |
| 308 | "ind", "heb", "yid", "jaw", "mol", "srp", "swc", "tgl", |
| 309 | nullptr |
| 310 | }; |
| 311 | |
| 312 | /** |
| 313 | * Table of 2-letter country codes. |
| 314 | * |
| 315 | * This list must be in sorted order. This list is returned directly |
| 316 | * to the user by some API. |
| 317 | * |
| 318 | * This list must be kept in sync with COUNTRIES_3, with corresponding |
| 319 | * entries matched. |
| 320 | * |
| 321 | * This table should be terminated with a nullptr entry, followed by a |
| 322 | * second list, and another nullptr entry. The first list is visible to |
| 323 | * user code when this array is returned by API. The second list |
| 324 | * contains codes we support, but do not expose through user API. |
| 325 | * |
| 326 | * Notes: |
| 327 | * |
| 328 | * ZR(ZAR) is now CD(COD) and FX(FXX) is PS(PSE) as per |
| 329 | * http://www.evertype.com/standards/iso3166/iso3166-1-en.html added |
| 330 | * new codes keeping the old ones for compatibility updated to include |
| 331 | * 1999/12/03 revisions *CWB* |
| 332 | * |
| 333 | * RO(ROM) is now RO(ROU) according to |
| 334 | * http://www.iso.org/iso/en/prods-services/iso3166ma/03updates-on-iso-3166/nlv3e-rou.html |
| 335 | */ |
| 336 | constexpr const char* COUNTRIES[] = { |
| 337 | "AD", "AE", "AF", "AG", "AI", "AL", "AM", |
| 338 | "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", |
| 339 | "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", |
| 340 | "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", |
| 341 | "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", |
| 342 | "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CQ", "CR", |
| 343 | "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DG", "DJ", "DK", |
| 344 | "DM", "DO", "DZ", "EA", "EC", "EE", "EG", "EH", "ER", |
| 345 | "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", |
| 346 | "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", |
| 347 | "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", |
| 348 | "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", |
| 349 | "IC", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", |
| 350 | "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", |
| 351 | "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", |
| 352 | "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", |
| 353 | "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", |
| 354 | "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", |
| 355 | "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", |
| 356 | "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", |
| 357 | "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", |
| 358 | "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", |
| 359 | "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", |
| 360 | "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", |
| 361 | "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", |
| 362 | "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", |
| 363 | "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", |
| 364 | "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", |
| 365 | "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", |
| 366 | "WS", "XK", "YE", "YT", "ZA", "ZM", "ZW", |
| 367 | nullptr, |
| 368 | "AN", "BU", "CS", "FX", "RO", "SU", "TP", "YD", "YU", "ZR", /* obsolete country codes */ |
| 369 | nullptr |
| 370 | }; |
| 371 | |
| 372 | constexpr const char* DEPRECATED_COUNTRIES[] = { |
| 373 | "AN", "BU", "CS", "DD", "DY", "FX", "HV", "NH", "RH", "SU", "TP", "UK", "VD", "YD", "YU", "ZR", nullptr, nullptr /* deprecated country list */ |
| 374 | }; |
| 375 | constexpr const char* REPLACEMENT_COUNTRIES[] = { |
| 376 | /* "AN", "BU", "CS", "DD", "DY", "FX", "HV", "NH", "RH", "SU", "TP", "UK", "VD", "YD", "YU", "ZR" */ |
| 377 | "CW", "MM", "RS", "DE", "BJ", "FR", "BF", "VU", "ZW", "RU", "TL", "GB", "VN", "YE", "RS", "CD", nullptr, nullptr /* replacement country codes */ |
| 378 | }; |
| 379 | |
| 380 | /** |
| 381 | * Table of 3-letter country codes. |
| 382 | * |
| 383 | * This is a lookup table used to convert 3-letter country codes to |
| 384 | * their 2-letter equivalent. It must be kept in sync with COUNTRIES. |
| 385 | * For all valid i, COUNTRIES[i] must refer to the same country as |
| 386 | * COUNTRIES_3[i]. The commented-out lines are copied from COUNTRIES |
| 387 | * to make eyeballing this baby easier. |
| 388 | * |
| 389 | * This table should be terminated with a nullptr entry, followed by a |
| 390 | * second list, and another nullptr entry. The two lists correspond to |
| 391 | * the two lists in COUNTRIES. |
| 392 | */ |
| 393 | constexpr const char* COUNTRIES_3[] = { |
| 394 | /* "AD", "AE", "AF", "AG", "AI", "AL", "AM", */ |
| 395 | "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", |
| 396 | /* "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", */ |
| 397 | "AGO", "ATA", "ARG", "ASM", "AUT", "AUS", "ABW", "ALA", "AZE", |
| 398 | /* "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", */ |
| 399 | "BIH", "BRB", "BGD", "BEL", "BFA", "BGR", "BHR", "BDI", |
| 400 | /* "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", */ |
| 401 | "BEN", "BLM", "BMU", "BRN", "BOL", "BES", "BRA", "BHS", "BTN", "BVT", |
| 402 | /* "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", */ |
| 403 | "BWA", "BLR", "BLZ", "CAN", "CCK", "COD", "CAF", "COG", |
| 404 | /* "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CQ", "CR", */ |
| 405 | "CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRQ", "CRI", |
| 406 | /* "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DG", "DJ", "DK", */ |
| 407 | "CUB", "CPV", "CUW", "CXR", "CYP", "CZE", "DEU", "DGA", "DJI", "DNK", |
| 408 | /* "DM", "DO", "DZ", "EA", "EC", "EE", "EG", "EH", "ER", */ |
| 409 | "DMA", "DOM", "DZA", "XEA", "ECU", "EST", "EGY", "ESH", "ERI", |
| 410 | /* "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", */ |
| 411 | "ESP", "ETH", "FIN", "FJI", "FLK", "FSM", "FRO", "FRA", |
| 412 | /* "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", */ |
| 413 | "GAB", "GBR", "GRD", "GEO", "GUF", "GGY", "GHA", "GIB", "GRL", |
| 414 | /* "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", */ |
| 415 | "GMB", "GIN", "GLP", "GNQ", "GRC", "SGS", "GTM", "GUM", |
| 416 | /* "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", */ |
| 417 | "GNB", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN", |
| 418 | /* "IC", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS" */ |
| 419 | "XIC", "IDN", "IRL", "ISR", "IMN", "IND", "IOT", "IRQ", "IRN", "ISL", |
| 420 | /* "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", */ |
| 421 | "ITA", "JEY", "JAM", "JOR", "JPN", "KEN", "KGZ", "KHM", "KIR", |
| 422 | /* "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", */ |
| 423 | "COM", "KNA", "PRK", "KOR", "KWT", "CYM", "KAZ", "LAO", |
| 424 | /* "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", */ |
| 425 | "LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU", "LUX", |
| 426 | /* "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", */ |
| 427 | "LVA", "LBY", "MAR", "MCO", "MDA", "MNE", "MAF", "MDG", "MHL", "MKD", |
| 428 | /* "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", */ |
| 429 | "MLI", "MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR", |
| 430 | /* "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", */ |
| 431 | "MLT", "MUS", "MDV", "MWI", "MEX", "MYS", "MOZ", "NAM", |
| 432 | /* "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", */ |
| 433 | "NCL", "NER", "NFK", "NGA", "NIC", "NLD", "NOR", "NPL", |
| 434 | /* "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", */ |
| 435 | "NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF", "PNG", |
| 436 | /* "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", */ |
| 437 | "PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT", |
| 438 | /* "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", */ |
| 439 | "PLW", "PRY", "QAT", "REU", "ROU", "SRB", "RUS", "RWA", "SAU", |
| 440 | /* "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", */ |
| 441 | "SLB", "SYC", "SDN", "SWE", "SGP", "SHN", "SVN", "SJM", |
| 442 | /* "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", */ |
| 443 | "SVK", "SLE", "SMR", "SEN", "SOM", "SUR", "SSD", "STP", "SLV", |
| 444 | /* "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", */ |
| 445 | "SXM", "SYR", "SWZ", "TCA", "TCD", "ATF", "TGO", "THA", "TJK", |
| 446 | /* "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", */ |
| 447 | "TKL", "TLS", "TKM", "TUN", "TON", "TUR", "TTO", "TUV", |
| 448 | /* "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", */ |
| 449 | "TWN", "TZA", "UKR", "UGA", "UMI", "USA", "URY", "UZB", |
| 450 | /* "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", */ |
| 451 | "VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "VUT", "WLF", |
| 452 | /* "WS", "XK", "YE", "YT", "ZA", "ZM", "ZW", */ |
| 453 | "WSM", "XKK", "YEM", "MYT", "ZAF", "ZMB", "ZWE", |
| 454 | nullptr, |
| 455 | /* "AN", "BU", "CS", "FX", "RO", "SU", "TP", "YD", "YU", "ZR" */ |
| 456 | "ANT", "BUR", "SCG", "FXX", "ROM", "SUN", "TMP", "YMD", "YUG", "ZAR", |
| 457 | nullptr |
| 458 | }; |
| 459 | |
| 460 | typedef struct CanonicalizationMap { |
| 461 | const char *id; /* input ID */ |
| 462 | const char *canonicalID; /* canonicalized output ID */ |
| 463 | } CanonicalizationMap; |
| 464 | |
| 465 | /** |
| 466 | * A map to canonicalize locale IDs. This handles a variety of |
| 467 | * different semantic kinds of transformations. |
| 468 | */ |
| 469 | constexpr CanonicalizationMap CANONICALIZE_MAP[] = { |
| 470 | { "art__LOJBAN", "jbo" }, /* registered name */ |
| 471 | { "hy__AREVELA", "hy" }, /* Registered IANA variant */ |
| 472 | { "hy__AREVMDA", "hyw" }, /* Registered IANA variant */ |
| 473 | { "zh__GUOYU", "zh" }, /* registered name */ |
| 474 | { "zh__HAKKA", "hak" }, /* registered name */ |
| 475 | { "zh__XIANG", "hsn" }, /* registered name */ |
| 476 | // subtags with 3 chars won't be treated as variants. |
| 477 | { "zh_GAN", "gan" }, /* registered name */ |
| 478 | { "zh_MIN_NAN", "nan" }, /* registered name */ |
| 479 | { "zh_WUU", "wuu" }, /* registered name */ |
| 480 | { "zh_YUE", "yue" }, /* registered name */ |
| 481 | }; |
| 482 | |
| 483 | /* ### BCP47 Conversion *******************************************/ |
| 484 | /* Gets the size of the shortest subtag in the given localeID. */ |
| 485 | int32_t getShortestSubtagLength(std::string_view localeID) { |
| 486 | int32_t localeIDLength = static_cast<int32_t>(localeID.length()); |
| 487 | int32_t length = localeIDLength; |
| 488 | int32_t tmpLength = 0; |
| 489 | int32_t i; |
| 490 | bool reset = true; |
| 491 | |
| 492 | for (i = 0; i < localeIDLength; i++) { |
| 493 | if (localeID[i] != '_' && localeID[i] != '-') { |
| 494 | if (reset) { |
| 495 | tmpLength = 0; |
| 496 | reset = false; |
| 497 | } |
| 498 | tmpLength++; |
| 499 | } else { |
| 500 | if (tmpLength != 0 && tmpLength < length) { |
| 501 | length = tmpLength; |
| 502 | } |
| 503 | reset = true; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return length; |
| 508 | } |
| 509 | /* Test if the locale id has BCP47 u extension and does not have '@' */ |
| 510 | inline bool _hasBCP47Extension(std::string_view id) { |
| 511 | return id.find('@') == std::string_view::npos && getShortestSubtagLength(id) == 1; |
| 512 | } |
| 513 | |
| 514 | /* ### Keywords **************************************************/ |
| 515 | inline bool UPRV_ISDIGIT(char c) { return c >= '0' && c <= '9'; } |
| 516 | inline bool UPRV_ISALPHANUM(char c) { return uprv_isASCIILetter(c) || UPRV_ISDIGIT(c); } |
| 517 | /* Punctuation/symbols allowed in legacy key values */ |
| 518 | inline bool UPRV_OK_VALUE_PUNCTUATION(char c) { return c == '_' || c == '-' || c == '+' || c == '/'; } |
| 519 | |
| 520 | } // namespace |
| 521 | |
| 522 | #define ULOC_KEYWORD_BUFFER_LEN 25 |
| 523 | #define ULOC_MAX_NO_KEYWORDS 25 |
| 524 | |
| 525 | U_CAPI const char * U_EXPORT2 |
| 526 | locale_getKeywordsStart(std::string_view localeID) { |
| 527 | if (size_t pos = localeID.find('@'); pos != std::string_view::npos) { |
| 528 | return localeID.data() + pos; |
| 529 | } |
| 530 | #if (U_CHARSET_FAMILY == U_EBCDIC_FAMILY) |
| 531 | else { |
| 532 | /* We do this because the @ sign is variant, and the @ sign used on one |
| 533 | EBCDIC machine won't be compiled the same way on other EBCDIC based |
| 534 | machines. */ |
| 535 | static const uint8_t ebcdicSigns[] = { 0x7C, 0x44, 0x66, 0x80, 0xAC, 0xAE, 0xAF, 0xB5, 0xEC, 0xEF, 0x00 }; |
| 536 | const uint8_t *charToFind = ebcdicSigns; |
| 537 | while(*charToFind) { |
| 538 | if (size_t pos = localeID.find(*charToFind); pos != std::string_view::npos) { |
| 539 | return localeID.data() + pos; |
| 540 | } |
| 541 | charToFind++; |
| 542 | } |
| 543 | } |
| 544 | #endif |
| 545 | return nullptr; |
| 546 | } |
| 547 | |
| 548 | namespace { |
| 549 | |
| 550 | /** |
| 551 | * @param keywordName incoming name to be canonicalized |
| 552 | * @param status return status (keyword too long) |
| 553 | * @return the keyword name |
| 554 | */ |
| 555 | CharString locale_canonKeywordName(std::string_view keywordName, UErrorCode& status) |
| 556 | { |
| 557 | if (U_FAILURE(status)) { return {}; } |
| 558 | CharString result; |
| 559 | |
| 560 | for (char c : keywordName) { |
| 561 | if (!UPRV_ISALPHANUM(c)) { |
| 562 | status = U_ILLEGAL_ARGUMENT_ERROR; /* malformed keyword name */ |
| 563 | return {}; |
| 564 | } |
| 565 | result.append(uprv_tolower(c), status); |
| 566 | } |
| 567 | if (result.isEmpty()) { |
| 568 | status = U_ILLEGAL_ARGUMENT_ERROR; /* empty keyword name */ |
| 569 | return {}; |
| 570 | } |
| 571 | |
| 572 | return result; |
| 573 | } |
| 574 | |
| 575 | typedef struct { |
| 576 | char keyword[ULOC_KEYWORD_BUFFER_LEN]; |
| 577 | int32_t keywordLen; |
| 578 | const char *valueStart; |
| 579 | int32_t valueLen; |
| 580 | } KeywordStruct; |
| 581 | |
| 582 | int32_t U_CALLCONV |
| 583 | compareKeywordStructs(const void * /*context*/, const void *left, const void *right) { |
| 584 | const char* leftString = static_cast<const KeywordStruct*>(left)->keyword; |
| 585 | const char* rightString = static_cast<const KeywordStruct*>(right)->keyword; |
| 586 | return uprv_strcmp(leftString, rightString); |
| 587 | } |
| 588 | |
| 589 | } // namespace |
| 590 | |
| 591 | U_EXPORT CharString |
| 592 | ulocimp_getKeywords(std::string_view localeID, |
| 593 | char prev, |
| 594 | bool valuesToo, |
| 595 | UErrorCode& status) |
| 596 | { |
| 597 | return ByteSinkUtil::viaByteSinkToCharString( |
| 598 | [&](ByteSink& sink, UErrorCode& status) { |
| 599 | ulocimp_getKeywords(localeID, |
| 600 | prev, |
| 601 | sink, |
| 602 | valuesToo, |
| 603 | status); |
| 604 | }, |
| 605 | status); |
| 606 | } |
| 607 | |
| 608 | U_EXPORT void |
| 609 | ulocimp_getKeywords(std::string_view localeID, |
| 610 | char prev, |
| 611 | ByteSink& sink, |
| 612 | bool valuesToo, |
| 613 | UErrorCode& status) |
| 614 | { |
| 615 | if (U_FAILURE(status)) { return; } |
| 616 | |
| 617 | KeywordStruct keywordList[ULOC_MAX_NO_KEYWORDS]; |
| 618 | |
| 619 | int32_t maxKeywords = ULOC_MAX_NO_KEYWORDS; |
| 620 | int32_t numKeywords = 0; |
| 621 | size_t equalSign = std::string_view::npos; |
| 622 | size_t semicolon = std::string_view::npos; |
| 623 | int32_t i = 0, j, n; |
| 624 | |
| 625 | if(prev == '@') { /* start of keyword definition */ |
| 626 | /* we will grab pairs, trim spaces, lowercase keywords, sort and return */ |
| 627 | do { |
| 628 | bool duplicate = false; |
| 629 | /* skip leading spaces */ |
| 630 | while (localeID.front() == ' ') { |
| 631 | localeID.remove_prefix(1); |
| 632 | } |
| 633 | if (localeID.empty()) { /* handle trailing "; " */ |
| 634 | break; |
| 635 | } |
| 636 | if(numKeywords == maxKeywords) { |
| 637 | status = U_INTERNAL_PROGRAM_ERROR; |
| 638 | return; |
| 639 | } |
| 640 | equalSign = localeID.find('='); |
| 641 | semicolon = localeID.find(';'); |
| 642 | /* lack of '=' [foo@currency] is illegal */ |
| 643 | /* ';' before '=' [foo@currency;collation=pinyin] is illegal */ |
| 644 | if (equalSign == std::string_view::npos || |
| 645 | (semicolon != std::string_view::npos && semicolon < equalSign)) { |
| 646 | status = U_INVALID_FORMAT_ERROR; |
| 647 | return; |
| 648 | } |
| 649 | /* zero-length keyword is an error. */ |
| 650 | if (equalSign == 0) { |
| 651 | status = U_INVALID_FORMAT_ERROR; |
| 652 | return; |
| 653 | } |
| 654 | /* need to normalize both keyword and keyword name */ |
| 655 | if (equalSign >= ULOC_KEYWORD_BUFFER_LEN) { |
| 656 | /* keyword name too long for internal buffer */ |
| 657 | status = U_INTERNAL_PROGRAM_ERROR; |
| 658 | return; |
| 659 | } |
| 660 | for (i = 0, n = 0; static_cast<size_t>(i) < equalSign; ++i) { |
| 661 | if (localeID[i] != ' ') { |
| 662 | keywordList[numKeywords].keyword[n++] = uprv_tolower(localeID[i]); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | keywordList[numKeywords].keyword[n] = 0; |
| 667 | keywordList[numKeywords].keywordLen = n; |
| 668 | /* now grab the value part. First we skip the '=' */ |
| 669 | equalSign++; |
| 670 | /* then we leading spaces */ |
| 671 | while (equalSign < localeID.length() && localeID[equalSign] == ' ') { |
| 672 | equalSign++; |
| 673 | } |
| 674 | |
| 675 | /* Premature end or zero-length value */ |
| 676 | if (equalSign == localeID.length() || equalSign == semicolon) { |
| 677 | status = U_INVALID_FORMAT_ERROR; |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | keywordList[numKeywords].valueStart = localeID.data() + equalSign; |
| 682 | |
| 683 | std::string_view value = localeID; |
| 684 | if (semicolon != std::string_view::npos) { |
| 685 | value.remove_suffix(value.length() - semicolon); |
| 686 | localeID.remove_prefix(semicolon + 1); |
| 687 | } else { |
| 688 | localeID = {}; |
| 689 | } |
| 690 | value.remove_prefix(equalSign); |
| 691 | if (size_t last = value.find_last_not_of(' '); last != std::string_view::npos) { |
| 692 | value.remove_suffix(value.length() - last - 1); |
| 693 | } |
| 694 | keywordList[numKeywords].valueLen = static_cast<int32_t>(value.length()); |
| 695 | |
| 696 | /* If this is a duplicate keyword, then ignore it */ |
| 697 | for (j=0; j<numKeywords; ++j) { |
| 698 | if (uprv_strcmp(keywordList[j].keyword, keywordList[numKeywords].keyword) == 0) { |
| 699 | duplicate = true; |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | if (!duplicate) { |
| 704 | ++numKeywords; |
| 705 | } |
| 706 | } while (!localeID.empty()); |
| 707 | |
| 708 | /* now we have a list of keywords */ |
| 709 | /* we need to sort it */ |
| 710 | uprv_sortArray(keywordList, numKeywords, sizeof(KeywordStruct), compareKeywordStructs, nullptr, false, &status); |
| 711 | |
| 712 | /* Now construct the keyword part */ |
| 713 | for(i = 0; i < numKeywords; i++) { |
| 714 | sink.Append(keywordList[i].keyword, keywordList[i].keywordLen); |
| 715 | if(valuesToo) { |
| 716 | sink.Append("=", 1); |
| 717 | sink.Append(keywordList[i].valueStart, keywordList[i].valueLen); |
| 718 | if(i < numKeywords - 1) { |
| 719 | sink.Append(";", 1); |
| 720 | } |
| 721 | } else { |
| 722 | sink.Append("\0", 1); |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | U_CAPI int32_t U_EXPORT2 |
| 729 | uloc_getKeywordValue(const char* localeID, |
| 730 | const char* keywordName, |
| 731 | char* buffer, int32_t bufferCapacity, |
| 732 | UErrorCode* status) |
| 733 | { |
| 734 | if (U_FAILURE(*status)) { return 0; } |
| 735 | if (keywordName == nullptr || *keywordName == '\0') { |
| 736 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
| 737 | return 0; |
| 738 | } |
| 739 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 740 | buffer, bufferCapacity, |
| 741 | [&](ByteSink& sink, UErrorCode& status) { |
| 742 | ulocimp_getKeywordValue(localeID, keywordName, sink, status); |
| 743 | }, |
| 744 | *status); |
| 745 | } |
| 746 | |
| 747 | U_EXPORT CharString |
| 748 | ulocimp_getKeywordValue(const char* localeID, |
| 749 | std::string_view keywordName, |
| 750 | UErrorCode& status) |
| 751 | { |
| 752 | return ByteSinkUtil::viaByteSinkToCharString( |
| 753 | [&](ByteSink& sink, UErrorCode& status) { |
| 754 | ulocimp_getKeywordValue(localeID, keywordName, sink, status); |
| 755 | }, |
| 756 | status); |
| 757 | } |
| 758 | |
| 759 | U_EXPORT void |
| 760 | ulocimp_getKeywordValue(const char* localeID, |
| 761 | std::string_view keywordName, |
| 762 | icu::ByteSink& sink, |
| 763 | UErrorCode& status) |
| 764 | { |
| 765 | if (U_FAILURE(status)) { return; } |
| 766 | |
| 767 | if (localeID == nullptr || keywordName.empty()) { |
| 768 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | const char* startSearchHere = nullptr; |
| 773 | const char* nextSeparator = nullptr; |
| 774 | |
| 775 | CharString tempBuffer; |
| 776 | const char* tmpLocaleID; |
| 777 | |
| 778 | CharString canonKeywordName = locale_canonKeywordName(keywordName, status); |
| 779 | if (U_FAILURE(status)) { |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | if (localeID != nullptr && _hasBCP47Extension(localeID)) { |
| 784 | tempBuffer = ulocimp_forLanguageTag(localeID, -1, nullptr, status); |
| 785 | tmpLocaleID = U_SUCCESS(status) && !tempBuffer.isEmpty() ? tempBuffer.data() : localeID; |
| 786 | } else { |
| 787 | tmpLocaleID=localeID; |
| 788 | } |
| 789 | |
| 790 | startSearchHere = locale_getKeywordsStart(tmpLocaleID); |
| 791 | if(startSearchHere == nullptr) { |
| 792 | /* no keywords, return at once */ |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | /* find the first keyword */ |
| 797 | while(startSearchHere) { |
| 798 | const char* keyValueTail; |
| 799 | |
| 800 | startSearchHere++; /* skip @ or ; */ |
| 801 | nextSeparator = uprv_strchr(startSearchHere, '='); |
| 802 | if(!nextSeparator) { |
| 803 | status = U_ILLEGAL_ARGUMENT_ERROR; /* key must have =value */ |
| 804 | return; |
| 805 | } |
| 806 | /* strip leading & trailing spaces (TC decided to tolerate these) */ |
| 807 | while(*startSearchHere == ' ') { |
| 808 | startSearchHere++; |
| 809 | } |
| 810 | keyValueTail = nextSeparator; |
| 811 | while (keyValueTail > startSearchHere && *(keyValueTail-1) == ' ') { |
| 812 | keyValueTail--; |
| 813 | } |
| 814 | /* now keyValueTail points to first char after the keyName */ |
| 815 | /* copy & normalize keyName from locale */ |
| 816 | if (startSearchHere == keyValueTail) { |
| 817 | status = U_ILLEGAL_ARGUMENT_ERROR; /* empty keyword name in passed-in locale */ |
| 818 | return; |
| 819 | } |
| 820 | CharString localeKeywordName; |
| 821 | while (startSearchHere < keyValueTail) { |
| 822 | if (!UPRV_ISALPHANUM(*startSearchHere)) { |
| 823 | status = U_ILLEGAL_ARGUMENT_ERROR; /* malformed keyword name */ |
| 824 | return; |
| 825 | } |
| 826 | localeKeywordName.append(uprv_tolower(*startSearchHere++), status); |
| 827 | } |
| 828 | if (U_FAILURE(status)) { |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | startSearchHere = uprv_strchr(nextSeparator, ';'); |
| 833 | |
| 834 | if (canonKeywordName == localeKeywordName) { |
| 835 | /* current entry matches the keyword. */ |
| 836 | nextSeparator++; /* skip '=' */ |
| 837 | /* First strip leading & trailing spaces (TC decided to tolerate these) */ |
| 838 | while(*nextSeparator == ' ') { |
| 839 | nextSeparator++; |
| 840 | } |
| 841 | keyValueTail = (startSearchHere)? startSearchHere: nextSeparator + uprv_strlen(nextSeparator); |
| 842 | while(keyValueTail > nextSeparator && *(keyValueTail-1) == ' ') { |
| 843 | keyValueTail--; |
| 844 | } |
| 845 | /* Now copy the value, but check well-formedness */ |
| 846 | if (nextSeparator == keyValueTail) { |
| 847 | status = U_ILLEGAL_ARGUMENT_ERROR; /* empty key value name in passed-in locale */ |
| 848 | return; |
| 849 | } |
| 850 | while (nextSeparator < keyValueTail) { |
| 851 | if (!UPRV_ISALPHANUM(*nextSeparator) && !UPRV_OK_VALUE_PUNCTUATION(*nextSeparator)) { |
| 852 | status = U_ILLEGAL_ARGUMENT_ERROR; /* malformed key value */ |
| 853 | return; |
| 854 | } |
| 855 | /* Should we lowercase value to return here? Tests expect as-is. */ |
| 856 | sink.Append(nextSeparator++, 1); |
| 857 | } |
| 858 | return; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | U_CAPI int32_t U_EXPORT2 |
| 864 | uloc_setKeywordValue(const char* keywordName, |
| 865 | const char* keywordValue, |
| 866 | char* buffer, int32_t bufferCapacity, |
| 867 | UErrorCode* status) |
| 868 | { |
| 869 | if (U_FAILURE(*status)) { return 0; } |
| 870 | |
| 871 | if (keywordName == nullptr || *keywordName == 0) { |
| 872 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | if (bufferCapacity <= 1) { |
| 877 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | int32_t bufLen = (int32_t)uprv_strlen(buffer); |
| 882 | if(bufferCapacity<bufLen) { |
| 883 | /* The capacity is less than the length?! Is this NUL terminated? */ |
| 884 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | char* keywords = const_cast<char*>( |
| 889 | locale_getKeywordsStart({buffer, static_cast<std::string_view::size_type>(bufLen)})); |
| 890 | int32_t baseLen = keywords == nullptr ? bufLen : keywords - buffer; |
| 891 | // Remove -1 from the capacity so that this function can guarantee NUL termination. |
| 892 | CheckedArrayByteSink sink(keywords == nullptr ? buffer + bufLen : keywords, |
| 893 | bufferCapacity - baseLen - 1); |
| 894 | int32_t reslen = ulocimp_setKeywordValue( |
| 895 | keywords == nullptr ? std::string_view() : keywords, |
| 896 | keywordName, |
| 897 | keywordValue == nullptr ? std::string_view() : keywordValue, |
| 898 | sink, |
| 899 | *status); |
| 900 | |
| 901 | if (U_FAILURE(*status)) { |
| 902 | return *status == U_BUFFER_OVERFLOW_ERROR ? reslen + baseLen : 0; |
| 903 | } |
| 904 | |
| 905 | // See the documentation for this function, it's guaranteed to never |
| 906 | // overflow the buffer but instead abort with BUFFER_OVERFLOW_ERROR. |
| 907 | // In this case, nothing has been written to the sink, so it cannot have Overflowed(). |
| 908 | U_ASSERT(!sink.Overflowed()); |
| 909 | U_ASSERT(reslen >= 0); |
| 910 | return u_terminateChars(buffer, bufferCapacity, reslen + baseLen, status); |
| 911 | } |
| 912 | |
| 913 | U_EXPORT void |
| 914 | ulocimp_setKeywordValue(std::string_view keywordName, |
| 915 | std::string_view keywordValue, |
| 916 | CharString& localeID, |
| 917 | UErrorCode& status) |
| 918 | { |
| 919 | if (U_FAILURE(status)) { return; } |
| 920 | std::string_view keywords; |
| 921 | if (const char* start = locale_getKeywordsStart(localeID.toStringPiece()); start != nullptr) { |
| 922 | // This is safe because CharString::truncate() doesn't actually erase any |
| 923 | // data, but simply sets the position for where new data will be written. |
| 924 | int32_t size = start - localeID.data(); |
| 925 | keywords = localeID.toStringPiece(); |
| 926 | keywords.remove_prefix(size); |
| 927 | localeID.truncate(size); |
| 928 | } |
| 929 | CharStringByteSink sink(&localeID); |
| 930 | ulocimp_setKeywordValue(keywords, keywordName, keywordValue, sink, status); |
| 931 | } |
| 932 | |
| 933 | U_EXPORT int32_t |
| 934 | ulocimp_setKeywordValue(std::string_view keywords, |
| 935 | std::string_view keywordName, |
| 936 | std::string_view keywordValue, |
| 937 | ByteSink& sink, |
| 938 | UErrorCode& status) |
| 939 | { |
| 940 | if (U_FAILURE(status)) { return 0; } |
| 941 | |
| 942 | /* TODO: sorting. removal. */ |
| 943 | int32_t needLen = 0; |
| 944 | int32_t rc; |
| 945 | CharString updatedKeysAndValues; |
| 946 | bool handledInputKeyAndValue = false; |
| 947 | char keyValuePrefix = '@'; |
| 948 | |
| 949 | if (status == U_STRING_NOT_TERMINATED_WARNING) { |
| 950 | status = U_ZERO_ERROR; |
| 951 | } |
| 952 | if (keywordName.empty()) { |
| 953 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 954 | return 0; |
| 955 | } |
| 956 | CharString canonKeywordName = locale_canonKeywordName(keywordName, status); |
| 957 | if (U_FAILURE(status)) { |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | CharString canonKeywordValue; |
| 962 | for (char c : keywordValue) { |
| 963 | if (!UPRV_ISALPHANUM(c) && !UPRV_OK_VALUE_PUNCTUATION(c)) { |
| 964 | status = U_ILLEGAL_ARGUMENT_ERROR; /* malformed key value */ |
| 965 | return 0; |
| 966 | } |
| 967 | /* Should we force lowercase in value to set? */ |
| 968 | canonKeywordValue.append(c, status); |
| 969 | } |
| 970 | if (U_FAILURE(status)) { |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | if (keywords.size() <= 1) { |
| 975 | if (canonKeywordValue.isEmpty()) { /* no keywords = nothing to remove */ |
| 976 | U_ASSERT(status != U_STRING_NOT_TERMINATED_WARNING); |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | needLen = 1 + canonKeywordName.length() + 1 + canonKeywordValue.length(); |
| 981 | int32_t capacity = 0; |
| 982 | char* buffer = sink.GetAppendBuffer( |
| 983 | needLen, needLen, nullptr, needLen, &capacity); |
| 984 | if (capacity < needLen || buffer == nullptr) { |
| 985 | status = U_BUFFER_OVERFLOW_ERROR; |
| 986 | return needLen; /* no change */ |
| 987 | } |
| 988 | char* it = buffer; |
| 989 | |
| 990 | *it++ = '@'; |
| 991 | uprv_memcpy(it, canonKeywordName.data(), canonKeywordName.length()); |
| 992 | it += canonKeywordName.length(); |
| 993 | *it++ = '='; |
| 994 | uprv_memcpy(it, canonKeywordValue.data(), canonKeywordValue.length()); |
| 995 | sink.Append(buffer, needLen); |
| 996 | U_ASSERT(status != U_STRING_NOT_TERMINATED_WARNING); |
| 997 | return needLen; |
| 998 | } /* end shortcut - no @ */ |
| 999 | |
| 1000 | /* search for keyword */ |
| 1001 | for (size_t keywordStart = 0; keywordStart != std::string_view::npos;) { |
| 1002 | keywordStart++; /* skip @ or ; */ |
| 1003 | size_t nextEqualsign = keywords.find('=', keywordStart); |
| 1004 | if (nextEqualsign == std::string_view::npos) { |
| 1005 | status = U_ILLEGAL_ARGUMENT_ERROR; /* key must have =value */ |
| 1006 | return 0; |
| 1007 | } |
| 1008 | /* strip leading & trailing spaces (TC decided to tolerate these) */ |
| 1009 | while (keywordStart < keywords.size() && keywords[keywordStart] == ' ') { |
| 1010 | keywordStart++; |
| 1011 | } |
| 1012 | size_t keyValueTail = nextEqualsign; |
| 1013 | while (keyValueTail > keywordStart && keywords[keyValueTail - 1] == ' ') { |
| 1014 | keyValueTail--; |
| 1015 | } |
| 1016 | /* now keyValueTail points to first char after the keyName */ |
| 1017 | /* copy & normalize keyName from locale */ |
| 1018 | if (keywordStart == keyValueTail) { |
| 1019 | status = U_ILLEGAL_ARGUMENT_ERROR; /* empty keyword name in passed-in locale */ |
| 1020 | return 0; |
| 1021 | } |
| 1022 | CharString localeKeywordName; |
| 1023 | while (keywordStart < keyValueTail) { |
| 1024 | if (!UPRV_ISALPHANUM(keywords[keywordStart])) { |
| 1025 | status = U_ILLEGAL_ARGUMENT_ERROR; /* malformed keyword name */ |
| 1026 | return 0; |
| 1027 | } |
| 1028 | localeKeywordName.append(uprv_tolower(keywords[keywordStart++]), status); |
| 1029 | } |
| 1030 | if (U_FAILURE(status)) { |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | size_t nextSeparator = keywords.find(';', nextEqualsign); |
| 1035 | |
| 1036 | /* start processing the value part */ |
| 1037 | nextEqualsign++; /* skip '=' */ |
| 1038 | /* First strip leading & trailing spaces (TC decided to tolerate these) */ |
| 1039 | while (nextEqualsign < keywords.size() && keywords[nextEqualsign] == ' ') { |
| 1040 | nextEqualsign++; |
| 1041 | } |
| 1042 | keyValueTail = nextSeparator == std::string_view::npos ? keywords.size() : nextSeparator; |
| 1043 | while (keyValueTail > nextEqualsign && keywords[keyValueTail - 1] == ' ') { |
| 1044 | keyValueTail--; |
| 1045 | } |
| 1046 | if (nextEqualsign == keyValueTail) { |
| 1047 | status = U_ILLEGAL_ARGUMENT_ERROR; /* empty key value in passed-in locale */ |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | rc = uprv_strcmp(canonKeywordName.data(), localeKeywordName.data()); |
| 1052 | if(rc == 0) { |
| 1053 | /* Current entry matches the input keyword. Update the entry */ |
| 1054 | if (!canonKeywordValue.isEmpty()) { /* updating a value */ |
| 1055 | updatedKeysAndValues.append(keyValuePrefix, status); |
| 1056 | keyValuePrefix = ';'; /* for any subsequent key-value pair */ |
| 1057 | updatedKeysAndValues.append(canonKeywordName, status); |
| 1058 | updatedKeysAndValues.append('=', status); |
| 1059 | updatedKeysAndValues.append(canonKeywordValue, status); |
| 1060 | } /* else removing this entry, don't emit anything */ |
| 1061 | handledInputKeyAndValue = true; |
| 1062 | } else { |
| 1063 | /* input keyword sorts earlier than current entry, add before current entry */ |
| 1064 | if (rc < 0 && !canonKeywordValue.isEmpty() && !handledInputKeyAndValue) { |
| 1065 | /* insert new entry at this location */ |
| 1066 | updatedKeysAndValues.append(keyValuePrefix, status); |
| 1067 | keyValuePrefix = ';'; /* for any subsequent key-value pair */ |
| 1068 | updatedKeysAndValues.append(canonKeywordName, status); |
| 1069 | updatedKeysAndValues.append('=', status); |
| 1070 | updatedKeysAndValues.append(canonKeywordValue, status); |
| 1071 | handledInputKeyAndValue = true; |
| 1072 | } |
| 1073 | /* copy the current entry */ |
| 1074 | updatedKeysAndValues.append(keyValuePrefix, status); |
| 1075 | keyValuePrefix = ';'; /* for any subsequent key-value pair */ |
| 1076 | updatedKeysAndValues.append(localeKeywordName, status); |
| 1077 | updatedKeysAndValues.append('=', status); |
| 1078 | updatedKeysAndValues.append(keywords.data() + nextEqualsign, |
| 1079 | static_cast<int32_t>(keyValueTail - nextEqualsign), status); |
| 1080 | } |
| 1081 | if (nextSeparator == std::string_view::npos && !canonKeywordValue.isEmpty() && !handledInputKeyAndValue) { |
| 1082 | /* append new entry at the end, it sorts later than existing entries */ |
| 1083 | updatedKeysAndValues.append(keyValuePrefix, status); |
| 1084 | /* skip keyValuePrefix update, no subsequent key-value pair */ |
| 1085 | updatedKeysAndValues.append(canonKeywordName, status); |
| 1086 | updatedKeysAndValues.append('=', status); |
| 1087 | updatedKeysAndValues.append(canonKeywordValue, status); |
| 1088 | handledInputKeyAndValue = true; |
| 1089 | } |
| 1090 | keywordStart = nextSeparator; |
| 1091 | } /* end loop searching */ |
| 1092 | |
| 1093 | /* Any error from updatedKeysAndValues.append above would be internal and not due to |
| 1094 | * problems with the passed-in locale. So if we did encounter problems with the |
| 1095 | * passed-in locale above, those errors took precedence and overrode any error |
| 1096 | * status from updatedKeysAndValues.append, and also caused a return of 0. If there |
| 1097 | * are errors here they are from updatedKeysAndValues.append; they do cause an |
| 1098 | * error return but the passed-in locale is unmodified and the original bufLen is |
| 1099 | * returned. |
| 1100 | */ |
| 1101 | if (!handledInputKeyAndValue || U_FAILURE(status)) { |
| 1102 | /* if input key/value specified removal of a keyword not present in locale, or |
| 1103 | * there was an error in CharString.append, leave original locale alone. */ |
| 1104 | U_ASSERT(status != U_STRING_NOT_TERMINATED_WARNING); |
| 1105 | return static_cast<int32_t>(keywords.size()); |
| 1106 | } |
| 1107 | |
| 1108 | needLen = updatedKeysAndValues.length(); |
| 1109 | // Check to see can we fit the updatedKeysAndValues, if not, return |
| 1110 | // U_BUFFER_OVERFLOW_ERROR without copy updatedKeysAndValues into it. |
| 1111 | // We do this because this API function does not behave like most others: |
| 1112 | // It promises never to set a U_STRING_NOT_TERMINATED_WARNING. |
| 1113 | // When the contents fits but without the terminating NUL, in this case we need to not change |
| 1114 | // the buffer contents and return with a buffer overflow error. |
| 1115 | if (needLen > 0) { |
| 1116 | int32_t capacity = 0; |
| 1117 | char* buffer = sink.GetAppendBuffer( |
| 1118 | needLen, needLen, nullptr, needLen, &capacity); |
| 1119 | if (capacity < needLen || buffer == nullptr) { |
| 1120 | status = U_BUFFER_OVERFLOW_ERROR; |
| 1121 | return needLen; |
| 1122 | } |
| 1123 | uprv_memcpy(buffer, updatedKeysAndValues.data(), needLen); |
| 1124 | sink.Append(buffer, needLen); |
| 1125 | } |
| 1126 | U_ASSERT(status != U_STRING_NOT_TERMINATED_WARNING); |
| 1127 | return needLen; |
| 1128 | } |
| 1129 | |
| 1130 | /* ### ID parsing implementation **************************************************/ |
| 1131 | |
| 1132 | namespace { |
| 1133 | |
| 1134 | inline bool _isPrefixLetter(char a) { return a == 'x' || a == 'X' || a == 'i' || a == 'I'; } |
| 1135 | |
| 1136 | /*returns true if one of the special prefixes is here (s=string) |
| 1137 | 'x-' or 'i-' */ |
| 1138 | inline bool _isIDPrefix(std::string_view s) { |
| 1139 | return s.size() >= 2 && _isPrefixLetter(s[0]) && _isIDSeparator(s[1]); |
| 1140 | } |
| 1141 | |
| 1142 | /* Dot terminates it because of POSIX form where dot precedes the codepage |
| 1143 | * except for variant |
| 1144 | */ |
| 1145 | inline bool _isTerminator(char a) { return a == '.' || a == '@'; } |
| 1146 | |
| 1147 | inline bool _isBCP47Extension(std::string_view p) { |
| 1148 | return p.size() >= 3 && |
| 1149 | p[0] == '-' && |
| 1150 | (p[1] == 't' || p[1] == 'T' || |
| 1151 | p[1] == 'u' || p[1] == 'U' || |
| 1152 | p[1] == 'x' || p[1] == 'X') && |
| 1153 | p[2] == '-'; |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * Lookup 'key' in the array 'list'. The array 'list' should contain |
| 1158 | * a nullptr entry, followed by more entries, and a second nullptr entry. |
| 1159 | * |
| 1160 | * The 'list' param should be LANGUAGES, LANGUAGES_3, COUNTRIES, or |
| 1161 | * COUNTRIES_3. |
| 1162 | */ |
| 1163 | std::optional<int16_t> _findIndex(const char* const* list, const char* key) |
| 1164 | { |
| 1165 | const char* const* anchor = list; |
| 1166 | int32_t pass = 0; |
| 1167 | |
| 1168 | /* Make two passes through two nullptr-terminated arrays at 'list' */ |
| 1169 | while (pass++ < 2) { |
| 1170 | while (*list) { |
| 1171 | if (uprv_strcmp(key, *list) == 0) { |
| 1172 | return static_cast<int16_t>(list - anchor); |
| 1173 | } |
| 1174 | list++; |
| 1175 | } |
| 1176 | ++list; /* skip final nullptr *CWB*/ |
| 1177 | } |
| 1178 | return std::nullopt; |
| 1179 | } |
| 1180 | |
| 1181 | } // namespace |
| 1182 | |
| 1183 | U_CFUNC const char* |
| 1184 | uloc_getCurrentCountryID(const char* oldID){ |
| 1185 | std::optional<int16_t> offset = _findIndex(DEPRECATED_COUNTRIES, oldID); |
| 1186 | return offset.has_value() ? REPLACEMENT_COUNTRIES[*offset] : oldID; |
| 1187 | } |
| 1188 | U_CFUNC const char* |
| 1189 | uloc_getCurrentLanguageID(const char* oldID){ |
| 1190 | std::optional<int16_t> offset = _findIndex(DEPRECATED_LANGUAGES, oldID); |
| 1191 | return offset.has_value() ? REPLACEMENT_LANGUAGES[*offset] : oldID; |
| 1192 | } |
| 1193 | |
| 1194 | namespace { |
| 1195 | |
| 1196 | /* |
| 1197 | * the internal functions _getLanguage(), _getScript(), _getRegion(), _getVariant() |
| 1198 | * avoid duplicating code to handle the earlier locale ID pieces |
| 1199 | * in the functions for the later ones by |
| 1200 | * setting the *pEnd pointer to where they stopped parsing |
| 1201 | * |
| 1202 | * TODO try to use this in Locale |
| 1203 | */ |
| 1204 | |
| 1205 | size_t _getLanguage(std::string_view localeID, ByteSink* sink, UErrorCode& status) { |
| 1206 | size_t skip = 0; |
| 1207 | if (localeID.size() == 4 && uprv_strnicmp(localeID.data(), "root", 4) == 0) { |
| 1208 | skip = 4; |
| 1209 | localeID.remove_prefix(skip); |
| 1210 | } else if (localeID.size() >= 3 && uprv_strnicmp(localeID.data(), "und", 3) == 0 && |
| 1211 | (localeID.size() == 3 || |
| 1212 | localeID[3] == '-' || |
| 1213 | localeID[3] == '_' || |
| 1214 | localeID[3] == '@')) { |
| 1215 | skip = 3; |
| 1216 | localeID.remove_prefix(skip); |
| 1217 | } |
| 1218 | |
| 1219 | constexpr int32_t MAXLEN = ULOC_LANG_CAPACITY - 1; // Minus NUL. |
| 1220 | |
| 1221 | /* if it starts with i- or x- then copy that prefix */ |
| 1222 | size_t len = _isIDPrefix(localeID) ? 2 : 0; |
| 1223 | while (len < localeID.size() && !_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len])) { |
| 1224 | if (len == MAXLEN) { |
| 1225 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1226 | return 0; |
| 1227 | } |
| 1228 | len++; |
| 1229 | } |
| 1230 | |
| 1231 | if (sink == nullptr || len == 0) { return skip + len; } |
| 1232 | |
| 1233 | int32_t minCapacity = uprv_max(static_cast<int32_t>(len), 4); // Minimum 3 letters plus NUL. |
| 1234 | char scratch[MAXLEN]; |
| 1235 | int32_t capacity = 0; |
| 1236 | char* buffer = sink->GetAppendBuffer( |
| 1237 | minCapacity, minCapacity, scratch, UPRV_LENGTHOF(scratch), &capacity); |
| 1238 | |
| 1239 | for (size_t i = 0; i < len; ++i) { |
| 1240 | buffer[i] = uprv_tolower(localeID[i]); |
| 1241 | } |
| 1242 | if (localeID.size() >= 2 && _isIDSeparator(localeID[1])) { |
| 1243 | buffer[1] = '-'; |
| 1244 | } |
| 1245 | |
| 1246 | if (len == 3) { |
| 1247 | /* convert 3 character code to 2 character code if possible *CWB*/ |
| 1248 | U_ASSERT(capacity >= 4); |
| 1249 | buffer[3] = '\0'; |
| 1250 | std::optional<int16_t> offset = _findIndex(LANGUAGES_3, buffer); |
| 1251 | if (offset.has_value()) { |
| 1252 | const char* const alias = LANGUAGES[*offset]; |
| 1253 | sink->Append(alias, static_cast<int32_t>(uprv_strlen(alias))); |
| 1254 | return skip + len; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | sink->Append(buffer, static_cast<int32_t>(len)); |
| 1259 | return skip + len; |
| 1260 | } |
| 1261 | |
| 1262 | size_t _getScript(std::string_view localeID, ByteSink* sink) { |
| 1263 | constexpr int32_t LENGTH = 4; |
| 1264 | |
| 1265 | size_t len = 0; |
| 1266 | while (len < localeID.size() && !_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len]) && |
| 1267 | uprv_isASCIILetter(localeID[len])) { |
| 1268 | if (len == LENGTH) { return 0; } |
| 1269 | len++; |
| 1270 | } |
| 1271 | if (len != LENGTH) { return 0; } |
| 1272 | |
| 1273 | if (sink == nullptr) { return len; } |
| 1274 | |
| 1275 | char scratch[LENGTH]; |
| 1276 | int32_t capacity = 0; |
| 1277 | char* buffer = sink->GetAppendBuffer( |
| 1278 | LENGTH, LENGTH, scratch, UPRV_LENGTHOF(scratch), &capacity); |
| 1279 | |
| 1280 | buffer[0] = uprv_toupper(localeID[0]); |
| 1281 | for (int32_t i = 1; i < LENGTH; ++i) { |
| 1282 | buffer[i] = uprv_tolower(localeID[i]); |
| 1283 | } |
| 1284 | |
| 1285 | sink->Append(buffer, LENGTH); |
| 1286 | return len; |
| 1287 | } |
| 1288 | |
| 1289 | size_t _getRegion(std::string_view localeID, ByteSink* sink) { |
| 1290 | constexpr int32_t MINLEN = 2; |
| 1291 | constexpr int32_t MAXLEN = ULOC_COUNTRY_CAPACITY - 1; // Minus NUL. |
| 1292 | |
| 1293 | size_t len = 0; |
| 1294 | while (len < localeID.size() && !_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len])) { |
| 1295 | if (len == MAXLEN) { return 0; } |
| 1296 | len++; |
| 1297 | } |
| 1298 | if (len < MINLEN) { return 0; } |
| 1299 | |
| 1300 | if (sink == nullptr) { return len; } |
| 1301 | |
| 1302 | char scratch[ULOC_COUNTRY_CAPACITY]; |
| 1303 | int32_t capacity = 0; |
| 1304 | char* buffer = sink->GetAppendBuffer( |
| 1305 | ULOC_COUNTRY_CAPACITY, |
| 1306 | ULOC_COUNTRY_CAPACITY, |
| 1307 | scratch, |
| 1308 | UPRV_LENGTHOF(scratch), |
| 1309 | &capacity); |
| 1310 | |
| 1311 | for (size_t i = 0; i < len; ++i) { |
| 1312 | buffer[i] = uprv_toupper(localeID[i]); |
| 1313 | } |
| 1314 | |
| 1315 | if (len == 3) { |
| 1316 | /* convert 3 character code to 2 character code if possible *CWB*/ |
| 1317 | U_ASSERT(capacity >= 4); |
| 1318 | buffer[3] = '\0'; |
| 1319 | std::optional<int16_t> offset = _findIndex(COUNTRIES_3, buffer); |
| 1320 | if (offset.has_value()) { |
| 1321 | const char* const alias = COUNTRIES[*offset]; |
| 1322 | sink->Append(alias, static_cast<int32_t>(uprv_strlen(alias))); |
| 1323 | return len; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | sink->Append(buffer, static_cast<int32_t>(len)); |
| 1328 | return len; |
| 1329 | } |
| 1330 | |
| 1331 | /** |
| 1332 | * @param needSeparator if true, then add leading '_' if any variants |
| 1333 | * are added to 'variant' |
| 1334 | */ |
| 1335 | size_t |
| 1336 | _getVariant(std::string_view localeID, |
| 1337 | char prev, |
| 1338 | ByteSink* sink, |
| 1339 | bool needSeparator, |
| 1340 | UErrorCode& status) { |
| 1341 | if (U_FAILURE(status) || localeID.empty()) return 0; |
| 1342 | |
| 1343 | // Reasonable upper limit for variants |
| 1344 | // There are no strict limitation of the syntax of variant in the legacy |
| 1345 | // locale format. If the locale is constructed from unicode_locale_id |
| 1346 | // as defined in UTS35, then we know each unicode_variant_subtag |
| 1347 | // could have max length of 8 ((alphanum{5,8} | digit alphanum{3}) |
| 1348 | // 179 would allow 20 unicode_variant_subtag with sep in the |
| 1349 | // unicode_locale_id |
| 1350 | // 8*20 + 1*(20-1) = 179 |
| 1351 | constexpr int32_t MAX_VARIANTS_LENGTH = 179; |
| 1352 | |
| 1353 | /* get one or more variant tags and separate them with '_' */ |
| 1354 | size_t index = 0; |
| 1355 | if (_isIDSeparator(prev)) { |
| 1356 | /* get a variant string after a '-' or '_' */ |
| 1357 | for (std::string_view sub = localeID;;) { |
| 1358 | size_t next = sub.find_first_of(".@_-"); |
| 1359 | // For historical reasons, a trailing separator is included in the variant. |
| 1360 | bool finished = next == std::string_view::npos || next + 1 == sub.length(); |
| 1361 | size_t limit = finished ? sub.length() : next; |
| 1362 | index += limit; |
| 1363 | if (index > MAX_VARIANTS_LENGTH) { |
| 1364 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | if (sink != nullptr) { |
| 1369 | if (needSeparator) { |
| 1370 | sink->Append("_", 1); |
| 1371 | } else { |
| 1372 | needSeparator = true; |
| 1373 | } |
| 1374 | |
| 1375 | int32_t length = static_cast<int32_t>(limit); |
| 1376 | int32_t minCapacity = uprv_min(length, MAX_VARIANTS_LENGTH); |
| 1377 | char scratch[MAX_VARIANTS_LENGTH]; |
| 1378 | int32_t capacity = 0; |
| 1379 | char* buffer = sink->GetAppendBuffer( |
| 1380 | minCapacity, minCapacity, scratch, UPRV_LENGTHOF(scratch), &capacity); |
| 1381 | |
| 1382 | for (size_t i = 0; i < limit; ++i) { |
| 1383 | buffer[i] = uprv_toupper(sub[i]); |
| 1384 | } |
| 1385 | sink->Append(buffer, length); |
| 1386 | } |
| 1387 | |
| 1388 | if (finished) { return index; } |
| 1389 | sub.remove_prefix(next); |
| 1390 | if (_isTerminator(sub.front()) || _isBCP47Extension(sub)) { return index; } |
| 1391 | sub.remove_prefix(1); |
| 1392 | index++; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | size_t skip = 0; |
| 1397 | /* if there is no variant tag after a '-' or '_' then look for '@' */ |
| 1398 | if (prev == '@') { |
| 1399 | /* keep localeID */ |
| 1400 | } else if (const char* p = locale_getKeywordsStart(localeID); p != nullptr) { |
| 1401 | skip = 1 + p - localeID.data(); /* point after the '@' */ |
| 1402 | localeID.remove_prefix(skip); |
| 1403 | } else { |
| 1404 | return 0; |
| 1405 | } |
| 1406 | for (; index < localeID.size() && !_isTerminator(localeID[index]); index++) { |
| 1407 | if (index >= MAX_VARIANTS_LENGTH) { // same as length > MAX_VARIANTS_LENGTH |
| 1408 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1409 | return 0; |
| 1410 | } |
| 1411 | if (needSeparator) { |
| 1412 | if (sink != nullptr) { |
| 1413 | sink->Append("_", 1); |
| 1414 | } |
| 1415 | needSeparator = false; |
| 1416 | } |
| 1417 | if (sink != nullptr) { |
| 1418 | char c = uprv_toupper(localeID[index]); |
| 1419 | if (c == '-' || c == ',') c = '_'; |
| 1420 | sink->Append(&c, 1); |
| 1421 | } |
| 1422 | } |
| 1423 | return skip + index; |
| 1424 | } |
| 1425 | |
| 1426 | } // namespace |
| 1427 | |
| 1428 | U_EXPORT CharString |
| 1429 | ulocimp_getLanguage(std::string_view localeID, UErrorCode& status) { |
| 1430 | return ByteSinkUtil::viaByteSinkToCharString( |
| 1431 | [&](ByteSink& sink, UErrorCode& status) { |
| 1432 | ulocimp_getSubtags( |
| 1433 | localeID, |
| 1434 | &sink, |
| 1435 | nullptr, |
| 1436 | nullptr, |
| 1437 | nullptr, |
| 1438 | nullptr, |
| 1439 | status); |
| 1440 | }, |
| 1441 | status); |
| 1442 | } |
| 1443 | |
| 1444 | U_EXPORT CharString |
| 1445 | ulocimp_getScript(std::string_view localeID, UErrorCode& status) { |
| 1446 | return ByteSinkUtil::viaByteSinkToCharString( |
| 1447 | [&](ByteSink& sink, UErrorCode& status) { |
| 1448 | ulocimp_getSubtags( |
| 1449 | localeID, |
| 1450 | nullptr, |
| 1451 | &sink, |
| 1452 | nullptr, |
| 1453 | nullptr, |
| 1454 | nullptr, |
| 1455 | status); |
| 1456 | }, |
| 1457 | status); |
| 1458 | } |
| 1459 | |
| 1460 | U_EXPORT CharString |
| 1461 | ulocimp_getRegion(std::string_view localeID, UErrorCode& status) { |
| 1462 | return ByteSinkUtil::viaByteSinkToCharString( |
| 1463 | [&](ByteSink& sink, UErrorCode& status) { |
| 1464 | ulocimp_getSubtags( |
| 1465 | localeID, |
| 1466 | nullptr, |
| 1467 | nullptr, |
| 1468 | &sink, |
| 1469 | nullptr, |
| 1470 | nullptr, |
| 1471 | status); |
| 1472 | }, |
| 1473 | status); |
| 1474 | } |
| 1475 | |
| 1476 | U_EXPORT CharString |
| 1477 | ulocimp_getVariant(std::string_view localeID, UErrorCode& status) { |
| 1478 | return ByteSinkUtil::viaByteSinkToCharString( |
| 1479 | [&](ByteSink& sink, UErrorCode& status) { |
| 1480 | ulocimp_getSubtags( |
| 1481 | localeID, |
| 1482 | nullptr, |
| 1483 | nullptr, |
| 1484 | nullptr, |
| 1485 | &sink, |
| 1486 | nullptr, |
| 1487 | status); |
| 1488 | }, |
| 1489 | status); |
| 1490 | } |
| 1491 | |
| 1492 | U_EXPORT void |
| 1493 | ulocimp_getSubtags( |
| 1494 | std::string_view localeID, |
| 1495 | CharString* language, |
| 1496 | CharString* script, |
| 1497 | CharString* region, |
| 1498 | CharString* variant, |
| 1499 | const char** pEnd, |
| 1500 | UErrorCode& status) { |
| 1501 | if (U_FAILURE(status)) { return; } |
| 1502 | |
| 1503 | std::optional<CharStringByteSink> languageSink; |
| 1504 | std::optional<CharStringByteSink> scriptSink; |
| 1505 | std::optional<CharStringByteSink> regionSink; |
| 1506 | std::optional<CharStringByteSink> variantSink; |
| 1507 | |
| 1508 | if (language != nullptr) { languageSink.emplace(language); } |
| 1509 | if (script != nullptr) { scriptSink.emplace(script); } |
| 1510 | if (region != nullptr) { regionSink.emplace(region); } |
| 1511 | if (variant != nullptr) { variantSink.emplace(variant); } |
| 1512 | |
| 1513 | ulocimp_getSubtags( |
| 1514 | localeID, |
| 1515 | languageSink.has_value() ? &*languageSink : nullptr, |
| 1516 | scriptSink.has_value() ? &*scriptSink : nullptr, |
| 1517 | regionSink.has_value() ? &*regionSink : nullptr, |
| 1518 | variantSink.has_value() ? &*variantSink : nullptr, |
| 1519 | pEnd, |
| 1520 | status); |
| 1521 | } |
| 1522 | |
| 1523 | U_EXPORT void |
| 1524 | ulocimp_getSubtags( |
| 1525 | std::string_view localeID, |
| 1526 | ByteSink* language, |
| 1527 | ByteSink* script, |
| 1528 | ByteSink* region, |
| 1529 | ByteSink* variant, |
| 1530 | const char** pEnd, |
| 1531 | UErrorCode& status) { |
| 1532 | if (U_FAILURE(status)) { return; } |
| 1533 | |
| 1534 | if (pEnd != nullptr) { |
| 1535 | *pEnd = localeID.data(); |
| 1536 | } else if (language == nullptr && |
| 1537 | script == nullptr && |
| 1538 | region == nullptr && |
| 1539 | variant == nullptr) { |
| 1540 | return; |
| 1541 | } |
| 1542 | |
| 1543 | if (localeID.empty()) { return; } |
| 1544 | |
| 1545 | bool hasRegion = false; |
| 1546 | |
| 1547 | { |
| 1548 | size_t len = _getLanguage(localeID, language, status); |
| 1549 | if (U_FAILURE(status)) { return; } |
| 1550 | if (len > 0) { |
| 1551 | localeID.remove_prefix(len); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | if (pEnd != nullptr) { |
| 1556 | *pEnd = localeID.data(); |
| 1557 | } else if (script == nullptr && |
| 1558 | region == nullptr && |
| 1559 | variant == nullptr) { |
| 1560 | return; |
| 1561 | } |
| 1562 | |
| 1563 | if (localeID.empty()) { return; } |
| 1564 | |
| 1565 | if (_isIDSeparator(localeID.front())) { |
| 1566 | std::string_view sub = localeID; |
| 1567 | sub.remove_prefix(1); |
| 1568 | size_t len = _getScript(sub, script); |
| 1569 | if (len > 0) { |
| 1570 | localeID.remove_prefix(len + 1); |
| 1571 | if (pEnd != nullptr) { *pEnd = localeID.data(); } |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | if ((region == nullptr && variant == nullptr && pEnd == nullptr) || localeID.empty()) { return; } |
| 1576 | |
| 1577 | if (_isIDSeparator(localeID.front())) { |
| 1578 | std::string_view sub = localeID; |
| 1579 | sub.remove_prefix(1); |
| 1580 | size_t len = _getRegion(sub, region); |
| 1581 | if (len > 0) { |
| 1582 | hasRegion = true; |
| 1583 | localeID.remove_prefix(len + 1); |
| 1584 | if (pEnd != nullptr) { *pEnd = localeID.data(); } |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | if ((variant == nullptr && pEnd == nullptr) || localeID.empty()) { return; } |
| 1589 | |
| 1590 | bool hasVariant = false; |
| 1591 | |
| 1592 | if (_isIDSeparator(localeID.front()) && !_isBCP47Extension(localeID)) { |
| 1593 | std::string_view sub = localeID; |
| 1594 | /* If there was no country ID, skip a possible extra IDSeparator */ |
| 1595 | size_t skip = !hasRegion && localeID.size() > 1 && _isIDSeparator(localeID[1]) ? 2 : 1; |
| 1596 | sub.remove_prefix(skip); |
| 1597 | size_t len = _getVariant(sub, localeID[0], variant, false, status); |
| 1598 | if (U_FAILURE(status)) { return; } |
| 1599 | if (len > 0) { |
| 1600 | hasVariant = true; |
| 1601 | localeID.remove_prefix(skip + len); |
| 1602 | if (pEnd != nullptr) { *pEnd = localeID.data(); } |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | if ((variant == nullptr && pEnd == nullptr) || localeID.empty()) { return; } |
| 1607 | |
| 1608 | if (_isBCP47Extension(localeID)) { |
| 1609 | localeID.remove_prefix(2); |
| 1610 | constexpr char vaposix[] = "-va-posix"; |
| 1611 | constexpr size_t length = sizeof vaposix - 1; |
| 1612 | for (size_t next;; localeID.remove_prefix(next)) { |
| 1613 | next = localeID.find('-', 1); |
| 1614 | if (next == std::string_view::npos) { break; } |
| 1615 | next = localeID.find('-', next + 1); |
| 1616 | bool finished = next == std::string_view::npos; |
| 1617 | std::string_view sub = localeID; |
| 1618 | if (!finished) { sub.remove_suffix(sub.length() - next); } |
| 1619 | |
| 1620 | if (sub.length() == length && uprv_strnicmp(sub.data(), vaposix, length) == 0) { |
| 1621 | if (variant != nullptr) { |
| 1622 | if (hasVariant) { variant->Append("_", 1); } |
| 1623 | constexpr char posix[] = "POSIX"; |
| 1624 | variant->Append(posix, sizeof posix - 1); |
| 1625 | } |
| 1626 | if (pEnd != nullptr) { *pEnd = localeID.data() + length; } |
| 1627 | } |
| 1628 | |
| 1629 | if (finished) { break; } |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | /* Keyword enumeration */ |
| 1635 | |
| 1636 | typedef struct UKeywordsContext { |
| 1637 | char* keywords; |
| 1638 | char* current; |
| 1639 | } UKeywordsContext; |
| 1640 | |
| 1641 | U_CDECL_BEGIN |
| 1642 | |
| 1643 | static void U_CALLCONV |
| 1644 | uloc_kw_closeKeywords(UEnumeration *enumerator) { |
| 1645 | uprv_free(((UKeywordsContext *)enumerator->context)->keywords); |
| 1646 | uprv_free(enumerator->context); |
| 1647 | uprv_free(enumerator); |
| 1648 | } |
| 1649 | |
| 1650 | static int32_t U_CALLCONV |
| 1651 | uloc_kw_countKeywords(UEnumeration *en, UErrorCode * /*status*/) { |
| 1652 | char *kw = ((UKeywordsContext *)en->context)->keywords; |
| 1653 | int32_t result = 0; |
| 1654 | while(*kw) { |
| 1655 | result++; |
| 1656 | kw += uprv_strlen(kw)+1; |
| 1657 | } |
| 1658 | return result; |
| 1659 | } |
| 1660 | |
| 1661 | static const char * U_CALLCONV |
| 1662 | uloc_kw_nextKeyword(UEnumeration* en, |
| 1663 | int32_t* resultLength, |
| 1664 | UErrorCode* /*status*/) { |
| 1665 | const char* result = ((UKeywordsContext *)en->context)->current; |
| 1666 | int32_t len = 0; |
| 1667 | if(*result) { |
| 1668 | len = (int32_t)uprv_strlen(((UKeywordsContext *)en->context)->current); |
| 1669 | ((UKeywordsContext *)en->context)->current += len+1; |
| 1670 | } else { |
| 1671 | result = nullptr; |
| 1672 | } |
| 1673 | if (resultLength) { |
| 1674 | *resultLength = len; |
| 1675 | } |
| 1676 | return result; |
| 1677 | } |
| 1678 | |
| 1679 | static void U_CALLCONV |
| 1680 | uloc_kw_resetKeywords(UEnumeration* en, |
| 1681 | UErrorCode* /*status*/) { |
| 1682 | ((UKeywordsContext *)en->context)->current = ((UKeywordsContext *)en->context)->keywords; |
| 1683 | } |
| 1684 | |
| 1685 | U_CDECL_END |
| 1686 | |
| 1687 | |
| 1688 | static const UEnumeration gKeywordsEnum = { |
| 1689 | nullptr, |
| 1690 | nullptr, |
| 1691 | uloc_kw_closeKeywords, |
| 1692 | uloc_kw_countKeywords, |
| 1693 | uenum_unextDefault, |
| 1694 | uloc_kw_nextKeyword, |
| 1695 | uloc_kw_resetKeywords |
| 1696 | }; |
| 1697 | |
| 1698 | U_CAPI UEnumeration* U_EXPORT2 |
| 1699 | uloc_openKeywordList(const char *keywordList, int32_t keywordListSize, UErrorCode* status) |
| 1700 | { |
| 1701 | if (U_FAILURE(*status)) { return nullptr; } |
| 1702 | |
| 1703 | LocalMemory<UKeywordsContext> myContext; |
| 1704 | LocalMemory<UEnumeration> result; |
| 1705 | |
| 1706 | myContext.adoptInstead(static_cast<UKeywordsContext *>(uprv_malloc(sizeof(UKeywordsContext)))); |
| 1707 | result.adoptInstead(static_cast<UEnumeration *>(uprv_malloc(sizeof(UEnumeration)))); |
| 1708 | if (myContext.isNull() || result.isNull()) { |
| 1709 | *status = U_MEMORY_ALLOCATION_ERROR; |
| 1710 | return nullptr; |
| 1711 | } |
| 1712 | uprv_memcpy(result.getAlias(), &gKeywordsEnum, sizeof(UEnumeration)); |
| 1713 | myContext->keywords = static_cast<char *>(uprv_malloc(keywordListSize+1)); |
| 1714 | if (myContext->keywords == nullptr) { |
| 1715 | *status = U_MEMORY_ALLOCATION_ERROR; |
| 1716 | return nullptr; |
| 1717 | } |
| 1718 | uprv_memcpy(myContext->keywords, keywordList, keywordListSize); |
| 1719 | myContext->keywords[keywordListSize] = 0; |
| 1720 | myContext->current = myContext->keywords; |
| 1721 | result->context = myContext.orphan(); |
| 1722 | return result.orphan(); |
| 1723 | } |
| 1724 | |
| 1725 | U_CAPI UEnumeration* U_EXPORT2 |
| 1726 | uloc_openKeywords(const char* localeID, |
| 1727 | UErrorCode* status) |
| 1728 | { |
| 1729 | if(status==nullptr || U_FAILURE(*status)) { |
| 1730 | return nullptr; |
| 1731 | } |
| 1732 | |
| 1733 | CharString tempBuffer; |
| 1734 | const char* tmpLocaleID; |
| 1735 | |
| 1736 | if (localeID != nullptr && _hasBCP47Extension(localeID)) { |
| 1737 | tempBuffer = ulocimp_forLanguageTag(localeID, -1, nullptr, *status); |
| 1738 | tmpLocaleID = U_SUCCESS(*status) && !tempBuffer.isEmpty() ? tempBuffer.data() : localeID; |
| 1739 | } else { |
| 1740 | if (localeID==nullptr) { |
| 1741 | localeID=uloc_getDefault(); |
| 1742 | } |
| 1743 | tmpLocaleID=localeID; |
| 1744 | } |
| 1745 | |
| 1746 | ulocimp_getSubtags( |
| 1747 | tmpLocaleID, |
| 1748 | nullptr, |
| 1749 | nullptr, |
| 1750 | nullptr, |
| 1751 | nullptr, |
| 1752 | &tmpLocaleID, |
| 1753 | *status); |
| 1754 | if (U_FAILURE(*status)) { |
| 1755 | return nullptr; |
| 1756 | } |
| 1757 | |
| 1758 | /* keywords are located after '@' */ |
| 1759 | if((tmpLocaleID = locale_getKeywordsStart(tmpLocaleID)) != nullptr) { |
| 1760 | CharString keywords = ulocimp_getKeywords(tmpLocaleID + 1, '@', false, *status); |
| 1761 | if (U_FAILURE(*status)) { |
| 1762 | return nullptr; |
| 1763 | } |
| 1764 | return uloc_openKeywordList(keywords.data(), keywords.length(), status); |
| 1765 | } |
| 1766 | return nullptr; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | /* bit-flags for 'options' parameter of _canonicalize */ |
| 1771 | #define _ULOC_STRIP_KEYWORDS 0x2 |
| 1772 | #define _ULOC_CANONICALIZE 0x1 |
| 1773 | |
| 1774 | namespace { |
| 1775 | |
| 1776 | inline bool OPTION_SET(uint32_t options, uint32_t mask) { return (options & mask) != 0; } |
| 1777 | |
| 1778 | constexpr char i_default[] = {'i', '-', 'd', 'e', 'f', 'a', 'u', 'l', 't'}; |
| 1779 | constexpr int32_t I_DEFAULT_LENGTH = UPRV_LENGTHOF(i_default); |
| 1780 | |
| 1781 | /** |
| 1782 | * Canonicalize the given localeID, to level 1 or to level 2, |
| 1783 | * depending on the options. To specify level 1, pass in options=0. |
| 1784 | * To specify level 2, pass in options=_ULOC_CANONICALIZE. |
| 1785 | * |
| 1786 | * This is the code underlying uloc_getName and uloc_canonicalize. |
| 1787 | */ |
| 1788 | void |
| 1789 | _canonicalize(std::string_view localeID, |
| 1790 | ByteSink& sink, |
| 1791 | uint32_t options, |
| 1792 | UErrorCode& err) { |
| 1793 | if (U_FAILURE(err)) { |
| 1794 | return; |
| 1795 | } |
| 1796 | |
| 1797 | int32_t j, fieldCount=0; |
| 1798 | CharString tempBuffer; // if localeID has a BCP47 extension, tmpLocaleID points to this |
| 1799 | CharString localeIDWithHyphens; // if localeID has a BPC47 extension and have _, tmpLocaleID points to this |
| 1800 | std::string_view origLocaleID; |
| 1801 | std::string_view tmpLocaleID; |
| 1802 | size_t keywordAssign = std::string_view::npos; |
| 1803 | size_t separatorIndicator = std::string_view::npos; |
| 1804 | |
| 1805 | if (_hasBCP47Extension(localeID)) { |
| 1806 | std::string_view localeIDPtr = localeID; |
| 1807 | |
| 1808 | // convert all underbars to hyphens, unless the "BCP47 extension" comes at the beginning of the string |
| 1809 | if (localeID.size() >= 2 && localeID.find('_') != std::string_view::npos && localeID[1] != '-' && localeID[1] != '_') { |
| 1810 | localeIDWithHyphens.append(localeID, err); |
| 1811 | if (U_SUCCESS(err)) { |
| 1812 | for (char* p = localeIDWithHyphens.data(); *p != '\0'; ++p) { |
| 1813 | if (*p == '_') { |
| 1814 | *p = '-'; |
| 1815 | } |
| 1816 | } |
| 1817 | localeIDPtr = localeIDWithHyphens.toStringPiece(); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | tempBuffer = ulocimp_forLanguageTag(localeIDPtr.data(), static_cast<int32_t>(localeIDPtr.size()), nullptr, err); |
| 1822 | tmpLocaleID = U_SUCCESS(err) && !tempBuffer.isEmpty() ? static_cast<std::string_view>(tempBuffer.toStringPiece()) : localeIDPtr; |
| 1823 | } else { |
| 1824 | tmpLocaleID=localeID; |
| 1825 | } |
| 1826 | |
| 1827 | origLocaleID=tmpLocaleID; |
| 1828 | |
| 1829 | /* get all pieces, one after another, and separate with '_' */ |
| 1830 | CharString tag; |
| 1831 | CharString script; |
| 1832 | CharString country; |
| 1833 | CharString variant; |
| 1834 | const char* end = nullptr; |
| 1835 | ulocimp_getSubtags( |
| 1836 | tmpLocaleID, |
| 1837 | &tag, |
| 1838 | &script, |
| 1839 | &country, |
| 1840 | &variant, |
| 1841 | &end, |
| 1842 | err); |
| 1843 | if (U_FAILURE(err)) { |
| 1844 | return; |
| 1845 | } |
| 1846 | U_ASSERT(end != nullptr); |
| 1847 | if (end > tmpLocaleID.data()) { |
| 1848 | tmpLocaleID.remove_prefix(end - tmpLocaleID.data()); |
| 1849 | } |
| 1850 | |
| 1851 | if (tag.length() == I_DEFAULT_LENGTH && origLocaleID.length() >= I_DEFAULT_LENGTH && |
| 1852 | uprv_strncmp(origLocaleID.data(), i_default, I_DEFAULT_LENGTH) == 0) { |
| 1853 | tag.clear(); |
| 1854 | tag.append(uloc_getDefault(), err); |
| 1855 | } else { |
| 1856 | if (!script.isEmpty()) { |
| 1857 | ++fieldCount; |
| 1858 | tag.append('_', err); |
| 1859 | tag.append(script, err); |
| 1860 | } |
| 1861 | if (!country.isEmpty()) { |
| 1862 | ++fieldCount; |
| 1863 | tag.append('_', err); |
| 1864 | tag.append(country, err); |
| 1865 | } |
| 1866 | if (!variant.isEmpty()) { |
| 1867 | ++fieldCount; |
| 1868 | if (country.isEmpty()) { |
| 1869 | tag.append('_', err); |
| 1870 | } |
| 1871 | tag.append('_', err); |
| 1872 | tag.append(variant, err); |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | /* Copy POSIX-style charset specifier, if any [mr.utf8] */ |
| 1877 | if (!OPTION_SET(options, _ULOC_CANONICALIZE) && !tmpLocaleID.empty() && tmpLocaleID.front() == '.') { |
| 1878 | tag.append('.', err); |
| 1879 | tmpLocaleID.remove_prefix(1); |
| 1880 | size_t length; |
| 1881 | if (size_t atPos = tmpLocaleID.find('@'); atPos != std::string_view::npos) { |
| 1882 | length = atPos; |
| 1883 | } else { |
| 1884 | length = tmpLocaleID.length(); |
| 1885 | } |
| 1886 | // The longest charset name we found in IANA charset registry |
| 1887 | // https://www.iana.org/assignments/character-sets/ is |
| 1888 | // "Extended_UNIX_Code_Packed_Format_for_Japanese" in length 45. |
| 1889 | // we therefore restrict the length here to be 64 which is a power of 2 |
| 1890 | // number that is longer than 45. |
| 1891 | constexpr size_t kMaxCharsetLength = 64; |
| 1892 | if (length > kMaxCharsetLength) { |
| 1893 | err = U_ILLEGAL_ARGUMENT_ERROR; /* malformed keyword name */ |
| 1894 | return; |
| 1895 | } |
| 1896 | if (length > 0) { |
| 1897 | tag.append(tmpLocaleID.data(), static_cast<int32_t>(length), err); |
| 1898 | tmpLocaleID.remove_prefix(length); |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | /* Scan ahead to next '@' and determine if it is followed by '=' and/or ';' |
| 1903 | After this, tmpLocaleID either starts at '@' or is empty. */ |
| 1904 | if (const char* start = locale_getKeywordsStart(tmpLocaleID); start != nullptr) { |
| 1905 | if (start > tmpLocaleID.data()) { |
| 1906 | tmpLocaleID.remove_prefix(start - tmpLocaleID.data()); |
| 1907 | } |
| 1908 | keywordAssign = tmpLocaleID.find('='); |
| 1909 | separatorIndicator = tmpLocaleID.find(';'); |
| 1910 | } else { |
| 1911 | tmpLocaleID = {}; |
| 1912 | } |
| 1913 | |
| 1914 | /* Copy POSIX-style variant, if any [mr@FOO] */ |
| 1915 | if (!OPTION_SET(options, _ULOC_CANONICALIZE) && |
| 1916 | !tmpLocaleID.empty() && keywordAssign == std::string_view::npos) { |
| 1917 | tag.append(tmpLocaleID, err); |
| 1918 | tmpLocaleID = {}; |
| 1919 | } |
| 1920 | |
| 1921 | if (OPTION_SET(options, _ULOC_CANONICALIZE)) { |
| 1922 | /* Handle @FOO variant if @ is present and not followed by = */ |
| 1923 | if (!tmpLocaleID.empty() && keywordAssign == std::string_view::npos) { |
| 1924 | /* Add missing '_' if needed */ |
| 1925 | if (fieldCount < 2 || (fieldCount < 3 && !script.isEmpty())) { |
| 1926 | do { |
| 1927 | tag.append('_', err); |
| 1928 | ++fieldCount; |
| 1929 | } while(fieldCount<2); |
| 1930 | } |
| 1931 | |
| 1932 | CharStringByteSink s(&tag); |
| 1933 | std::string_view sub = tmpLocaleID; |
| 1934 | sub.remove_prefix(1); |
| 1935 | _getVariant(sub, '@', &s, !variant.isEmpty(), err); |
| 1936 | if (U_FAILURE(err)) { return; } |
| 1937 | } |
| 1938 | |
| 1939 | /* Look up the ID in the canonicalization map */ |
| 1940 | for (j=0; j<UPRV_LENGTHOF(CANONICALIZE_MAP); j++) { |
| 1941 | StringPiece id(CANONICALIZE_MAP[j].id); |
| 1942 | if (tag == id) { |
| 1943 | if (id.empty() && !tmpLocaleID.empty()) { |
| 1944 | break; /* Don't remap "" if keywords present */ |
| 1945 | } |
| 1946 | tag.clear(); |
| 1947 | tag.append(CANONICALIZE_MAP[j].canonicalID, err); |
| 1948 | break; |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | sink.Append(tag.data(), tag.length()); |
| 1954 | |
| 1955 | if (!OPTION_SET(options, _ULOC_STRIP_KEYWORDS)) { |
| 1956 | if (!tmpLocaleID.empty() && keywordAssign != std::string_view::npos && |
| 1957 | (separatorIndicator == std::string_view::npos || separatorIndicator > keywordAssign)) { |
| 1958 | sink.Append("@", 1); |
| 1959 | ++fieldCount; |
| 1960 | tmpLocaleID.remove_prefix(1); |
| 1961 | ulocimp_getKeywords(tmpLocaleID, '@', sink, true, err); |
| 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | } // namespace |
| 1967 | |
| 1968 | /* ### ID parsing API **************************************************/ |
| 1969 | |
| 1970 | U_CAPI int32_t U_EXPORT2 |
| 1971 | uloc_getParent(const char* localeID, |
| 1972 | char* parent, |
| 1973 | int32_t parentCapacity, |
| 1974 | UErrorCode* err) |
| 1975 | { |
| 1976 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 1977 | parent, parentCapacity, |
| 1978 | [&](ByteSink& sink, UErrorCode& status) { |
| 1979 | ulocimp_getParent(localeID, sink, status); |
| 1980 | }, |
| 1981 | *err); |
| 1982 | } |
| 1983 | |
| 1984 | U_EXPORT CharString |
| 1985 | ulocimp_getParent(const char* localeID, |
| 1986 | UErrorCode& err) |
| 1987 | { |
| 1988 | return ByteSinkUtil::viaByteSinkToCharString( |
| 1989 | [&](ByteSink& sink, UErrorCode& status) { |
| 1990 | ulocimp_getParent(localeID, sink, status); |
| 1991 | }, |
| 1992 | err); |
| 1993 | } |
| 1994 | |
| 1995 | U_EXPORT void |
| 1996 | ulocimp_getParent(const char* localeID, |
| 1997 | icu::ByteSink& sink, |
| 1998 | UErrorCode& err) |
| 1999 | { |
| 2000 | if (U_FAILURE(err)) { return; } |
| 2001 | |
| 2002 | const char *lastUnderscore; |
| 2003 | int32_t i; |
| 2004 | |
| 2005 | if (localeID == nullptr) |
| 2006 | localeID = uloc_getDefault(); |
| 2007 | |
| 2008 | lastUnderscore=uprv_strrchr(localeID, '_'); |
| 2009 | if(lastUnderscore!=nullptr) { |
| 2010 | i = static_cast<int32_t>(lastUnderscore - localeID); |
| 2011 | } else { |
| 2012 | i=0; |
| 2013 | } |
| 2014 | |
| 2015 | if (i > 0) { |
| 2016 | if (uprv_strnicmp(localeID, "und_", 4) == 0) { |
| 2017 | localeID += 3; |
| 2018 | i -= 3; |
| 2019 | } |
| 2020 | sink.Append(localeID, i); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | U_CAPI int32_t U_EXPORT2 |
| 2025 | uloc_getLanguage(const char* localeID, |
| 2026 | char* language, |
| 2027 | int32_t languageCapacity, |
| 2028 | UErrorCode* err) |
| 2029 | { |
| 2030 | if (localeID == nullptr) { |
| 2031 | localeID = uloc_getDefault(); |
| 2032 | } |
| 2033 | |
| 2034 | /* uloc_getLanguage will return a 2 character iso-639 code if one exists. *CWB*/ |
| 2035 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 2036 | language, languageCapacity, |
| 2037 | [&](ByteSink& sink, UErrorCode& status) { |
| 2038 | ulocimp_getSubtags( |
| 2039 | localeID, |
| 2040 | &sink, |
| 2041 | nullptr, |
| 2042 | nullptr, |
| 2043 | nullptr, |
| 2044 | nullptr, |
| 2045 | status); |
| 2046 | }, |
| 2047 | *err); |
| 2048 | } |
| 2049 | |
| 2050 | U_CAPI int32_t U_EXPORT2 |
| 2051 | uloc_getScript(const char* localeID, |
| 2052 | char* script, |
| 2053 | int32_t scriptCapacity, |
| 2054 | UErrorCode* err) |
| 2055 | { |
| 2056 | if (localeID == nullptr) { |
| 2057 | localeID = uloc_getDefault(); |
| 2058 | } |
| 2059 | |
| 2060 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 2061 | script, scriptCapacity, |
| 2062 | [&](ByteSink& sink, UErrorCode& status) { |
| 2063 | ulocimp_getSubtags( |
| 2064 | localeID, |
| 2065 | nullptr, |
| 2066 | &sink, |
| 2067 | nullptr, |
| 2068 | nullptr, |
| 2069 | nullptr, |
| 2070 | status); |
| 2071 | }, |
| 2072 | *err); |
| 2073 | } |
| 2074 | |
| 2075 | U_CAPI int32_t U_EXPORT2 |
| 2076 | uloc_getCountry(const char* localeID, |
| 2077 | char* country, |
| 2078 | int32_t countryCapacity, |
| 2079 | UErrorCode* err) |
| 2080 | { |
| 2081 | if (localeID == nullptr) { |
| 2082 | localeID = uloc_getDefault(); |
| 2083 | } |
| 2084 | |
| 2085 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 2086 | country, countryCapacity, |
| 2087 | [&](ByteSink& sink, UErrorCode& status) { |
| 2088 | ulocimp_getSubtags( |
| 2089 | localeID, |
| 2090 | nullptr, |
| 2091 | nullptr, |
| 2092 | &sink, |
| 2093 | nullptr, |
| 2094 | nullptr, |
| 2095 | status); |
| 2096 | }, |
| 2097 | *err); |
| 2098 | } |
| 2099 | |
| 2100 | U_CAPI int32_t U_EXPORT2 |
| 2101 | uloc_getVariant(const char* localeID, |
| 2102 | char* variant, |
| 2103 | int32_t variantCapacity, |
| 2104 | UErrorCode* err) |
| 2105 | { |
| 2106 | if (localeID == nullptr) { |
| 2107 | localeID = uloc_getDefault(); |
| 2108 | } |
| 2109 | |
| 2110 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 2111 | variant, variantCapacity, |
| 2112 | [&](ByteSink& sink, UErrorCode& status) { |
| 2113 | ulocimp_getSubtags( |
| 2114 | localeID, |
| 2115 | nullptr, |
| 2116 | nullptr, |
| 2117 | nullptr, |
| 2118 | &sink, |
| 2119 | nullptr, |
| 2120 | status); |
| 2121 | }, |
| 2122 | *err); |
| 2123 | } |
| 2124 | |
| 2125 | U_CAPI int32_t U_EXPORT2 |
| 2126 | uloc_getName(const char* localeID, |
| 2127 | char* name, |
| 2128 | int32_t nameCapacity, |
| 2129 | UErrorCode* err) |
| 2130 | { |
| 2131 | if (localeID == nullptr) { |
| 2132 | localeID = uloc_getDefault(); |
| 2133 | } |
| 2134 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 2135 | name, nameCapacity, |
| 2136 | [&](ByteSink& sink, UErrorCode& status) { |
| 2137 | ulocimp_getName(localeID, sink, status); |
| 2138 | }, |
| 2139 | *err); |
| 2140 | } |
| 2141 | |
| 2142 | U_EXPORT CharString |
| 2143 | ulocimp_getName(std::string_view localeID, |
| 2144 | UErrorCode& err) |
| 2145 | { |
| 2146 | return ByteSinkUtil::viaByteSinkToCharString( |
| 2147 | [&](ByteSink& sink, UErrorCode& status) { |
| 2148 | ulocimp_getName(localeID, sink, status); |
| 2149 | }, |
| 2150 | err); |
| 2151 | } |
| 2152 | |
| 2153 | U_EXPORT void |
| 2154 | ulocimp_getName(std::string_view localeID, |
| 2155 | ByteSink& sink, |
| 2156 | UErrorCode& err) |
| 2157 | { |
| 2158 | _canonicalize(localeID, sink, 0, err); |
| 2159 | } |
| 2160 | |
| 2161 | U_CAPI int32_t U_EXPORT2 |
| 2162 | uloc_getBaseName(const char* localeID, |
| 2163 | char* name, |
| 2164 | int32_t nameCapacity, |
| 2165 | UErrorCode* err) |
| 2166 | { |
| 2167 | if (localeID == nullptr) { |
| 2168 | localeID = uloc_getDefault(); |
| 2169 | } |
| 2170 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 2171 | name, nameCapacity, |
| 2172 | [&](ByteSink& sink, UErrorCode& status) { |
| 2173 | ulocimp_getBaseName(localeID, sink, status); |
| 2174 | }, |
| 2175 | *err); |
| 2176 | } |
| 2177 | |
| 2178 | U_EXPORT CharString |
| 2179 | ulocimp_getBaseName(std::string_view localeID, |
| 2180 | UErrorCode& err) |
| 2181 | { |
| 2182 | return ByteSinkUtil::viaByteSinkToCharString( |
| 2183 | [&](ByteSink& sink, UErrorCode& status) { |
| 2184 | ulocimp_getBaseName(localeID, sink, status); |
| 2185 | }, |
| 2186 | err); |
| 2187 | } |
| 2188 | |
| 2189 | U_EXPORT void |
| 2190 | ulocimp_getBaseName(std::string_view localeID, |
| 2191 | ByteSink& sink, |
| 2192 | UErrorCode& err) |
| 2193 | { |
| 2194 | _canonicalize(localeID, sink, _ULOC_STRIP_KEYWORDS, err); |
| 2195 | } |
| 2196 | |
| 2197 | U_CAPI int32_t U_EXPORT2 |
| 2198 | uloc_canonicalize(const char* localeID, |
| 2199 | char* name, |
| 2200 | int32_t nameCapacity, |
| 2201 | UErrorCode* err) |
| 2202 | { |
| 2203 | if (localeID == nullptr) { |
| 2204 | localeID = uloc_getDefault(); |
| 2205 | } |
| 2206 | return ByteSinkUtil::viaByteSinkToTerminatedChars( |
| 2207 | name, nameCapacity, |
| 2208 | [&](ByteSink& sink, UErrorCode& status) { |
| 2209 | ulocimp_canonicalize(localeID, sink, status); |
| 2210 | }, |
| 2211 | *err); |
| 2212 | } |
| 2213 | |
| 2214 | U_EXPORT CharString |
| 2215 | ulocimp_canonicalize(std::string_view localeID, |
| 2216 | UErrorCode& err) |
| 2217 | { |
| 2218 | return ByteSinkUtil::viaByteSinkToCharString( |
| 2219 | [&](ByteSink& sink, UErrorCode& status) { |
| 2220 | ulocimp_canonicalize(localeID, sink, status); |
| 2221 | }, |
| 2222 | err); |
| 2223 | } |
| 2224 | |
| 2225 | U_EXPORT void |
| 2226 | ulocimp_canonicalize(std::string_view localeID, |
| 2227 | ByteSink& sink, |
| 2228 | UErrorCode& err) |
| 2229 | { |
| 2230 | _canonicalize(localeID, sink, _ULOC_CANONICALIZE, err); |
| 2231 | } |
| 2232 | |
| 2233 | U_CAPI const char* U_EXPORT2 |
| 2234 | uloc_getISO3Language(const char* localeID) |
| 2235 | { |
| 2236 | UErrorCode err = U_ZERO_ERROR; |
| 2237 | |
| 2238 | if (localeID == nullptr) |
| 2239 | { |
| 2240 | localeID = uloc_getDefault(); |
| 2241 | } |
| 2242 | CharString lang = ulocimp_getLanguage(localeID, err); |
| 2243 | if (U_FAILURE(err)) |
| 2244 | return ""; |
| 2245 | std::optional<int16_t> offset = _findIndex(LANGUAGES, lang.data()); |
| 2246 | return offset.has_value() ? LANGUAGES_3[*offset] : ""; |
| 2247 | } |
| 2248 | |
| 2249 | U_CAPI const char* U_EXPORT2 |
| 2250 | uloc_getISO3Country(const char* localeID) |
| 2251 | { |
| 2252 | UErrorCode err = U_ZERO_ERROR; |
| 2253 | |
| 2254 | if (localeID == nullptr) |
| 2255 | { |
| 2256 | localeID = uloc_getDefault(); |
| 2257 | } |
| 2258 | CharString cntry = ulocimp_getRegion(localeID, err); |
| 2259 | if (U_FAILURE(err)) |
| 2260 | return ""; |
| 2261 | std::optional<int16_t> offset = _findIndex(COUNTRIES, cntry.data()); |
| 2262 | return offset.has_value() ? COUNTRIES_3[*offset] : ""; |
| 2263 | } |
| 2264 | |
| 2265 | U_CAPI uint32_t U_EXPORT2 |
| 2266 | uloc_getLCID(const char* localeID) |
| 2267 | { |
| 2268 | UErrorCode status = U_ZERO_ERROR; |
| 2269 | uint32_t lcid = 0; |
| 2270 | |
| 2271 | /* Check for incomplete id. */ |
| 2272 | if (!localeID || uprv_strlen(localeID) < 2) { |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | // First, attempt Windows platform lookup if available, but fall |
| 2277 | // through to catch any special cases (ICU vs Windows name differences). |
| 2278 | lcid = uprv_convertToLCIDPlatform(localeID, &status); |
| 2279 | if (U_FAILURE(status)) { |
| 2280 | return 0; |
| 2281 | } |
| 2282 | if (lcid > 0) { |
| 2283 | // Windows found an LCID, return that |
| 2284 | return lcid; |
| 2285 | } |
| 2286 | |
| 2287 | CharString langID = ulocimp_getLanguage(localeID, status); |
| 2288 | if (U_FAILURE(status)) { |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
| 2292 | if (uprv_strchr(localeID, '@')) { |
| 2293 | // uprv_convertToLCID does not support keywords other than collation. |
| 2294 | // Remove all keywords except collation. |
| 2295 | CharString collVal = ulocimp_getKeywordValue(localeID, "collation", status); |
| 2296 | if (U_SUCCESS(status) && !collVal.isEmpty()) { |
| 2297 | CharString tmpLocaleID = ulocimp_getBaseName(localeID, status); |
| 2298 | ulocimp_setKeywordValue("collation", collVal.toStringPiece(), tmpLocaleID, status); |
| 2299 | if (U_SUCCESS(status)) { |
| 2300 | return uprv_convertToLCID(langID.data(), tmpLocaleID.data(), &status); |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | // fall through - all keywords are simply ignored |
| 2305 | status = U_ZERO_ERROR; |
| 2306 | } |
| 2307 | |
| 2308 | return uprv_convertToLCID(langID.data(), localeID, &status); |
| 2309 | } |
| 2310 | |
| 2311 | U_CAPI int32_t U_EXPORT2 |
| 2312 | uloc_getLocaleForLCID(uint32_t hostid, char *locale, int32_t localeCapacity, |
| 2313 | UErrorCode *status) |
| 2314 | { |
| 2315 | return uprv_convertToPosix(hostid, locale, localeCapacity, status); |
| 2316 | } |
| 2317 | |
| 2318 | /* ### Default locale **************************************************/ |
| 2319 | |
| 2320 | U_CAPI const char* U_EXPORT2 |
| 2321 | uloc_getDefault() |
| 2322 | { |
| 2323 | return locale_get_default(); |
| 2324 | } |
| 2325 | |
| 2326 | U_CAPI void U_EXPORT2 |
| 2327 | uloc_setDefault(const char* newDefaultLocale, |
| 2328 | UErrorCode* err) |
| 2329 | { |
| 2330 | if (U_FAILURE(*err)) |
| 2331 | return; |
| 2332 | /* the error code isn't currently used for anything by this function*/ |
| 2333 | |
| 2334 | /* propagate change to C++ */ |
| 2335 | locale_set_default(newDefaultLocale); |
| 2336 | } |
| 2337 | |
| 2338 | /** |
| 2339 | * Returns a list of all 2-letter language codes defined in ISO 639. This is a pointer |
| 2340 | * to an array of pointers to arrays of char. All of these pointers are owned |
| 2341 | * by ICU-- do not delete them, and do not write through them. The array is |
| 2342 | * terminated with a null pointer. |
| 2343 | */ |
| 2344 | U_CAPI const char* const* U_EXPORT2 |
| 2345 | uloc_getISOLanguages() |
| 2346 | { |
| 2347 | return LANGUAGES; |
| 2348 | } |
| 2349 | |
| 2350 | /** |
| 2351 | * Returns a list of all 2-letter country codes defined in ISO 639. This is a |
| 2352 | * pointer to an array of pointers to arrays of char. All of these pointers are |
| 2353 | * owned by ICU-- do not delete them, and do not write through them. The array is |
| 2354 | * terminated with a null pointer. |
| 2355 | */ |
| 2356 | U_CAPI const char* const* U_EXPORT2 |
| 2357 | uloc_getISOCountries() |
| 2358 | { |
| 2359 | return COUNTRIES; |
| 2360 | } |
| 2361 | |
| 2362 | U_CAPI const char* U_EXPORT2 |
| 2363 | uloc_toUnicodeLocaleKey(const char* keyword) |
| 2364 | { |
| 2365 | if (keyword == nullptr || *keyword == '\0') { return nullptr; } |
| 2366 | std::optional<std::string_view> result = ulocimp_toBcpKeyWithFallback(keyword); |
| 2367 | return result.has_value() ? result->data() : nullptr; // Known to be NUL terminated. |
| 2368 | } |
| 2369 | |
| 2370 | U_EXPORT std::optional<std::string_view> |
| 2371 | ulocimp_toBcpKeyWithFallback(std::string_view keyword) |
| 2372 | { |
| 2373 | std::optional<std::string_view> bcpKey = ulocimp_toBcpKey(keyword); |
| 2374 | if (!bcpKey.has_value() && |
| 2375 | ultag_isUnicodeLocaleKey(keyword.data(), static_cast<int32_t>(keyword.size()))) { |
| 2376 | // unknown keyword, but syntax is fine.. |
| 2377 | return keyword; |
| 2378 | } |
| 2379 | return bcpKey; |
| 2380 | } |
| 2381 | |
| 2382 | U_CAPI const char* U_EXPORT2 |
| 2383 | uloc_toUnicodeLocaleType(const char* keyword, const char* value) |
| 2384 | { |
| 2385 | if (keyword == nullptr || *keyword == '\0' || |
| 2386 | value == nullptr || *value == '\0') { return nullptr; } |
| 2387 | std::optional<std::string_view> result = ulocimp_toBcpTypeWithFallback(keyword, value); |
| 2388 | return result.has_value() ? result->data() : nullptr; // Known to be NUL terminated. |
| 2389 | } |
| 2390 | |
| 2391 | U_EXPORT std::optional<std::string_view> |
| 2392 | ulocimp_toBcpTypeWithFallback(std::string_view keyword, std::string_view value) |
| 2393 | { |
| 2394 | std::optional<std::string_view> bcpType = ulocimp_toBcpType(keyword, value); |
| 2395 | if (!bcpType.has_value() && |
| 2396 | ultag_isUnicodeLocaleType(value.data(), static_cast<int32_t>(value.size()))) { |
| 2397 | // unknown keyword, but syntax is fine.. |
| 2398 | return value; |
| 2399 | } |
| 2400 | return bcpType; |
| 2401 | } |
| 2402 | |
| 2403 | namespace { |
| 2404 | |
| 2405 | bool |
| 2406 | isWellFormedLegacyKey(std::string_view key) |
| 2407 | { |
| 2408 | return std::all_of(key.begin(), key.end(), UPRV_ISALPHANUM); |
| 2409 | } |
| 2410 | |
| 2411 | bool |
| 2412 | isWellFormedLegacyType(std::string_view legacyType) |
| 2413 | { |
| 2414 | int32_t alphaNumLen = 0; |
| 2415 | for (char c : legacyType) { |
| 2416 | if (c == '_' || c == '/' || c == '-') { |
| 2417 | if (alphaNumLen == 0) { |
| 2418 | return false; |
| 2419 | } |
| 2420 | alphaNumLen = 0; |
| 2421 | } else if (UPRV_ISALPHANUM(c)) { |
| 2422 | alphaNumLen++; |
| 2423 | } else { |
| 2424 | return false; |
| 2425 | } |
| 2426 | } |
| 2427 | return alphaNumLen != 0; |
| 2428 | } |
| 2429 | |
| 2430 | } // namespace |
| 2431 | |
| 2432 | U_CAPI const char* U_EXPORT2 |
| 2433 | uloc_toLegacyKey(const char* keyword) |
| 2434 | { |
| 2435 | if (keyword == nullptr || *keyword == '\0') { return nullptr; } |
| 2436 | std::optional<std::string_view> result = ulocimp_toLegacyKeyWithFallback(keyword); |
| 2437 | return result.has_value() ? result->data() : nullptr; // Known to be NUL terminated. |
| 2438 | } |
| 2439 | |
| 2440 | U_EXPORT std::optional<std::string_view> |
| 2441 | ulocimp_toLegacyKeyWithFallback(std::string_view keyword) |
| 2442 | { |
| 2443 | std::optional<std::string_view> legacyKey = ulocimp_toLegacyKey(keyword); |
| 2444 | if (!legacyKey.has_value() && isWellFormedLegacyKey(keyword)) { |
| 2445 | // Checks if the specified locale key is well-formed with the legacy locale syntax. |
| 2446 | // |
| 2447 | // Note: |
| 2448 | // LDML/CLDR provides some definition of keyword syntax in |
| 2449 | // * http://www.unicode.org/reports/tr35/#Unicode_locale_identifier and |
| 2450 | // * http://www.unicode.org/reports/tr35/#Old_Locale_Extension_Syntax |
| 2451 | // Keys can only consist of [0-9a-zA-Z]. |
| 2452 | return keyword; |
| 2453 | } |
| 2454 | return legacyKey; |
| 2455 | } |
| 2456 | |
| 2457 | U_CAPI const char* U_EXPORT2 |
| 2458 | uloc_toLegacyType(const char* keyword, const char* value) |
| 2459 | { |
| 2460 | if (keyword == nullptr || *keyword == '\0' || |
| 2461 | value == nullptr || *value == '\0') { return nullptr; } |
| 2462 | std::optional<std::string_view> result = ulocimp_toLegacyTypeWithFallback(keyword, value); |
| 2463 | return result.has_value() ? result->data() : nullptr; // Known to be NUL terminated. |
| 2464 | } |
| 2465 | |
| 2466 | U_EXPORT std::optional<std::string_view> |
| 2467 | ulocimp_toLegacyTypeWithFallback(std::string_view keyword, std::string_view value) |
| 2468 | { |
| 2469 | std::optional<std::string_view> legacyType = ulocimp_toLegacyType(keyword, value); |
| 2470 | if (!legacyType.has_value() && isWellFormedLegacyType(value)) { |
| 2471 | // Checks if the specified locale type is well-formed with the legacy locale syntax. |
| 2472 | // |
| 2473 | // Note: |
| 2474 | // LDML/CLDR provides some definition of keyword syntax in |
| 2475 | // * http://www.unicode.org/reports/tr35/#Unicode_locale_identifier and |
| 2476 | // * http://www.unicode.org/reports/tr35/#Old_Locale_Extension_Syntax |
| 2477 | // Values (types) can only consist of [0-9a-zA-Z], plus for legacy values |
| 2478 | // we allow [/_-+] in the middle (e.g. "Etc/GMT+1", "Asia/Tel_Aviv") |
| 2479 | return value; |
| 2480 | } |
| 2481 | return legacyType; |
| 2482 | } |
| 2483 | |
| 2484 | /*eof*/ |