#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/uloc.h"
#include "putilimp.h"
#include "ustr_imp.h"
#include "ulocimp.h"
#include "umutex.h"
#include "cstring.h"
#include "cmemory.h"
#include "locmap.h"
#include "uarrsort.h"
#include "uenumimp.h"
#include "uassert.h"
#include "charstr.h"
#include <stdio.h>
U_NAMESPACE_USE
U_CFUNC void locale_set_default(const char *id);
U_CFUNC const char *locale_get_default(void);
U_CFUNC int32_t
locale_getKeywords(const char *localeID,
char prev,
char *keywords, int32_t keywordCapacity,
char *values, int32_t valuesCapacity, int32_t *valLen,
UBool valuesToo,
UErrorCode *status);
static const char * const LANGUAGES[] = {
"aa", "ab", "ace", "ach", "ada", "ady", "ae", "aeb",
"af", "afh", "agq", "ain", "ak", "akk", "akz", "ale",
"aln", "alt", "am", "an", "ang", "anp", "ar", "arc",
"arn", "aro", "arp", "arq", "ars", "arw", "ary", "arz", "as",
"asa", "ase", "ast", "av", "avk", "awa", "ay", "az",
"ba", "bal", "ban", "bar", "bas", "bax", "bbc", "bbj",
"be", "bej", "bem", "bew", "bez", "bfd", "bfq", "bg",
"bgn", "bho", "bi", "bik", "bin", "bjn", "bkm", "bla",
"bm", "bn", "bo", "bpy", "bqi", "br", "bra", "brh",
"brx", "bs", "bss", "bua", "bug", "bum", "byn", "byv",
"ca", "cad", "car", "cay", "cch", "ccp", "ce", "ceb", "cgg",
"ch", "chb", "chg", "chk", "chm", "chn", "cho", "chp",
"chr", "chy", "ckb", "co", "cop", "cps", "cr", "crh",
"cs", "csb", "cu", "cv", "cy",
"da", "dak", "dar", "dav", "de", "del", "den", "dgr",
"din", "dje", "doi", "dsb", "dtp", "dua", "dum", "dv",
"dyo", "dyu", "dz", "dzg",
"ebu", "ee", "efi", "egl", "egy", "eka", "el", "elx",
"en", "enm", "eo", "es", "esu", "et", "eu", "ewo",
"ext",
"fa", "fan", "fat", "ff", "fi", "fil", "fit", "fj",
"fo", "fon", "fr", "frc", "frm", "fro", "frp", "frr",
"frs", "fur", "fy",
"ga", "gaa", "gag", "gan", "gay", "gba", "gbz", "gd",
"gez", "gil", "gl", "glk", "gmh", "gn", "goh", "gom",
"gon", "gor", "got", "grb", "grc", "gsw", "gu", "guc",
"gur", "guz", "gv", "gwi",
"ha", "hai", "hak", "haw", "he", "hi", "hif", "hil",
"hit", "hmn", "ho", "hr", "hsb", "hsn", "ht", "hu",
"hup", "hy", "hz",
"ia", "iba", "ibb", "id", "ie", "ig", "ii", "ik",
"ilo", "inh", "io", "is", "it", "iu", "izh",
"ja", "jam", "jbo", "jgo", "jmc", "jpr", "jrb", "jut",
"jv",
"ka", "kaa", "kab", "kac", "kaj", "kam", "kaw", "kbd",
"kbl", "kcg", "kde", "kea", "ken", "kfo", "kg", "kgp",
"kha", "kho", "khq", "khw", "ki", "kiu", "kj", "kk",
"kkj", "kl", "kln", "km", "kmb", "kn", "ko", "koi",
"kok", "kos", "kpe", "kr", "krc", "kri", "krj", "krl",
"kru", "ks", "ksb", "ksf", "ksh", "ku", "kum", "kut",
"kv", "kw", "ky",
"la", "lad", "lag", "lah", "lam", "lb", "lez", "lfn",
"lg", "li", "lij", "liv", "lkt", "lmo", "ln", "lo",
"lol", "loz", "lrc", "lt", "ltg", "lu", "lua", "lui",
"lun", "luo", "lus", "luy", "lv", "lzh", "lzz",
"mad", "maf", "mag", "mai", "mak", "man", "mas", "mde",
"mdf", "mdh", "mdr", "men", "mer", "mfe", "mg", "mga",
"mgh", "mgo", "mh", "mi", "mic", "min", "mis", "mk",
"ml", "mn", "mnc", "mni", "mo",
"moh", "mos", "mr", "mrj",
"ms", "mt", "mua", "mul", "mus", "mwl", "mwr", "mwv",
"my", "mye", "myv", "mzn",
"na", "nan", "nap", "naq", "nb", "nd", "nds", "ne",
"new", "ng", "nia", "niu", "njo", "nl", "nmg", "nn",
"nnh", "no", "nog", "non", "nov", "nqo", "nr", "nso",
"nus", "nv", "nwc", "ny", "nym", "nyn", "nyo", "nzi",
"oc", "oj", "om", "or", "os", "osa", "ota",
"pa", "pag", "pal", "pam", "pap", "pau", "pcd", "pdc",
"pdt", "peo", "pfl", "phn", "pi", "pl", "pms", "pnt",
"pon", "prg", "pro", "ps", "pt",
"qu", "quc", "qug",
"raj", "rap", "rar", "rgn", "rif", "rm", "rn", "ro",
"rof", "rom", "rtm", "ru", "rue", "rug", "rup",
"rw", "rwk",
"sa", "sad", "sah", "sam", "saq", "sas", "sat", "saz",
"sba", "sbp", "sc", "scn", "sco", "sd", "sdc", "sdh",
"se", "see", "seh", "sei", "sel", "ses", "sg", "sga",
"sgs", "shi", "shn", "shu", "si", "sid", "sk",
"sl", "sli", "sly", "sm", "sma", "smj", "smn", "sms",
"sn", "snk", "so", "sog", "sq", "sr", "srn", "srr",
"ss", "ssy", "st", "stq", "su", "suk", "sus", "sux",
"sv", "sw", "swb", "swc", "syc", "syr", "szl",
"ta", "tcy", "te", "tem", "teo", "ter", "tet", "tg",
"th", "ti", "tig", "tiv", "tk", "tkl", "tkr", "tl",
"tlh", "tli", "tly", "tmh", "tn", "to", "tog", "tpi",
"tr", "tru", "trv", "ts", "tsd", "tsi", "tt", "ttt",
"tum", "tvl", "tw", "twq", "ty", "tyv", "tzm",
"udm", "ug", "uga", "uk", "umb", "und", "ur", "uz",
"vai", "ve", "vec", "vep", "vi", "vls", "vmf", "vo",
"vot", "vro", "vun",
"wa", "wae", "wal", "war", "was", "wbp", "wo", "wuu",
"xal", "xh", "xmf", "xog",
"yao", "yap", "yav", "ybb", "yi", "yo", "yrl", "yue",
"za", "zap", "zbl", "zea", "zen", "zgh", "zh", "zu",
"zun", "zxx", "zza",
NULL,
"in", "iw", "ji", "jw", "sh",
NULL
};
static const char* const DEPRECATED_LANGUAGES[]={
"in", "iw", "ji", "jw", NULL, NULL
};
static const char* const REPLACEMENT_LANGUAGES[]={
"id", "he", "yi", "jv", NULL, NULL
};
static const char * const LANGUAGES_3[] = {
"aar", "abk", "ace", "ach", "ada", "ady", "ave", "aeb",
"afr", "afh", "agq", "ain", "aka", "akk", "akz", "ale",
"aln", "alt", "amh", "arg", "ang", "anp", "ara", "arc",
"arn", "aro", "arp", "arq", "ars", "arw", "ary", "arz", "asm",
"asa", "ase", "ast", "ava", "avk", "awa", "aym", "aze",
"bak", "bal", "ban", "bar", "bas", "bax", "bbc", "bbj",
"bel", "bej", "bem", "bew", "bez", "bfd", "bfq", "bul",
"bgn", "bho", "bis", "bik", "bin", "bjn", "bkm", "bla",
"bam", "ben", "bod", "bpy", "bqi", "bre", "bra", "brh",
"brx", "bos", "bss", "bua", "bug", "bum", "byn", "byv",
"cat", "cad", "car", "cay", "cch", "ccp", "che", "ceb", "cgg",
"cha", "chb", "chg", "chk", "chm", "chn", "cho", "chp",
"chr", "chy", "ckb", "cos", "cop", "cps", "cre", "crh",
"ces", "csb", "chu", "chv", "cym",
"dan", "dak", "dar", "dav", "deu", "del", "den", "dgr",
"din", "dje", "doi", "dsb", "dtp", "dua", "dum", "div",
"dyo", "dyu", "dzo", "dzg",
"ebu", "ewe", "efi", "egl", "egy", "eka", "ell", "elx",
"eng", "enm", "epo", "spa", "esu", "est", "eus", "ewo",
"ext",
"fas", "fan", "fat", "ful", "fin", "fil", "fit", "fij",
"fao", "fon", "fra", "frc", "frm", "fro", "frp", "frr",
"frs", "fur", "fry",
"gle", "gaa", "gag", "gan", "gay", "gba", "gbz", "gla",
"gez", "gil", "glg", "glk", "gmh", "grn", "goh", "gom",
"gon", "gor", "got", "grb", "grc", "gsw", "guj", "guc",
"gur", "guz", "glv", "gwi",
"hau", "hai", "hak", "haw", "heb", "hin", "hif", "hil",
"hit", "hmn", "hmo", "hrv", "hsb", "hsn", "hat", "hun",
"hup", "hye", "her",
"ina", "iba", "ibb", "ind", "ile", "ibo", "iii", "ipk",
"ilo", "inh", "ido", "isl", "ita", "iku", "izh",
"jpn", "jam", "jbo", "jgo", "jmc", "jpr", "jrb", "jut",
"jav",
"kat", "kaa", "kab", "kac", "kaj", "kam", "kaw", "kbd",
"kbl", "kcg", "kde", "kea", "ken", "kfo", "kon", "kgp",
"kha", "kho", "khq", "khw", "kik", "kiu", "kua", "kaz",
"kkj", "kal", "kln", "khm", "kmb", "kan", "kor", "koi",
"kok", "kos", "kpe", "kau", "krc", "kri", "krj", "krl",
"kru", "kas", "ksb", "ksf", "ksh", "kur", "kum", "kut",
"kom", "cor", "kir",
"lat", "lad", "lag", "lah", "lam", "ltz", "lez", "lfn",
"lug", "lim", "lij", "liv", "lkt", "lmo", "lin", "lao",
"lol", "loz", "lrc", "lit", "ltg", "lub", "lua", "lui",
"lun", "luo", "lus", "luy", "lav", "lzh", "lzz",
"mad", "maf", "mag", "mai", "mak", "man", "mas", "mde",
"mdf", "mdh", "mdr", "men", "mer", "mfe", "mlg", "mga",
"mgh", "mgo", "mah", "mri", "mic", "min", "mis", "mkd",
"mal", "mon", "mnc", "mni", "mol",
"moh", "mos", "mar", "mrj",
"msa", "mlt", "mua", "mul", "mus", "mwl", "mwr", "mwv",
"mya", "mye", "myv", "mzn",
"nau", "nan", "nap", "naq", "nob", "nde", "nds", "nep",
"new", "ndo", "nia", "niu", "njo", "nld", "nmg", "nno",
"nnh", "nor", "nog", "non", "nov", "nqo", "nbl", "nso",
"nus", "nav", "nwc", "nya", "nym", "nyn", "nyo", "nzi",
"oci", "oji", "orm", "ori", "oss", "osa", "ota",
"pan", "pag", "pal", "pam", "pap", "pau", "pcd", "pdc",
"pdt", "peo", "pfl", "phn", "pli", "pol", "pms", "pnt",
"pon", "prg", "pro", "pus", "por",
"que", "quc", "qug",
"raj", "rap", "rar", "rgn", "rif", "roh", "run", "ron",
"rof", "rom", "rtm", "rus", "rue", "rug", "rup",
"kin", "rwk",
"san", "sad", "sah", "sam", "saq", "sas", "sat", "saz",
"sba", "sbp", "srd", "scn", "sco", "snd", "sdc", "sdh",
"sme", "see", "seh", "sei", "sel", "ses", "sag", "sga",
"sgs", "shi", "shn", "shu", "sin", "sid", "slk",
"slv", "sli", "sly", "smo", "sma", "smj", "smn", "sms",
"sna", "snk", "som", "sog", "sqi", "srp", "srn", "srr",
"ssw", "ssy", "sot", "stq", "sun", "suk", "sus", "sux",
"swe", "swa", "swb", "swc", "syc", "syr", "szl",
"tam", "tcy", "tel", "tem", "teo", "ter", "tet", "tgk",
"tha", "tir", "tig", "tiv", "tuk", "tkl", "tkr", "tgl",
"tlh", "tli", "tly", "tmh", "tsn", "ton", "tog", "tpi",
"tur", "tru", "trv", "tso", "tsd", "tsi", "tat", "ttt",
"tum", "tvl", "twi", "twq", "tah", "tyv", "tzm",
"udm", "uig", "uga", "ukr", "umb", "und", "urd", "uzb",
"vai", "ven", "vec", "vep", "vie", "vls", "vmf", "vol",
"vot", "vro", "vun",
"wln", "wae", "wal", "war", "was", "wbp", "wol", "wuu",
"xal", "xho", "xmf", "xog",
"yao", "yap", "yav", "ybb", "yid", "yor", "yrl", "yue",
"zha", "zap", "zbl", "zea", "zen", "zgh", "zho", "zul",
"zun", "zxx", "zza",
NULL,
"ind", "heb", "yid", "jaw", "srp",
NULL
};
static const char * const COUNTRIES[] = {
"AC", "AD", "AE", "AF", "AG", "AI", "AL", "AM",
"AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ",
"BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI",
"BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV",
"BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG",
"CH", "CI", "CK", "CL", "CM", "CN", "CO", "CP", "CR",
"CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DG", "DJ", "DK",
"DM", "DO", "DZ", "EA", "EC", "EE", "EG", "EH", "ER",
"ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR",
"GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL",
"GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU",
"GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU",
"IC", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS",
"IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI",
"KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA",
"LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU",
"LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK",
"ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS",
"MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
"NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP",
"NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG",
"PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT",
"PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA",
"SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ",
"SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV",
"SX", "SY", "SZ", "TA", "TC", "TD", "TF", "TG", "TH", "TJ",
"TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV",
"TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ",
"VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF",
"WS", "XK", "YE", "YT", "ZA", "ZM", "ZW",
NULL,
"AN", "BU", "CS", "FX", "RO", "SU", "TP", "YD", "YU", "ZR",
NULL
};
static const char* const DEPRECATED_COUNTRIES[] = {
"AN", "BU", "CS", "DD", "DY", "FX", "HV", "NH", "RH", "SU", "TP", "UK", "VD", "YD", "YU", "ZR", NULL, NULL
};
static const char* const REPLACEMENT_COUNTRIES[] = {
"CW", "MM", "RS", "DE", "BJ", "FR", "BF", "VU", "ZW", "RU", "TL", "GB", "VN", "YE", "RS", "CD", NULL, NULL
};
static const char * const COUNTRIES_3[] = {
"ASC", "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM",
"AGO", "ATA", "ARG", "ASM", "AUT", "AUS", "ABW", "ALA", "AZE",
"BIH", "BRB", "BGD", "BEL", "BFA", "BGR", "BHR", "BDI",
"BEN", "BLM", "BMU", "BRN", "BOL", "BES", "BRA", "BHS", "BTN", "BVT",
"BWA", "BLR", "BLZ", "CAN", "CCK", "COD", "CAF", "COG",
"CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CPT", "CRI",
"CUB", "CPV", "CUW", "CXR", "CYP", "CZE", "DEU", "DGA", "DJI", "DNK",
"DMA", "DOM", "DZA", "EA ", "ECU", "EST", "EGY", "ESH", "ERI",
"ESP", "ETH", "FIN", "FJI", "FLK", "FSM", "FRO", "FRA",
"GAB", "GBR", "GRD", "GEO", "GUF", "GGY", "GHA", "GIB", "GRL",
"GMB", "GIN", "GLP", "GNQ", "GRC", "SGS", "GTM", "GUM",
"GNB", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN",
"IC ", "IDN", "IRL", "ISR", "IMN", "IND", "IOT", "IRQ", "IRN", "ISL",
"ITA", "JEY", "JAM", "JOR", "JPN", "KEN", "KGZ", "KHM", "KIR",
"COM", "KNA", "PRK", "KOR", "KWT", "CYM", "KAZ", "LAO",
"LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU", "LUX",
"LVA", "LBY", "MAR", "MCO", "MDA", "MNE", "MAF", "MDG", "MHL", "MKD",
"MLI", "MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR",
"MLT", "MUS", "MDV", "MWI", "MEX", "MYS", "MOZ", "NAM",
"NCL", "NER", "NFK", "NGA", "NIC", "NLD", "NOR", "NPL",
"NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF", "PNG",
"PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT",
"PLW", "PRY", "QAT", "REU", "ROU", "SRB", "RUS", "RWA", "SAU",
"SLB", "SYC", "SDN", "SWE", "SGP", "SHN", "SVN", "SJM",
"SVK", "SLE", "SMR", "SEN", "SOM", "SUR", "SSD", "STP", "SLV",
"SXM", "SYR", "SWZ", "TAA", "TCA", "TCD", "ATF", "TGO", "THA", "TJK",
"TKL", "TLS", "TKM", "TUN", "TON", "TUR", "TTO", "TUV",
"TWN", "TZA", "UKR", "UGA", "UMI", "USA", "URY", "UZB",
"VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "VUT", "WLF",
"WSM", "XKK", "YEM", "MYT", "ZAF", "ZMB", "ZWE",
NULL,
"ANT", "BUR", "SCG", "FXX", "ROM", "SUN", "TMP", "YMD", "YUG", "ZAR",
NULL
};
typedef struct CanonicalizationMap {
const char *id;
const char *canonicalID;
} CanonicalizationMap;
static const CanonicalizationMap CANONICALIZE_MAP[] = {
{ "", "en_US_POSIX" }, { "c", "en_US_POSIX" }, { "posix", "en_US_POSIX" }, { "art_LOJBAN", "jbo" },
{ "hy__AREVELA", "hy" },
{ "hy__AREVMDA", "hyw" },
{ "zh_GAN", "gan" },
{ "zh_GUOYU", "zh" },
{ "zh_HAKKA", "hak" },
{ "zh_MIN_NAN", "nan" },
{ "zh_WUU", "wuu" },
{ "zh_XIANG", "hsn" },
{ "zh_YUE", "yue" },
};
#define _hasBCP47Extension(id) (id && uprv_strstr(id, "@") == NULL && getShortestSubtagLength(localeID) == 1)
#define _ConvertBCP47(finalID, id, buffer, length,err) UPRV_BLOCK_MACRO_BEGIN { \
if (uloc_forLanguageTag(id, buffer, length, NULL, err) <= 0 || \
U_FAILURE(*err) || *err == U_STRING_NOT_TERMINATED_WARNING) { \
finalID=id; \
if (*err == U_STRING_NOT_TERMINATED_WARNING) { *err = U_BUFFER_OVERFLOW_ERROR; } \
} else { \
finalID=buffer; \
} \
} UPRV_BLOCK_MACRO_END
static int32_t getShortestSubtagLength(const char *localeID) {
int32_t localeIDLength = static_cast<int32_t>(uprv_strlen(localeID));
int32_t length = localeIDLength;
int32_t tmpLength = 0;
int32_t i;
UBool reset = TRUE;
for (i = 0; i < localeIDLength; i++) {
if (localeID[i] != '_' && localeID[i] != '-') {
if (reset) {
tmpLength = 0;
reset = FALSE;
}
tmpLength++;
} else {
if (tmpLength != 0 && tmpLength < length) {
length = tmpLength;
}
reset = TRUE;
}
}
return length;
}
#define UPRV_ISDIGIT(c) (((c) >= '0') && ((c) <= '9'))
#define UPRV_ISALPHANUM(c) (uprv_isASCIILetter(c) || UPRV_ISDIGIT(c) )
#define UPRV_OK_VALUE_PUNCTUATION(c) ((c) == '_' || (c) == '-' || (c) == '+' || (c) == '/')
#define ULOC_KEYWORD_BUFFER_LEN 25
#define ULOC_MAX_NO_KEYWORDS 25
U_CAPI const char * U_EXPORT2
locale_getKeywordsStart(const char *localeID) {
const char *result = NULL;
if((result = uprv_strchr(localeID, '@')) != NULL) {
return result;
}
#if (U_CHARSET_FAMILY == U_EBCDIC_FAMILY)
else {
static const uint8_t ebcdicSigns[] = { 0x7C, 0x44, 0x66, 0x80, 0xAC, 0xAE, 0xAF, 0xB5, 0xEC, 0xEF, 0x00 };
const uint8_t *charToFind = ebcdicSigns;
while(*charToFind) {
if((result = uprv_strchr(localeID, *charToFind)) != NULL) {
return result;
}
charToFind++;
}
}
#endif
return NULL;
}
static int32_t locale_canonKeywordName(char *buf, const char *keywordName, UErrorCode *status)
{
int32_t keywordNameLen = 0;
for (; *keywordName != 0; keywordName++) {
if (!UPRV_ISALPHANUM(*keywordName)) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if (keywordNameLen < ULOC_KEYWORD_BUFFER_LEN - 1) {
buf[keywordNameLen++] = uprv_tolower(*keywordName);
} else {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
}
if (keywordNameLen == 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
buf[keywordNameLen] = 0;
return keywordNameLen;
}
typedef struct {
char keyword[ULOC_KEYWORD_BUFFER_LEN];
int32_t keywordLen;
const char *valueStart;
int32_t valueLen;
} KeywordStruct;
static int32_t U_CALLCONV
compareKeywordStructs(const void * , const void *left, const void *right) {
const char* leftString = ((const KeywordStruct *)left)->keyword;
const char* rightString = ((const KeywordStruct *)right)->keyword;
return uprv_strcmp(leftString, rightString);
}
static int32_t
_getKeywords(const char *localeID,
char prev,
char *keywords, int32_t keywordCapacity,
char *values, int32_t valuesCapacity, int32_t *valLen,
UBool valuesToo,
UErrorCode *status)
{
KeywordStruct keywordList[ULOC_MAX_NO_KEYWORDS];
int32_t maxKeywords = ULOC_MAX_NO_KEYWORDS;
int32_t numKeywords = 0;
const char* pos = localeID;
const char* equalSign = NULL;
const char* semicolon = NULL;
int32_t i = 0, j, n;
int32_t keywordsLen = 0;
int32_t valuesLen = 0;
if(prev == '@') {
do {
UBool duplicate = FALSE;
while(*pos == ' ') {
pos++;
}
if (!*pos) {
break;
}
if(numKeywords == maxKeywords) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
equalSign = uprv_strchr(pos, '=');
semicolon = uprv_strchr(pos, ';');
if(!equalSign || (semicolon && semicolon<equalSign)) {
*status = U_INVALID_FORMAT_ERROR;
return 0;
}
if(equalSign - pos >= ULOC_KEYWORD_BUFFER_LEN) {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
for(i = 0, n = 0; i < equalSign - pos; ++i) {
if (pos[i] != ' ') {
keywordList[numKeywords].keyword[n++] = uprv_tolower(pos[i]);
}
}
if (n == 0) {
*status = U_INVALID_FORMAT_ERROR;
return 0;
}
keywordList[numKeywords].keyword[n] = 0;
keywordList[numKeywords].keywordLen = n;
equalSign++;
while(*equalSign == ' ') {
equalSign++;
}
if (!*equalSign || equalSign == semicolon) {
*status = U_INVALID_FORMAT_ERROR;
return 0;
}
keywordList[numKeywords].valueStart = equalSign;
pos = semicolon;
i = 0;
if(pos) {
while(*(pos - i - 1) == ' ') {
i++;
}
keywordList[numKeywords].valueLen = (int32_t)(pos - equalSign - i);
pos++;
} else {
i = (int32_t)uprv_strlen(equalSign);
while(i && equalSign[i-1] == ' ') {
i--;
}
keywordList[numKeywords].valueLen = i;
}
for (j=0; j<numKeywords; ++j) {
if (uprv_strcmp(keywordList[j].keyword, keywordList[numKeywords].keyword) == 0) {
duplicate = TRUE;
break;
}
}
if (!duplicate) {
++numKeywords;
}
} while(pos);
uprv_sortArray(keywordList, numKeywords, sizeof(KeywordStruct), compareKeywordStructs, NULL, FALSE, status);
for(i = 0; i < numKeywords; i++) {
if(keywordsLen + keywordList[i].keywordLen + 1< keywordCapacity) {
uprv_strcpy(keywords+keywordsLen, keywordList[i].keyword);
if(valuesToo) {
keywords[keywordsLen + keywordList[i].keywordLen] = '=';
} else {
keywords[keywordsLen + keywordList[i].keywordLen] = 0;
}
}
keywordsLen += keywordList[i].keywordLen + 1;
if(valuesToo) {
if(keywordsLen + keywordList[i].valueLen <= keywordCapacity) {
uprv_strncpy(keywords+keywordsLen, keywordList[i].valueStart, keywordList[i].valueLen);
}
keywordsLen += keywordList[i].valueLen;
if(i < numKeywords - 1) {
if(keywordsLen < keywordCapacity) {
keywords[keywordsLen] = ';';
}
keywordsLen++;
}
}
if(values) {
if(valuesLen + keywordList[i].valueLen + 1< valuesCapacity) {
uprv_strcpy(values+valuesLen, keywordList[i].valueStart);
values[valuesLen + keywordList[i].valueLen] = 0;
}
valuesLen += keywordList[i].valueLen + 1;
}
}
if(values) {
values[valuesLen] = 0;
if(valLen) {
*valLen = valuesLen;
}
}
return u_terminateChars(keywords, keywordCapacity, keywordsLen, status);
} else {
return 0;
}
}
U_CFUNC int32_t
locale_getKeywords(const char *localeID,
char prev,
char *keywords, int32_t keywordCapacity,
char *values, int32_t valuesCapacity, int32_t *valLen,
UBool valuesToo,
UErrorCode *status) {
return _getKeywords(localeID, prev, keywords, keywordCapacity,
values, valuesCapacity, valLen, valuesToo,
status);
}
U_CAPI int32_t U_EXPORT2
uloc_getKeywordValue(const char* localeID,
const char* keywordName,
char* buffer, int32_t bufferCapacity,
UErrorCode* status)
{
if (buffer != nullptr) {
buffer[0] = '\0';
}
const char* startSearchHere = NULL;
const char* nextSeparator = NULL;
char keywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
char localeKeywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
int32_t result = 0;
if(status && U_SUCCESS(*status) && localeID) {
char tempBuffer[ULOC_FULLNAME_CAPACITY];
const char* tmpLocaleID;
if (keywordName == NULL || keywordName[0] == 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
locale_canonKeywordName(keywordNameBuffer, keywordName, status);
if(U_FAILURE(*status)) {
return 0;
}
if (_hasBCP47Extension(localeID)) {
_ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), status);
} else {
tmpLocaleID=localeID;
}
startSearchHere = locale_getKeywordsStart(tmpLocaleID);
if(startSearchHere == NULL) {
return 0;
}
while(startSearchHere) {
const char* keyValueTail;
int32_t keyValueLen;
startSearchHere++;
nextSeparator = uprv_strchr(startSearchHere, '=');
if(!nextSeparator) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
while(*startSearchHere == ' ') {
startSearchHere++;
}
keyValueTail = nextSeparator;
while (keyValueTail > startSearchHere && *(keyValueTail-1) == ' ') {
keyValueTail--;
}
if (startSearchHere == keyValueTail) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
keyValueLen = 0;
while (startSearchHere < keyValueTail) {
if (!UPRV_ISALPHANUM(*startSearchHere)) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if (keyValueLen < ULOC_KEYWORD_BUFFER_LEN - 1) {
localeKeywordNameBuffer[keyValueLen++] = uprv_tolower(*startSearchHere++);
} else {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
}
localeKeywordNameBuffer[keyValueLen] = 0;
startSearchHere = uprv_strchr(nextSeparator, ';');
if(uprv_strcmp(keywordNameBuffer, localeKeywordNameBuffer) == 0) {
nextSeparator++;
while(*nextSeparator == ' ') {
nextSeparator++;
}
keyValueTail = (startSearchHere)? startSearchHere: nextSeparator + uprv_strlen(nextSeparator);
while(keyValueTail > nextSeparator && *(keyValueTail-1) == ' ') {
keyValueTail--;
}
if (nextSeparator == keyValueTail) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
keyValueLen = 0;
while (nextSeparator < keyValueTail) {
if (!UPRV_ISALPHANUM(*nextSeparator) && !UPRV_OK_VALUE_PUNCTUATION(*nextSeparator)) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if (keyValueLen < bufferCapacity) {
buffer[keyValueLen++] = *nextSeparator++;
} else {
keyValueLen++;
nextSeparator++;
}
}
result = u_terminateChars(buffer, bufferCapacity, keyValueLen, status);
return result;
}
}
}
return 0;
}
U_CAPI int32_t U_EXPORT2
uloc_setKeywordValue(const char* keywordName,
const char* keywordValue,
char* buffer, int32_t bufferCapacity,
UErrorCode* status)
{
int32_t keywordNameLen;
int32_t keywordValueLen;
int32_t bufLen;
int32_t needLen = 0;
char keywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
char keywordValueBuffer[ULOC_KEYWORDS_CAPACITY+1];
char localeKeywordNameBuffer[ULOC_KEYWORD_BUFFER_LEN];
int32_t rc;
char* nextSeparator = NULL;
char* nextEqualsign = NULL;
char* startSearchHere = NULL;
char* keywordStart = NULL;
CharString updatedKeysAndValues;
int32_t updatedKeysAndValuesLen;
UBool handledInputKeyAndValue = FALSE;
char keyValuePrefix = '@';
if(U_FAILURE(*status)) {
return -1;
}
if (keywordName == NULL || keywordName[0] == 0 || bufferCapacity <= 1) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
bufLen = (int32_t)uprv_strlen(buffer);
if(bufferCapacity<bufLen) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
keywordNameLen = locale_canonKeywordName(keywordNameBuffer, keywordName, status);
if(U_FAILURE(*status)) {
return 0;
}
keywordValueLen = 0;
if(keywordValue) {
while (*keywordValue != 0) {
if (!UPRV_ISALPHANUM(*keywordValue) && !UPRV_OK_VALUE_PUNCTUATION(*keywordValue)) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if (keywordValueLen < ULOC_KEYWORDS_CAPACITY) {
keywordValueBuffer[keywordValueLen++] = *keywordValue++;
} else {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
}
}
keywordValueBuffer[keywordValueLen] = 0;
startSearchHere = (char*)locale_getKeywordsStart(buffer);
if(startSearchHere == NULL || (startSearchHere[1]==0)) {
if(keywordValueLen == 0) {
return bufLen;
}
needLen = bufLen+1+keywordNameLen+1+keywordValueLen;
if(startSearchHere) {
needLen--;
} else {
startSearchHere=buffer+bufLen;
}
if(needLen >= bufferCapacity) {
*status = U_BUFFER_OVERFLOW_ERROR;
return needLen;
}
*startSearchHere++ = '@';
uprv_strcpy(startSearchHere, keywordNameBuffer);
startSearchHere += keywordNameLen;
*startSearchHere++ = '=';
uprv_strcpy(startSearchHere, keywordValueBuffer);
return needLen;
}
keywordStart = startSearchHere;
while(keywordStart) {
const char* keyValueTail;
int32_t keyValueLen;
keywordStart++;
nextEqualsign = uprv_strchr(keywordStart, '=');
if (!nextEqualsign) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
while(*keywordStart == ' ') {
keywordStart++;
}
keyValueTail = nextEqualsign;
while (keyValueTail > keywordStart && *(keyValueTail-1) == ' ') {
keyValueTail--;
}
if (keywordStart == keyValueTail) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
keyValueLen = 0;
while (keywordStart < keyValueTail) {
if (!UPRV_ISALPHANUM(*keywordStart)) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if (keyValueLen < ULOC_KEYWORD_BUFFER_LEN - 1) {
localeKeywordNameBuffer[keyValueLen++] = uprv_tolower(*keywordStart++);
} else {
*status = U_INTERNAL_PROGRAM_ERROR;
return 0;
}
}
localeKeywordNameBuffer[keyValueLen] = 0;
nextSeparator = uprv_strchr(nextEqualsign, ';');
nextEqualsign++;
while(*nextEqualsign == ' ') {
nextEqualsign++;
}
keyValueTail = (nextSeparator)? nextSeparator: nextEqualsign + uprv_strlen(nextEqualsign);
while(keyValueTail > nextEqualsign && *(keyValueTail-1) == ' ') {
keyValueTail--;
}
if (nextEqualsign == keyValueTail) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
rc = uprv_strcmp(keywordNameBuffer, localeKeywordNameBuffer);
if(rc == 0) {
if(keywordValueLen > 0) {
updatedKeysAndValues.append(keyValuePrefix, *status);
keyValuePrefix = ';';
updatedKeysAndValues.append(keywordNameBuffer, keywordNameLen, *status);
updatedKeysAndValues.append('=', *status);
updatedKeysAndValues.append(keywordValueBuffer, keywordValueLen, *status);
}
handledInputKeyAndValue = TRUE;
} else {
if (rc < 0 && keywordValueLen > 0 && !handledInputKeyAndValue) {
updatedKeysAndValues.append(keyValuePrefix, *status);
keyValuePrefix = ';';
updatedKeysAndValues.append(keywordNameBuffer, keywordNameLen, *status);
updatedKeysAndValues.append('=', *status);
updatedKeysAndValues.append(keywordValueBuffer, keywordValueLen, *status);
handledInputKeyAndValue = TRUE;
}
updatedKeysAndValues.append(keyValuePrefix, *status);
keyValuePrefix = ';';
updatedKeysAndValues.append(localeKeywordNameBuffer, keyValueLen, *status);
updatedKeysAndValues.append('=', *status);
updatedKeysAndValues.append(nextEqualsign, static_cast<int32_t>(keyValueTail-nextEqualsign), *status);
}
if (!nextSeparator && keywordValueLen > 0 && !handledInputKeyAndValue) {
updatedKeysAndValues.append(keyValuePrefix, *status);
updatedKeysAndValues.append(keywordNameBuffer, keywordNameLen, *status);
updatedKeysAndValues.append('=', *status);
updatedKeysAndValues.append(keywordValueBuffer, keywordValueLen, *status);
handledInputKeyAndValue = TRUE;
}
keywordStart = nextSeparator;
}
if (!handledInputKeyAndValue || U_FAILURE(*status)) {
return bufLen;
}
updatedKeysAndValuesLen = updatedKeysAndValues.length();
needLen = (int32_t)(startSearchHere - buffer) + updatedKeysAndValuesLen;
if(needLen >= bufferCapacity) {
*status = U_BUFFER_OVERFLOW_ERROR;
return needLen;
}
if (updatedKeysAndValuesLen > 0) {
uprv_strncpy(startSearchHere, updatedKeysAndValues.data(), updatedKeysAndValuesLen);
}
buffer[needLen]=0;
return needLen;
}
#define _isPrefixLetter(a) ((a=='x')||(a=='X')||(a=='i')||(a=='I'))
#define _isIDPrefix(s) (_isPrefixLetter(s[0])&&_isIDSeparator(s[1]))
#define _isTerminator(a) ((a==0)||(a=='.')||(a=='@'))
static int16_t _findIndex(const char* const* list, const char* key)
{
const char* const* anchor = list;
int32_t pass = 0;
while (pass++ < 2) {
while (*list) {
if (uprv_strcmp(key, *list) == 0) {
return (int16_t)(list - anchor);
}
list++;
}
++list;
}
return -1;
}
static inline int32_t
_copyCount(char *dest, int32_t destCapacity, const char *src) {
const char *anchor;
char c;
anchor=src;
for(;;) {
if((c=*src)==0) {
return (int32_t)(src-anchor);
}
if(destCapacity<=0) {
return (int32_t)((src-anchor)+uprv_strlen(src));
}
++src;
*dest++=c;
--destCapacity;
}
}
U_CFUNC const char*
uloc_getCurrentCountryID(const char* oldID){
int32_t offset = _findIndex(DEPRECATED_COUNTRIES, oldID);
if (offset >= 0) {
return REPLACEMENT_COUNTRIES[offset];
}
return oldID;
}
U_CFUNC const char*
uloc_getCurrentLanguageID(const char* oldID){
int32_t offset = _findIndex(DEPRECATED_LANGUAGES, oldID);
if (offset >= 0) {
return REPLACEMENT_LANGUAGES[offset];
}
return oldID;
}
U_CFUNC int32_t
ulocimp_getLanguage(const char *localeID,
char *language, int32_t languageCapacity,
const char **pEnd) {
int32_t i=0;
int32_t offset;
char lang[4]={ 0, 0, 0, 0 };
if(_isIDPrefix(localeID)) {
if(i<languageCapacity) {
language[i]=(char)uprv_tolower(*localeID);
}
if(i<languageCapacity) {
language[i+1]='-';
}
i+=2;
localeID+=2;
}
while(!_isTerminator(*localeID) && !_isIDSeparator(*localeID)) {
if(i<languageCapacity) {
language[i]=(char)uprv_tolower(*localeID);
}
if(i<3) {
U_ASSERT(i>=0);
lang[i]=(char)uprv_tolower(*localeID);
}
i++;
localeID++;
}
if(i==3) {
offset=_findIndex(LANGUAGES_3, lang);
if(offset>=0) {
i=_copyCount(language, languageCapacity, LANGUAGES[offset]);
}
}
if(pEnd!=NULL) {
*pEnd=localeID;
}
return i;
}
U_CFUNC int32_t
ulocimp_getScript(const char *localeID,
char *script, int32_t scriptCapacity,
const char **pEnd)
{
int32_t idLen = 0;
if (pEnd != NULL) {
*pEnd = localeID;
}
while(!_isTerminator(localeID[idLen]) && !_isIDSeparator(localeID[idLen])
&& uprv_isASCIILetter(localeID[idLen])) {
idLen++;
}
if (idLen == 4) {
int32_t i;
if (pEnd != NULL) {
*pEnd = localeID+idLen;
}
if(idLen > scriptCapacity) {
idLen = scriptCapacity;
}
if (idLen >= 1) {
script[0]=(char)uprv_toupper(*(localeID++));
}
for (i = 1; i < idLen; i++) {
script[i]=(char)uprv_tolower(*(localeID++));
}
}
else {
idLen = 0;
}
return idLen;
}
U_CFUNC int32_t
ulocimp_getCountry(const char *localeID,
char *country, int32_t countryCapacity,
const char **pEnd)
{
int32_t idLen=0;
char cnty[ULOC_COUNTRY_CAPACITY]={ 0, 0, 0, 0 };
int32_t offset;
while(!_isTerminator(localeID[idLen]) && !_isIDSeparator(localeID[idLen])) {
if(idLen<(ULOC_COUNTRY_CAPACITY-1)) {
cnty[idLen]=(char)uprv_toupper(localeID[idLen]);
}
idLen++;
}
if (idLen == 2 || idLen == 3) {
UBool gotCountry = FALSE;
if(idLen==3) {
offset=_findIndex(COUNTRIES_3, cnty);
if(offset>=0) {
idLen=_copyCount(country, countryCapacity, COUNTRIES[offset]);
gotCountry = TRUE;
}
}
if (!gotCountry) {
int32_t i = 0;
for (i = 0; i < idLen; i++) {
if (i < countryCapacity) {
country[i]=(char)uprv_toupper(localeID[i]);
}
}
}
localeID+=idLen;
} else {
idLen = 0;
}
if(pEnd!=NULL) {
*pEnd=localeID;
}
return idLen;
}
static int32_t
_getVariantEx(const char *localeID,
char prev,
char *variant, int32_t variantCapacity,
UBool needSeparator) {
int32_t i=0;
if(_isIDSeparator(prev)) {
while(!_isTerminator(*localeID)) {
if (needSeparator) {
if (i<variantCapacity) {
variant[i] = '_';
}
++i;
needSeparator = FALSE;
}
if(i<variantCapacity) {
variant[i]=(char)uprv_toupper(*localeID);
if(variant[i]=='-') {
variant[i]='_';
}
}
i++;
localeID++;
}
}
if(i==0) {
if(prev=='@') {
} else if((localeID=locale_getKeywordsStart(localeID))!=NULL) {
++localeID;
} else {
return 0;
}
while(!_isTerminator(*localeID)) {
if (needSeparator) {
if (i<variantCapacity) {
variant[i] = '_';
}
++i;
needSeparator = FALSE;
}
if(i<variantCapacity) {
variant[i]=(char)uprv_toupper(*localeID);
if(variant[i]=='-' || variant[i]==',') {
variant[i]='_';
}
}
i++;
localeID++;
}
}
return i;
}
static int32_t
_getVariant(const char *localeID,
char prev,
char *variant, int32_t variantCapacity) {
return _getVariantEx(localeID, prev, variant, variantCapacity, FALSE);
}
typedef struct UKeywordsContext {
char* keywords;
char* current;
} UKeywordsContext;
U_CDECL_BEGIN
static void U_CALLCONV
uloc_kw_closeKeywords(UEnumeration *enumerator) {
uprv_free(((UKeywordsContext *)enumerator->context)->keywords);
uprv_free(enumerator->context);
uprv_free(enumerator);
}
static int32_t U_CALLCONV
uloc_kw_countKeywords(UEnumeration *en, UErrorCode * ) {
char *kw = ((UKeywordsContext *)en->context)->keywords;
int32_t result = 0;
while(*kw) {
result++;
kw += uprv_strlen(kw)+1;
}
return result;
}
static const char * U_CALLCONV
uloc_kw_nextKeyword(UEnumeration* en,
int32_t* resultLength,
UErrorCode* ) {
const char* result = ((UKeywordsContext *)en->context)->current;
int32_t len = 0;
if(*result) {
len = (int32_t)uprv_strlen(((UKeywordsContext *)en->context)->current);
((UKeywordsContext *)en->context)->current += len+1;
} else {
result = NULL;
}
if (resultLength) {
*resultLength = len;
}
return result;
}
static void U_CALLCONV
uloc_kw_resetKeywords(UEnumeration* en,
UErrorCode* ) {
((UKeywordsContext *)en->context)->current = ((UKeywordsContext *)en->context)->keywords;
}
U_CDECL_END
static const UEnumeration gKeywordsEnum = {
NULL,
NULL,
uloc_kw_closeKeywords,
uloc_kw_countKeywords,
uenum_unextDefault,
uloc_kw_nextKeyword,
uloc_kw_resetKeywords
};
U_CAPI UEnumeration* U_EXPORT2
uloc_openKeywordList(const char *keywordList, int32_t keywordListSize, UErrorCode* status)
{
LocalMemory<UKeywordsContext> myContext;
LocalMemory<UEnumeration> result;
if (U_FAILURE(*status)) {
return nullptr;
}
myContext.adoptInstead(static_cast<UKeywordsContext *>(uprv_malloc(sizeof(UKeywordsContext))));
result.adoptInstead(static_cast<UEnumeration *>(uprv_malloc(sizeof(UEnumeration))));
if (myContext.isNull() || result.isNull()) {
*status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
uprv_memcpy(result.getAlias(), &gKeywordsEnum, sizeof(UEnumeration));
myContext->keywords = static_cast<char *>(uprv_malloc(keywordListSize+1));
if (myContext->keywords == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
uprv_memcpy(myContext->keywords, keywordList, keywordListSize);
myContext->keywords[keywordListSize] = 0;
myContext->current = myContext->keywords;
result->context = myContext.orphan();
return result.orphan();
}
U_CAPI UEnumeration* U_EXPORT2
uloc_openKeywords(const char* localeID,
UErrorCode* status)
{
int32_t i=0;
char keywords[256];
int32_t keywordsCapacity = 256;
char tempBuffer[ULOC_FULLNAME_CAPACITY];
const char* tmpLocaleID;
if(status==NULL || U_FAILURE(*status)) {
return 0;
}
if (_hasBCP47Extension(localeID)) {
_ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), status);
} else {
if (localeID==NULL) {
localeID=uloc_getDefault();
}
tmpLocaleID=localeID;
}
ulocimp_getLanguage(tmpLocaleID, NULL, 0, &tmpLocaleID);
if(_isIDSeparator(*tmpLocaleID)) {
const char *scriptID;
ulocimp_getScript(tmpLocaleID+1, NULL, 0, &scriptID);
if(scriptID != tmpLocaleID+1) {
tmpLocaleID = scriptID;
}
if (_isIDSeparator(*tmpLocaleID)) {
ulocimp_getCountry(tmpLocaleID+1, NULL, 0, &tmpLocaleID);
if(_isIDSeparator(*tmpLocaleID)) {
_getVariant(tmpLocaleID+1, *tmpLocaleID, NULL, 0);
}
}
}
if((tmpLocaleID = locale_getKeywordsStart(tmpLocaleID)) != NULL) {
i=locale_getKeywords(tmpLocaleID+1, '@', keywords, keywordsCapacity, NULL, 0, NULL, FALSE, status);
}
if(i) {
return uloc_openKeywordList(keywords, i, status);
} else {
return NULL;
}
}
#define _ULOC_STRIP_KEYWORDS 0x2
#define _ULOC_CANONICALIZE 0x1
#define OPTION_SET(options, mask) ((options & mask) != 0)
static const char i_default[] = {'i', '-', 'd', 'e', 'f', 'a', 'u', 'l', 't'};
#define I_DEFAULT_LENGTH UPRV_LENGTHOF(i_default)
static int32_t
_canonicalize(const char* localeID,
char* result,
int32_t resultCapacity,
uint32_t options,
UErrorCode* err) {
int32_t j, len, fieldCount=0, scriptSize=0, variantSize=0, nameCapacity;
char localeBuffer[ULOC_FULLNAME_CAPACITY];
char tempBuffer[ULOC_FULLNAME_CAPACITY];
const char* origLocaleID;
const char* tmpLocaleID;
const char* keywordAssign = NULL;
const char* separatorIndicator = NULL;
char* name;
char* variant = NULL;
if (U_FAILURE(*err)) {
return 0;
}
if (_hasBCP47Extension(localeID)) {
_ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), err);
} else {
if (localeID==NULL) {
localeID=uloc_getDefault();
}
tmpLocaleID=localeID;
}
origLocaleID=tmpLocaleID;
if (
(result == NULL || resultCapacity < (int32_t)sizeof(localeBuffer))) {
name = localeBuffer;
nameCapacity = (int32_t)sizeof(localeBuffer);
} else {
name = result;
nameCapacity = resultCapacity;
}
len=ulocimp_getLanguage(tmpLocaleID, name, nameCapacity, &tmpLocaleID);
if(len == I_DEFAULT_LENGTH && uprv_strncmp(origLocaleID, i_default, len) == 0) {
const char *d = uloc_getDefault();
len = (int32_t)uprv_strlen(d);
if (name != NULL) {
uprv_memcpy(name, d, len);
}
} else if(_isIDSeparator(*tmpLocaleID)) {
const char *scriptID;
++fieldCount;
if(len<nameCapacity) {
name[len]='_';
}
++len;
scriptSize=ulocimp_getScript(tmpLocaleID+1,
(len<nameCapacity ? name+len : NULL), nameCapacity-len, &scriptID);
if(scriptSize > 0) {
tmpLocaleID = scriptID;
++fieldCount;
len+=scriptSize;
if (_isIDSeparator(*tmpLocaleID)) {
if(len<nameCapacity) {
name[len]='_';
}
++len;
}
}
if (_isIDSeparator(*tmpLocaleID)) {
const char *cntryID;
int32_t cntrySize = ulocimp_getCountry(tmpLocaleID+1,
(len<nameCapacity ? name+len : NULL), nameCapacity-len, &cntryID);
if (cntrySize > 0) {
tmpLocaleID = cntryID;
len+=cntrySize;
}
if(_isIDSeparator(*tmpLocaleID)) {
if (cntrySize >= 0 && ! _isIDSeparator(*(tmpLocaleID+1)) ) {
++fieldCount;
if(len<nameCapacity) {
name[len]='_';
}
++len;
}
variantSize = _getVariant(tmpLocaleID+1, *tmpLocaleID,
(len<nameCapacity ? name+len : NULL), nameCapacity-len);
if (variantSize > 0) {
variant = len<nameCapacity ? name+len : NULL;
len += variantSize;
tmpLocaleID += variantSize + 1;
}
}
}
}
if (!OPTION_SET(options, _ULOC_CANONICALIZE) && *tmpLocaleID == '.') {
UBool done = FALSE;
do {
char c = *tmpLocaleID;
switch (c) {
case 0:
case '@':
done = TRUE;
break;
default:
if (len<nameCapacity) {
name[len] = c;
}
++len;
++tmpLocaleID;
break;
}
} while (!done);
}
if ((tmpLocaleID=locale_getKeywordsStart(tmpLocaleID))!=NULL) {
keywordAssign = uprv_strchr(tmpLocaleID, '=');
separatorIndicator = uprv_strchr(tmpLocaleID, ';');
}
if (!OPTION_SET(options, _ULOC_CANONICALIZE) &&
tmpLocaleID != NULL && keywordAssign == NULL) {
for (;;) {
char c = *tmpLocaleID;
if (c == 0) {
break;
}
if (len<nameCapacity) {
name[len] = c;
}
++len;
++tmpLocaleID;
}
}
if (OPTION_SET(options, _ULOC_CANONICALIZE)) {
if (tmpLocaleID!=NULL && keywordAssign==NULL) {
int32_t posixVariantSize;
if (fieldCount < 2 || (fieldCount < 3 && scriptSize > 0)) {
do {
if(len<nameCapacity) {
name[len]='_';
}
++len;
++fieldCount;
} while(fieldCount<2);
}
posixVariantSize = _getVariantEx(tmpLocaleID+1, '@', name+len, nameCapacity-len,
(UBool)(variantSize > 0));
if (posixVariantSize > 0) {
if (variant == NULL) {
variant = name+len;
}
len += posixVariantSize;
variantSize += posixVariantSize;
}
}
for (j=0; j<UPRV_LENGTHOF(CANONICALIZE_MAP); j++) {
const char* id = CANONICALIZE_MAP[j].id;
int32_t n = (int32_t)uprv_strlen(id);
if (len == n && uprv_strncmp(name, id, n) == 0) {
if (n == 0 && tmpLocaleID != NULL) {
break;
}
len = _copyCount(name, nameCapacity, CANONICALIZE_MAP[j].canonicalID);
break;
}
}
}
if (!OPTION_SET(options, _ULOC_STRIP_KEYWORDS)) {
if (tmpLocaleID!=NULL && keywordAssign!=NULL &&
(!separatorIndicator || separatorIndicator > keywordAssign)) {
if(len<nameCapacity) {
name[len]='@';
}
++len;
++fieldCount;
len += _getKeywords(tmpLocaleID+1, '@', (len<nameCapacity ? name+len : NULL), nameCapacity-len,
NULL, 0, NULL, TRUE, err);
}
}
if (U_SUCCESS(*err) && result != NULL && name == localeBuffer) {
uprv_strncpy(result, localeBuffer, (len > resultCapacity) ? resultCapacity : len);
}
return u_terminateChars(result, resultCapacity, len, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getParent(const char* localeID,
char* parent,
int32_t parentCapacity,
UErrorCode* err)
{
const char *lastUnderscore;
int32_t i;
if (U_FAILURE(*err))
return 0;
if (localeID == NULL)
localeID = uloc_getDefault();
lastUnderscore=uprv_strrchr(localeID, '_');
if(lastUnderscore!=NULL) {
i=(int32_t)(lastUnderscore-localeID);
} else {
i=0;
}
if(i>0 && parent != localeID) {
uprv_memcpy(parent, localeID, uprv_min(i, parentCapacity));
}
return u_terminateChars(parent, parentCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getLanguage(const char* localeID,
char* language,
int32_t languageCapacity,
UErrorCode* err)
{
int32_t i=0;
if (err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
i=ulocimp_getLanguage(localeID, language, languageCapacity, NULL);
return u_terminateChars(language, languageCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getScript(const char* localeID,
char* script,
int32_t scriptCapacity,
UErrorCode* err)
{
int32_t i=0;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
ulocimp_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
i=ulocimp_getScript(localeID+1, script, scriptCapacity, NULL);
}
return u_terminateChars(script, scriptCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getCountry(const char* localeID,
char* country,
int32_t countryCapacity,
UErrorCode* err)
{
int32_t i=0;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if(localeID==NULL) {
localeID=uloc_getDefault();
}
ulocimp_getLanguage(localeID, NULL, 0, &localeID);
if(_isIDSeparator(*localeID)) {
const char *scriptID;
ulocimp_getScript(localeID+1, NULL, 0, &scriptID);
if(scriptID != localeID+1) {
localeID = scriptID;
}
if(_isIDSeparator(*localeID)) {
i=ulocimp_getCountry(localeID+1, country, countryCapacity, NULL);
}
}
return u_terminateChars(country, countryCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getVariant(const char* localeID,
char* variant,
int32_t variantCapacity,
UErrorCode* err)
{
char tempBuffer[ULOC_FULLNAME_CAPACITY];
const char* tmpLocaleID;
int32_t i=0;
if(err==NULL || U_FAILURE(*err)) {
return 0;
}
if (_hasBCP47Extension(localeID)) {
_ConvertBCP47(tmpLocaleID, localeID, tempBuffer, sizeof(tempBuffer), err);
} else {
if (localeID==NULL) {
localeID=uloc_getDefault();
}
tmpLocaleID=localeID;
}
ulocimp_getLanguage(tmpLocaleID, NULL, 0, &tmpLocaleID);
if(_isIDSeparator(*tmpLocaleID)) {
const char *scriptID;
ulocimp_getScript(tmpLocaleID+1, NULL, 0, &scriptID);
if(scriptID != tmpLocaleID+1) {
tmpLocaleID = scriptID;
}
if (_isIDSeparator(*tmpLocaleID)) {
const char *cntryID;
ulocimp_getCountry(tmpLocaleID+1, NULL, 0, &cntryID);
if (cntryID != tmpLocaleID+1) {
tmpLocaleID = cntryID;
}
if(_isIDSeparator(*tmpLocaleID)) {
if (tmpLocaleID != cntryID && _isIDSeparator(tmpLocaleID[1])) {
tmpLocaleID++;
}
i=_getVariant(tmpLocaleID+1, *tmpLocaleID, variant, variantCapacity);
}
}
}
return u_terminateChars(variant, variantCapacity, i, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getName(const char* localeID,
char* name,
int32_t nameCapacity,
UErrorCode* err)
{
return _canonicalize(localeID, name, nameCapacity, 0, err);
}
U_CAPI int32_t U_EXPORT2
uloc_getBaseName(const char* localeID,
char* name,
int32_t nameCapacity,
UErrorCode* err)
{
return _canonicalize(localeID, name, nameCapacity, _ULOC_STRIP_KEYWORDS, err);
}
U_CAPI int32_t U_EXPORT2
uloc_canonicalize(const char* localeID,
char* name,
int32_t nameCapacity,
UErrorCode* err)
{
return _canonicalize(localeID, name, nameCapacity, _ULOC_CANONICALIZE, err);
}
U_CAPI const char* U_EXPORT2
uloc_getISO3Language(const char* localeID)
{
int16_t offset;
char lang[ULOC_LANG_CAPACITY];
UErrorCode err = U_ZERO_ERROR;
if (localeID == NULL)
{
localeID = uloc_getDefault();
}
uloc_getLanguage(localeID, lang, ULOC_LANG_CAPACITY, &err);
if (U_FAILURE(err))
return "";
offset = _findIndex(LANGUAGES, lang);
if (offset < 0)
return "";
return LANGUAGES_3[offset];
}
U_CAPI const char* U_EXPORT2
uloc_getISO3Country(const char* localeID)
{
int16_t offset;
char cntry[ULOC_LANG_CAPACITY];
UErrorCode err = U_ZERO_ERROR;
if (localeID == NULL)
{
localeID = uloc_getDefault();
}
uloc_getCountry(localeID, cntry, ULOC_LANG_CAPACITY, &err);
if (U_FAILURE(err))
return "";
offset = _findIndex(COUNTRIES, cntry);
if (offset < 0)
return "";
return COUNTRIES_3[offset];
}
U_CAPI uint32_t U_EXPORT2
uloc_getLCID(const char* localeID)
{
UErrorCode status = U_ZERO_ERROR;
char langID[ULOC_FULLNAME_CAPACITY];
uint32_t lcid = 0;
if (!localeID || uprv_strlen(localeID) < 2) {
return 0;
}
lcid = uprv_convertToLCIDPlatform(localeID, &status);
if (U_FAILURE(status)) {
return 0;
}
if (lcid > 0) {
return lcid;
}
uloc_getLanguage(localeID, langID, sizeof(langID), &status);
if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
return 0;
}
if (uprv_strchr(localeID, '@')) {
int32_t len;
char collVal[ULOC_KEYWORDS_CAPACITY];
char tmpLocaleID[ULOC_FULLNAME_CAPACITY];
len = uloc_getKeywordValue(localeID, "collation", collVal,
UPRV_LENGTHOF(collVal) - 1, &status);
if (U_SUCCESS(status) && len > 0) {
collVal[len] = 0;
len = uloc_getBaseName(localeID, tmpLocaleID,
UPRV_LENGTHOF(tmpLocaleID) - 1, &status);
if (U_SUCCESS(status) && len > 0) {
tmpLocaleID[len] = 0;
len = uloc_setKeywordValue("collation", collVal, tmpLocaleID,
UPRV_LENGTHOF(tmpLocaleID) - len - 1, &status);
if (U_SUCCESS(status) && len > 0) {
tmpLocaleID[len] = 0;
return uprv_convertToLCID(langID, tmpLocaleID, &status);
}
}
}
status = U_ZERO_ERROR;
}
return uprv_convertToLCID(langID, localeID, &status);
}
U_CAPI int32_t U_EXPORT2
uloc_getLocaleForLCID(uint32_t hostid, char *locale, int32_t localeCapacity,
UErrorCode *status)
{
return uprv_convertToPosix(hostid, locale, localeCapacity, status);
}
U_CAPI const char* U_EXPORT2
uloc_getDefault()
{
return locale_get_default();
}
U_CAPI void U_EXPORT2
uloc_setDefault(const char* newDefaultLocale,
UErrorCode* err)
{
if (U_FAILURE(*err))
return;
locale_set_default(newDefaultLocale);
}
U_CAPI const char* const* U_EXPORT2
uloc_getISOLanguages()
{
return LANGUAGES;
}
U_CAPI const char* const* U_EXPORT2
uloc_getISOCountries()
{
return COUNTRIES;
}
static char gDecimal = 0;
static
double
_uloc_strtod(const char *start, char **end) {
char *decimal;
char *myEnd;
char buf[30];
double rv;
if (!gDecimal) {
char rep[5];
sprintf(rep, "%+1.1f", 1.0);
gDecimal = rep[2];
}
if(gDecimal == '.') {
return uprv_strtod(start, end);
} else {
uprv_strncpy(buf, start, 29);
buf[29]=0;
decimal = uprv_strchr(buf, '.');
if(decimal) {
*decimal = gDecimal;
} else {
return uprv_strtod(start, end);
}
rv = uprv_strtod(buf, &myEnd);
if(end) {
*end = (char*)(start+(myEnd-buf));
}
return rv;
}
}
typedef struct {
float q;
int32_t dummy;
char locale[ULOC_FULLNAME_CAPACITY+1];
} _acceptLangItem;
static int32_t U_CALLCONV
uloc_acceptLanguageCompare(const void * , const void *a, const void *b)
{
const _acceptLangItem *aa = (const _acceptLangItem*)a;
const _acceptLangItem *bb = (const _acceptLangItem*)b;
int32_t rc = 0;
if(bb->q < aa->q) {
rc = -1;
} else if(bb->q > aa->q) {
rc = 1;
} else {
rc = 0;
}
if(rc==0) {
rc = uprv_stricmp(aa->locale, bb->locale);
}
#if defined(ULOC_DEBUG)
#endif
return rc;
}
U_CAPI int32_t U_EXPORT2
uloc_acceptLanguageFromHTTP(char *result, int32_t resultAvailable, UAcceptResult *outResult,
const char *httpAcceptLanguage,
UEnumeration* availableLocales,
UErrorCode *status)
{
MaybeStackArray<_acceptLangItem, 4> items; char tmp[ULOC_FULLNAME_CAPACITY +1];
int32_t n = 0;
const char *itemEnd;
const char *paramEnd;
const char *s;
const char *t;
int32_t res;
int32_t i;
int32_t l = (int32_t)uprv_strlen(httpAcceptLanguage);
if(U_FAILURE(*status)) {
return -1;
}
for(s=httpAcceptLanguage;s&&*s;) {
while(isspace(*s))
s++;
itemEnd=uprv_strchr(s,',');
paramEnd=uprv_strchr(s,';');
if(!itemEnd) {
itemEnd = httpAcceptLanguage+l;
}
if(paramEnd && paramEnd<itemEnd) {
t = paramEnd+1;
if(*t=='q') {
t++;
}
while(isspace(*t)) {
t++;
}
if(*t=='=') {
t++;
}
while(isspace(*t)) {
t++;
}
items[n].q = (float)_uloc_strtod(t,NULL);
} else {
items[n].q = 1.0f;
paramEnd = itemEnd;
}
items[n].dummy=0;
for(t=(paramEnd-1);(paramEnd>s)&&isspace(*t);t--)
;
int32_t slen = static_cast<int32_t>(((t+1)-s));
if(slen > ULOC_FULLNAME_CAPACITY) {
*status = U_BUFFER_OVERFLOW_ERROR;
return -1; }
uprv_strncpy(items[n].locale, s, slen);
items[n].locale[slen]=0; int32_t clen = uloc_canonicalize(items[n].locale, tmp, UPRV_LENGTHOF(tmp)-1, status);
if(U_FAILURE(*status)) return -1;
if((clen!=slen) || (uprv_strncmp(items[n].locale, tmp, slen))) {
uprv_strncpy(items[n].locale, tmp, clen);
items[n].locale[clen] = 0; }
#if defined(ULOC_DEBUG)
#endif
n++;
s = itemEnd;
while(*s==',') {
s++;
}
if(n>=items.getCapacity()) { if(NULL == items.resize(items.getCapacity()*2, items.getCapacity())) {
*status = U_MEMORY_ALLOCATION_ERROR;
return -1;
}
#if defined(ULOC_DEBUG)
fprintf(stderr,"malloced at size %d\n", items.getCapacity());
#endif
}
}
uprv_sortArray(items.getAlias(), n, sizeof(items[0]), uloc_acceptLanguageCompare, NULL, TRUE, status);
if (U_FAILURE(*status)) {
return -1;
}
LocalMemory<const char*> strs(NULL);
if (strs.allocateInsteadAndReset(n) == NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return -1;
}
for(i=0;i<n;i++) {
#if defined(ULOC_DEBUG)
#endif
strs[i]=items[i].locale;
}
res = uloc_acceptLanguage(result, resultAvailable, outResult,
strs.getAlias(), n, availableLocales, status);
return res;
}
U_CAPI int32_t U_EXPORT2
uloc_acceptLanguage(char *result, int32_t resultAvailable,
UAcceptResult *outResult, const char **acceptList,
int32_t acceptListCount,
UEnumeration* availableLocales,
UErrorCode *status)
{
int32_t i,j;
int32_t len;
int32_t maxLen=0;
char tmp[ULOC_FULLNAME_CAPACITY+1];
const char *l;
char **fallbackList;
if(U_FAILURE(*status)) {
return -1;
}
fallbackList = static_cast<char **>(uprv_malloc((size_t)(sizeof(fallbackList[0])*acceptListCount)));
if(fallbackList==NULL) {
*status = U_MEMORY_ALLOCATION_ERROR;
return -1;
}
for(i=0;i<acceptListCount;i++) {
#if defined(ULOC_DEBUG)
fprintf(stderr,"%02d: %s\n", i, acceptList[i]);
#endif
while((l=uenum_next(availableLocales, NULL, status)) != NULL) {
#if defined(ULOC_DEBUG)
fprintf(stderr," %s\n", l);
#endif
len = (int32_t)uprv_strlen(l);
if(!uprv_strcmp(acceptList[i], l)) {
if(outResult) {
*outResult = ULOC_ACCEPT_VALID;
}
#if defined(ULOC_DEBUG)
fprintf(stderr, "MATCH! %s\n", l);
#endif
if(len>0) {
uprv_strncpy(result, l, uprv_min(len, resultAvailable));
}
for(j=0;j<i;j++) {
uprv_free(fallbackList[j]);
}
uprv_free(fallbackList);
return u_terminateChars(result, resultAvailable, len, status);
}
if(len>maxLen) {
maxLen = len;
}
}
uenum_reset(availableLocales, status);
if(uloc_getParent(acceptList[i], tmp, UPRV_LENGTHOF(tmp), status)!=0) {
fallbackList[i] = uprv_strdup(tmp);
} else {
fallbackList[i]=0;
}
}
for(maxLen--;maxLen>0;maxLen--) {
for(i=0;i<acceptListCount;i++) {
if(fallbackList[i] && ((int32_t)uprv_strlen(fallbackList[i])==maxLen)) {
#if defined(ULOC_DEBUG)
fprintf(stderr,"Try: [%s]", fallbackList[i]);
#endif
while((l=uenum_next(availableLocales, NULL, status)) != NULL) {
#if defined(ULOC_DEBUG)
fprintf(stderr," %s\n", l);
#endif
len = (int32_t)uprv_strlen(l);
if(!uprv_strcmp(fallbackList[i], l)) {
if(outResult) {
*outResult = ULOC_ACCEPT_FALLBACK;
}
#if defined(ULOC_DEBUG)
fprintf(stderr, "fallback MATCH! %s\n", l);
#endif
if(len>0) {
uprv_strncpy(result, l, uprv_min(len, resultAvailable));
}
for(j=0;j<acceptListCount;j++) {
uprv_free(fallbackList[j]);
}
uprv_free(fallbackList);
return u_terminateChars(result, resultAvailable, len, status);
}
}
uenum_reset(availableLocales, status);
if(uloc_getParent(fallbackList[i], tmp, UPRV_LENGTHOF(tmp), status)!=0) {
uprv_free(fallbackList[i]);
fallbackList[i] = uprv_strdup(tmp);
} else {
uprv_free(fallbackList[i]);
fallbackList[i]=0;
}
}
}
if(outResult) {
*outResult = ULOC_ACCEPT_FAILED;
}
}
for(i=0;i<acceptListCount;i++) {
uprv_free(fallbackList[i]);
}
uprv_free(fallbackList);
return -1;
}
U_CAPI const char* U_EXPORT2
uloc_toUnicodeLocaleKey(const char* keyword)
{
const char* bcpKey = ulocimp_toBcpKey(keyword);
if (bcpKey == NULL && ultag_isUnicodeLocaleKey(keyword, -1)) {
return keyword;
}
return bcpKey;
}
U_CAPI const char* U_EXPORT2
uloc_toUnicodeLocaleType(const char* keyword, const char* value)
{
const char* bcpType = ulocimp_toBcpType(keyword, value, NULL, NULL);
if (bcpType == NULL && ultag_isUnicodeLocaleType(value, -1)) {
return value;
}
return bcpType;
}
static UBool
isWellFormedLegacyKey(const char* legacyKey)
{
const char* p = legacyKey;
while (*p) {
if (!UPRV_ISALPHANUM(*p)) {
return FALSE;
}
p++;
}
return TRUE;
}
static UBool
isWellFormedLegacyType(const char* legacyType)
{
const char* p = legacyType;
int32_t alphaNumLen = 0;
while (*p) {
if (*p == '_' || *p == '/' || *p == '-') {
if (alphaNumLen == 0) {
return FALSE;
}
alphaNumLen = 0;
} else if (UPRV_ISALPHANUM(*p)) {
alphaNumLen++;
} else {
return FALSE;
}
p++;
}
return (alphaNumLen != 0);
}
U_CAPI const char* U_EXPORT2
uloc_toLegacyKey(const char* keyword)
{
const char* legacyKey = ulocimp_toLegacyKey(keyword);
if (legacyKey == NULL) {
if (isWellFormedLegacyKey(keyword)) {
return keyword;
}
}
return legacyKey;
}
U_CAPI const char* U_EXPORT2
uloc_toLegacyType(const char* keyword, const char* value)
{
const char* legacyType = ulocimp_toLegacyType(keyword, value, NULL, NULL);
if (legacyType == NULL) {
if (isWellFormedLegacyType(value)) {
return value;
}
}
return legacyType;
}