1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2009-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
10 #include "unicode/bytestream.h"
11 #include "unicode/utypes.h"
12 #include "unicode/ures.h"
13 #include "unicode/localpointer.h"
14 #include "unicode/putil.h"
15 #include "unicode/uenum.h"
16 #include "unicode/uloc.h"
27 /* struct holding a single variant */
28 typedef struct VariantListEntry
{
30 struct VariantListEntry
*next
;
33 /* struct holding a single attribute value */
34 struct AttributeListEntry
: public icu::UMemory
{
35 const char *attribute
;
36 struct AttributeListEntry
*next
;
39 /* struct holding a single extension */
40 struct ExtensionListEntry
: public icu::UMemory
{
43 struct ExtensionListEntry
*next
;
47 typedef struct ULanguageTag
{
48 char *buf
; /* holding parsed subtags */
50 const char *extlang
[MAXEXTLANG
];
53 VariantListEntry
*variants
;
54 ExtensionListEntry
*extensions
;
55 const char *privateuse
;
56 const char *grandfathered
;
61 #define PRIVATEUSE 'x'
64 #define LOCALE_SEP '_'
65 #define LOCALE_EXT_SEP '@'
66 #define LOCALE_KEYWORD_SEP ';'
67 #define LOCALE_KEY_TYPE_SEP '='
69 #define ISALPHA(c) uprv_isASCIILetter(c)
70 #define ISNUMERIC(c) ((c)>='0' && (c)<='9')
72 static const char EMPTY
[] = "";
73 static const char LANG_UND
[] = "und";
74 static const char PRIVATEUSE_KEY
[] = "x";
75 static const char _POSIX
[] = "_POSIX";
76 static const char POSIX_KEY
[] = "va";
77 static const char POSIX_VALUE
[] = "posix";
78 static const char LOCALE_ATTRIBUTE_KEY
[] = "attribute";
79 static const char PRIVUSE_VARIANT_PREFIX
[] = "lvariant";
80 static const char LOCALE_TYPE_YES
[] = "yes";
82 #define LANG_UND_LEN 3
85 Updated on 2018-09-12 from
86 https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry .
88 This table has 2 parts. The parts for Grandfathered tags is generated by the
89 following scripts from the IANA language tag registry.
91 curl https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry |\
92 egrep -A 7 'Type: grandfathered' | \
93 egrep 'Tag|Prefe' | grep -B1 'Preferred' | grep -v '^--' | \
94 awk -n '/Tag/ {printf(" \"%s\", ", $2);} /Preferred/ {printf("\"%s\",\n", $2);}' |\
98 The 2nd part is made of five ICU-specific entries. They're kept for
99 the backward compatibility for now, even though there are no preferred
100 values. They may have to be removed for the strict BCP 47 compliance.
103 static const char* const GRANDFATHERED
[] = {
104 /* grandfathered preferred */
106 "en-gb-oed", "en-gb-oxendict",
127 // Grandfathered tags with no preferred value in the IANA
128 // registry. Kept for now for the backward compatibility
129 // because ICU has mapped them this way.
130 "cel-gaulish", "xtg-x-cel-gaulish",
131 "i-default", "en-x-i-default",
132 "i-enochian", "und-x-i-enochian",
133 "i-mingo", "see-x-i-mingo",
134 "zh-min", "nan-x-zh-min",
138 Updated on 2018-09-12 from
139 https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry .
141 The table lists redundant tags with preferred value in the IANA languate tag registry.
142 It's generated with the following command:
144 curl https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry |\
145 grep 'Type: redundant' -A 5 | egrep '^(Tag:|Prefer)' | grep -B1 'Preferred' | \
146 awk -n '/Tag/ {printf(" \"%s\", ", $2);} /Preferred/ {printf("\"%s\",\n", $2);}' | \
149 In addition, ja-latn-hepburn-heploc is mapped to ja-latn-alalc97 because
150 a variant tag 'hepburn-heploc' has the preferred subtag, 'alaic97'.
153 static const char* const REDUNDANT
[] = {
154 // redundant preferred
175 "zh-cmn-hans", "cmn-hans",
176 "zh-cmn-hant", "cmn-hant",
181 // variant tag with preferred value
182 "ja-latn-hepburn-heploc", "ja-latn-alalc97",
186 Updated on 2018-09-12 from
187 https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry .
189 grep 'Type: language' -A 7 language-subtag-registry | egrep 'Subtag|Prefe' | \
190 grep -B1 'Preferred' | grep -v '^--' | \
191 awk -n '/Subtag/ {printf(" \"%s\", ", $2);} /Preferred/ {printf("\"%s\",\n", $2);}'
193 Make sure that 2-letter language subtags come before 3-letter subtags.
195 static const char DEPRECATEDLANGS
[][4] = {
278 Updated on 2018-04-24 from
280 curl https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry | \
281 grep 'Type: region' -A 7 | egrep 'Subtag|Prefe' | \
282 grep -B1 'Preferred' | \
283 awk -n '/Subtag/ {printf(" \"%s\", ", $2);} /Preferred/ {printf("\"%s\",\n", $2);}'
285 static const char DEPRECATEDREGIONS
[][3] = {
296 * -------------------------------------------------
298 * These ultag_ functions may be exposed as APIs later
300 * -------------------------------------------------
304 ultag_parse(const char* tag
, int32_t tagLen
, int32_t* parsedLen
, UErrorCode
* status
);
307 ultag_close(ULanguageTag
* langtag
);
310 ultag_getLanguage(const ULanguageTag
* langtag
);
314 ultag_getJDKLanguage(const ULanguageTag
* langtag
);
318 ultag_getExtlang(const ULanguageTag
* langtag
, int32_t idx
);
321 ultag_getExtlangSize(const ULanguageTag
* langtag
);
324 ultag_getScript(const ULanguageTag
* langtag
);
327 ultag_getRegion(const ULanguageTag
* langtag
);
330 ultag_getVariant(const ULanguageTag
* langtag
, int32_t idx
);
333 ultag_getVariantsSize(const ULanguageTag
* langtag
);
336 ultag_getExtensionKey(const ULanguageTag
* langtag
, int32_t idx
);
339 ultag_getExtensionValue(const ULanguageTag
* langtag
, int32_t idx
);
342 ultag_getExtensionsSize(const ULanguageTag
* langtag
);
345 ultag_getPrivateUse(const ULanguageTag
* langtag
);
349 ultag_getGrandfathered(const ULanguageTag
* langtag
);
355 * \class LocalULanguageTagPointer
356 * "Smart pointer" class, closes a ULanguageTag via ultag_close().
357 * For most methods see the LocalPointerBase base class.
359 * @see LocalPointerBase
363 U_DEFINE_LOCAL_OPEN_POINTER(LocalULanguageTagPointer
, ULanguageTag
, ultag_close
);
368 * -------------------------------------------------
370 * Language subtag syntax validation functions
372 * -------------------------------------------------
376 _isAlphaString(const char* s
, int32_t len
) {
378 for (i
= 0; i
< len
; i
++) {
379 if (!ISALPHA(*(s
+ i
))) {
387 _isNumericString(const char* s
, int32_t len
) {
389 for (i
= 0; i
< len
; i
++) {
390 if (!ISNUMERIC(*(s
+ i
))) {
398 _isAlphaNumericString(const char* s
, int32_t len
) {
400 for (i
= 0; i
< len
; i
++) {
401 if (!ISALPHA(*(s
+ i
)) && !ISNUMERIC(*(s
+ i
))) {
409 _isAlphaNumericStringLimitedLength(const char* s
, int32_t len
, int32_t min
, int32_t max
) {
411 len
= (int32_t)uprv_strlen(s
);
413 if (len
>= min
&& len
<= max
&& _isAlphaNumericString(s
, len
)) {
420 ultag_isLanguageSubtag(const char* s
, int32_t len
) {
422 * unicode_language_subtag = alpha{2,3} | alpha{5,8};
423 * NOTE: Per ICUTC 2019/01/23- accepting alpha 4
427 len
= (int32_t)uprv_strlen(s
);
429 if (len
>= 2 && len
<= 8 && _isAlphaString(s
, len
)) {
436 _isExtlangSubtag(const char* s
, int32_t len
) {
438 * extlang = 3ALPHA ; selected ISO 639 codes
439 * *2("-" 3ALPHA) ; permanently reserved
442 len
= (int32_t)uprv_strlen(s
);
444 if (len
== 3 && _isAlphaString(s
, len
)) {
451 ultag_isScriptSubtag(const char* s
, int32_t len
) {
453 * script = 4ALPHA ; ISO 15924 code
456 len
= (int32_t)uprv_strlen(s
);
458 if (len
== 4 && _isAlphaString(s
, len
)) {
465 ultag_isRegionSubtag(const char* s
, int32_t len
) {
467 * region = 2ALPHA ; ISO 3166-1 code
468 * / 3DIGIT ; UN M.49 code
471 len
= (int32_t)uprv_strlen(s
);
473 if (len
== 2 && _isAlphaString(s
, len
)) {
476 if (len
== 3 && _isNumericString(s
, len
)) {
483 _isVariantSubtag(const char* s
, int32_t len
) {
485 * variant = 5*8alphanum ; registered variants
486 * / (DIGIT 3alphanum)
489 len
= (int32_t)uprv_strlen(s
);
491 if (_isAlphaNumericStringLimitedLength(s
, len
, 5, 8)) {
494 if (len
== 4 && ISNUMERIC(*s
) && _isAlphaNumericString(s
+ 1, 3)) {
501 _isSepListOf(UBool (*test
)(const char*, int32_t), const char* s
, int32_t len
) {
503 const char *pSubtag
= NULL
;
506 len
= (int32_t)uprv_strlen(s
);
509 while ((p
- s
) < len
) {
511 if (pSubtag
== NULL
) {
514 if (!test(pSubtag
, (int32_t)(p
- pSubtag
))) {
518 } else if (pSubtag
== NULL
) {
523 if (pSubtag
== NULL
) {
526 return test(pSubtag
, (int32_t)(p
- pSubtag
));
530 ultag_isVariantSubtags(const char* s
, int32_t len
) {
531 return _isSepListOf(&_isVariantSubtag
, s
, len
);
534 // This is for the ICU-specific "lvariant" handling.
536 _isPrivateuseVariantSubtag(const char* s
, int32_t len
) {
538 * variant = 1*8alphanum ; registered variants
539 * / (DIGIT 3alphanum)
541 return _isAlphaNumericStringLimitedLength(s
, len
, 1, 8);
545 _isExtensionSingleton(const char* s
, int32_t len
) {
547 * extension = singleton 1*("-" (2*8alphanum))
549 * singleton = DIGIT ; 0 - 9
556 len
= (int32_t)uprv_strlen(s
);
558 if (len
== 1 && (ISALPHA(*s
) || ISNUMERIC(*s
)) && (uprv_tolower(*s
) != PRIVATEUSE
)) {
565 _isExtensionSubtag(const char* s
, int32_t len
) {
567 * extension = singleton 1*("-" (2*8alphanum))
569 return _isAlphaNumericStringLimitedLength(s
, len
, 2, 8);
573 ultag_isExtensionSubtags(const char* s
, int32_t len
) {
574 return _isSepListOf(&_isExtensionSubtag
, s
, len
);
578 _isPrivateuseValueSubtag(const char* s
, int32_t len
) {
580 * privateuse = "x" 1*("-" (1*8alphanum))
582 return _isAlphaNumericStringLimitedLength(s
, len
, 1, 8);
586 ultag_isPrivateuseValueSubtags(const char* s
, int32_t len
) {
587 return _isSepListOf(&_isPrivateuseValueSubtag
, s
, len
);
591 ultag_isUnicodeLocaleAttribute(const char* s
, int32_t len
) {
593 * attribute = alphanum{3,8} ;
595 return _isAlphaNumericStringLimitedLength(s
, len
, 3, 8);
599 ultag_isUnicodeLocaleAttributes(const char* s
, int32_t len
) {
600 return _isSepListOf(&ultag_isUnicodeLocaleAttribute
, s
, len
);
604 ultag_isUnicodeLocaleKey(const char* s
, int32_t len
) {
606 * key = alphanum alpha ;
609 len
= (int32_t)uprv_strlen(s
);
611 if (len
== 2 && (ISALPHA(*s
) || ISNUMERIC(*s
)) && ISALPHA(s
[1])) {
618 _isUnicodeLocaleTypeSubtag(const char*s
, int32_t len
) {
622 return _isAlphaNumericStringLimitedLength(s
, len
, 3, 8);
626 ultag_isUnicodeLocaleType(const char*s
, int32_t len
) {
628 * type = alphanum{3,8} (sep alphanum{3,8})* ;
630 return _isSepListOf(&_isUnicodeLocaleTypeSubtag
, s
, len
);
634 _isTKey(const char* s
, int32_t len
)
637 * tkey = alpha digit ;
640 len
= (int32_t)uprv_strlen(s
);
642 if (len
== 2 && ISALPHA(*s
) && ISNUMERIC(*(s
+ 1))) {
649 _isTValue(const char* s
, int32_t len
)
652 * tvalue = (sep alphanum{3,8})+ ;
654 return _isAlphaNumericStringLimitedLength(s
, len
, 3, 8);
658 _isTransformedExtensionSubtag(int32_t& state
, const char* s
, int32_t len
)
660 const int32_t kStart
= 0; // Start, wait for unicode_language_subtag, tkey or end
661 const int32_t kGotLanguage
= 1; // Got unicode_language_subtag, wait for unicode_script_subtag,
662 // unicode_region_subtag, unicode_variant_subtag, tkey or end
663 const int32_t kGotScript
= 2; // Got unicode_script_subtag, wait for unicode_region_subtag,
664 // unicode_variant_subtag, tkey, or end
665 const int32_t kGotRegion
= 3; // Got unicode_region_subtag, wait for unicode_variant_subtag,
667 const int32_t kGotVariant
= 4; // Got unicode_variant_subtag, wait for unicode_variant_subtag
669 const int32_t kGotTKey
= -1; // Got tkey, wait for tvalue. ERROR if stop here.
670 const int32_t kGotTValue
= 6; // Got tvalue, wait for tkey, tvalue or end
674 if (ultag_isLanguageSubtag(s
, len
)) {
675 state
= kGotLanguage
;
678 if (_isTKey(s
, len
)) {
684 if (ultag_isScriptSubtag(s
, len
)) {
690 if (ultag_isRegionSubtag(s
, len
)) {
698 if (_isVariantSubtag(s
, len
)) {
702 if (_isTKey(s
, len
)) {
708 if (_isTValue(s
, len
)) {
714 if (_isTKey(s
, len
)) {
718 if (_isTValue(s
, len
)) {
727 _isUnicodeExtensionSubtag(int32_t& state
, const char* s
, int32_t len
)
729 const int32_t kStart
= 0; // Start, wait for a key or attribute or end
730 const int32_t kGotKey
= 1; // Got a key, wait for type or key or end
731 const int32_t kGotType
= 2; // Got a type, wait for key or end
735 if (ultag_isUnicodeLocaleKey(s
, len
)) {
739 if (ultag_isUnicodeLocaleAttribute(s
, len
)) {
744 if (ultag_isUnicodeLocaleKey(s
, len
)) {
747 if (_isUnicodeLocaleTypeSubtag(s
, len
)) {
753 if (ultag_isUnicodeLocaleKey(s
, len
)) {
757 if (_isUnicodeLocaleTypeSubtag(s
, len
)) {
766 _isStatefulSepListOf(UBool (*test
)(int32_t&, const char*, int32_t), const char* s
, int32_t len
)
770 const char* start
= s
;
771 int32_t subtagLen
= 0;
774 len
= (int32_t)uprv_strlen(s
);
777 for (p
= s
; len
> 0; p
++, len
--) {
779 if (!test(state
, start
, subtagLen
)) {
789 if (test(state
, start
, subtagLen
) && state
>= 0) {
796 ultag_isTransformedExtensionSubtags(const char* s
, int32_t len
)
798 return _isStatefulSepListOf(&_isTransformedExtensionSubtag
, s
, len
);
802 ultag_isUnicodeExtensionSubtags(const char* s
, int32_t len
) {
803 return _isStatefulSepListOf(&_isUnicodeExtensionSubtag
, s
, len
);
808 * -------------------------------------------------
812 * -------------------------------------------------
816 _addVariantToList(VariantListEntry
**first
, VariantListEntry
*var
) {
819 if (*first
== NULL
) {
823 VariantListEntry
*prev
, *cur
;
826 /* variants order should be preserved */
836 /* Checking for duplicate variant */
837 cmp
= uprv_compareInvCharsAsAscii(var
->variant
, cur
->variant
);
839 /* duplicated variant */
852 _addAttributeToList(AttributeListEntry
**first
, AttributeListEntry
*attr
) {
855 if (*first
== NULL
) {
859 AttributeListEntry
*prev
, *cur
;
862 /* reorder variants in alphabetical order */
871 cmp
= uprv_compareInvCharsAsAscii(attr
->attribute
, cur
->attribute
);
882 /* duplicated variant */
896 _addExtensionToList(ExtensionListEntry
**first
, ExtensionListEntry
*ext
, UBool localeToBCP
) {
899 if (*first
== NULL
) {
903 ExtensionListEntry
*prev
, *cur
;
906 /* reorder variants in alphabetical order */
916 /* special handling for locale to bcp conversion */
919 len
= (int32_t)uprv_strlen(ext
->key
);
920 curlen
= (int32_t)uprv_strlen(cur
->key
);
922 if (len
== 1 && curlen
== 1) {
923 if (*(ext
->key
) == *(cur
->key
)) {
925 } else if (*(ext
->key
) == PRIVATEUSE
) {
927 } else if (*(cur
->key
) == PRIVATEUSE
) {
930 cmp
= *(ext
->key
) - *(cur
->key
);
932 } else if (len
== 1) {
933 cmp
= *(ext
->key
) - LDMLEXT
;
934 } else if (curlen
== 1) {
935 cmp
= LDMLEXT
- *(cur
->key
);
937 cmp
= uprv_compareInvCharsAsAscii(ext
->key
, cur
->key
);
938 /* Both are u extension keys - we need special handling for 'attribute' */
940 if (uprv_strcmp(cur
->key
, LOCALE_ATTRIBUTE_KEY
) == 0) {
942 } else if (uprv_strcmp(ext
->key
, LOCALE_ATTRIBUTE_KEY
) == 0) {
948 cmp
= uprv_compareInvCharsAsAscii(ext
->key
, cur
->key
);
960 /* duplicated extension key */
973 _initializeULanguageTag(ULanguageTag
* langtag
) {
978 langtag
->language
= EMPTY
;
979 for (i
= 0; i
< MAXEXTLANG
; i
++) {
980 langtag
->extlang
[i
] = NULL
;
983 langtag
->script
= EMPTY
;
984 langtag
->region
= EMPTY
;
986 langtag
->variants
= NULL
;
987 langtag
->extensions
= NULL
;
989 langtag
->grandfathered
= EMPTY
;
990 langtag
->privateuse
= EMPTY
;
994 _appendLanguageToLanguageTag(const char* localeID
, icu::ByteSink
& sink
, UBool strict
, UErrorCode
* status
) {
995 char buf
[ULOC_LANG_CAPACITY
];
996 UErrorCode tmpStatus
= U_ZERO_ERROR
;
999 if (U_FAILURE(*status
)) {
1003 len
= uloc_getLanguage(localeID
, buf
, sizeof(buf
), &tmpStatus
);
1004 if (U_FAILURE(tmpStatus
) || tmpStatus
== U_STRING_NOT_TERMINATED_WARNING
) {
1006 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1012 /* Note: returned language code is in lower case letters */
1015 sink
.Append(LANG_UND
, LANG_UND_LEN
);
1016 } else if (!ultag_isLanguageSubtag(buf
, len
)) {
1017 /* invalid language code */
1019 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1022 sink
.Append(LANG_UND
, LANG_UND_LEN
);
1024 /* resolve deprecated */
1025 for (i
= 0; i
< UPRV_LENGTHOF(DEPRECATEDLANGS
); i
+= 2) {
1026 // 2-letter deprecated subtags are listede before 3-letter
1027 // ones in DEPRECATEDLANGS[]. Get out of loop on coming
1028 // across the 1st 3-letter subtag, if the input is a 2-letter code.
1029 // to avoid continuing to try when there's no match.
1030 if (uprv_strlen(buf
) < uprv_strlen(DEPRECATEDLANGS
[i
])) break;
1031 if (uprv_compareInvCharsAsAscii(buf
, DEPRECATEDLANGS
[i
]) == 0) {
1032 uprv_strcpy(buf
, DEPRECATEDLANGS
[i
+ 1]);
1033 len
= (int32_t)uprv_strlen(buf
);
1037 sink
.Append(buf
, len
);
1042 _appendScriptToLanguageTag(const char* localeID
, icu::ByteSink
& sink
, UBool strict
, UErrorCode
* status
) {
1043 char buf
[ULOC_SCRIPT_CAPACITY
];
1044 UErrorCode tmpStatus
= U_ZERO_ERROR
;
1047 if (U_FAILURE(*status
)) {
1051 len
= uloc_getScript(localeID
, buf
, sizeof(buf
), &tmpStatus
);
1052 if (U_FAILURE(tmpStatus
) || tmpStatus
== U_STRING_NOT_TERMINATED_WARNING
) {
1054 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1060 if (!ultag_isScriptSubtag(buf
, len
)) {
1061 /* invalid script code */
1063 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1067 sink
.Append("-", 1);
1068 sink
.Append(buf
, len
);
1074 _appendRegionToLanguageTag(const char* localeID
, icu::ByteSink
& sink
, UBool strict
, UErrorCode
* status
) {
1075 char buf
[ULOC_COUNTRY_CAPACITY
];
1076 UErrorCode tmpStatus
= U_ZERO_ERROR
;
1079 if (U_FAILURE(*status
)) {
1083 len
= uloc_getCountry(localeID
, buf
, sizeof(buf
), &tmpStatus
);
1084 if (U_FAILURE(tmpStatus
) || tmpStatus
== U_STRING_NOT_TERMINATED_WARNING
) {
1086 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1092 if (!ultag_isRegionSubtag(buf
, len
)) {
1093 /* invalid region code */
1095 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1099 sink
.Append("-", 1);
1100 /* resolve deprecated */
1101 for (int i
= 0; i
< UPRV_LENGTHOF(DEPRECATEDREGIONS
); i
+= 2) {
1102 if (uprv_compareInvCharsAsAscii(buf
, DEPRECATEDREGIONS
[i
]) == 0) {
1103 uprv_strcpy(buf
, DEPRECATEDREGIONS
[i
+ 1]);
1104 len
= (int32_t)uprv_strlen(buf
);
1108 sink
.Append(buf
, len
);
1114 _appendVariantsToLanguageTag(const char* localeID
, icu::ByteSink
& sink
, UBool strict
, UBool
*hadPosix
, UErrorCode
* status
) {
1115 char buf
[ULOC_FULLNAME_CAPACITY
];
1116 UErrorCode tmpStatus
= U_ZERO_ERROR
;
1119 if (U_FAILURE(*status
)) {
1123 len
= uloc_getVariant(localeID
, buf
, sizeof(buf
), &tmpStatus
);
1124 if (U_FAILURE(tmpStatus
) || tmpStatus
== U_STRING_NOT_TERMINATED_WARNING
) {
1126 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1134 VariantListEntry
*var
;
1135 VariantListEntry
*varFirst
= NULL
;
1140 if (*p
== SEP
|| *p
== LOCALE_SEP
|| *p
== 0) {
1144 *p
= 0; /* terminate */
1148 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1151 /* ignore empty variant */
1153 /* ICU uses upper case letters for variants, but
1154 the canonical format is lowercase in BCP47 */
1155 for (i
= 0; *(pVar
+ i
) != 0; i
++) {
1156 *(pVar
+ i
) = uprv_tolower(*(pVar
+ i
));
1160 if (_isVariantSubtag(pVar
, -1)) {
1161 if (uprv_strcmp(pVar
,POSIX_VALUE
) || len
!= (int32_t)uprv_strlen(POSIX_VALUE
)) {
1162 /* emit the variant to the list */
1163 var
= (VariantListEntry
*)uprv_malloc(sizeof(VariantListEntry
));
1165 *status
= U_MEMORY_ALLOCATION_ERROR
;
1168 var
->variant
= pVar
;
1169 if (!_addVariantToList(&varFirst
, var
)) {
1170 /* duplicated variant */
1173 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1178 /* Special handling for POSIX variant, need to remember that we had it and then */
1179 /* treat it like an extension later. */
1182 } else if (strict
) {
1183 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1185 } else if (_isPrivateuseValueSubtag(pVar
, -1)) {
1186 /* Handle private use subtags separately */
1190 /* reset variant starting position */
1192 } else if (pVar
== NULL
) {
1198 if (U_SUCCESS(*status
)) {
1199 if (varFirst
!= NULL
) {
1202 /* write out validated/normalized variants to the target */
1204 while (var
!= NULL
) {
1205 sink
.Append("-", 1);
1206 varLen
= (int32_t)uprv_strlen(var
->variant
);
1207 sink
.Append(var
->variant
, varLen
);
1215 while (var
!= NULL
) {
1216 VariantListEntry
*tmpVar
= var
->next
;
1221 if (U_FAILURE(*status
)) {
1228 _appendKeywordsToLanguageTag(const char* localeID
, icu::ByteSink
& sink
, UBool strict
, UBool hadPosix
, UErrorCode
* status
) {
1229 char attrBuf
[ULOC_KEYWORD_AND_VALUES_CAPACITY
] = { 0 };
1230 int32_t attrBufLength
= 0;
1232 icu::MemoryPool
<AttributeListEntry
> attrPool
;
1233 icu::MemoryPool
<ExtensionListEntry
> extPool
;
1234 icu::MemoryPool
<icu::CharString
> strPool
;
1236 icu::LocalUEnumerationPointer
keywordEnum(uloc_openKeywords(localeID
, status
));
1237 if (U_FAILURE(*status
) && !hadPosix
) {
1240 if (keywordEnum
.isValid() || hadPosix
) {
1241 /* reorder extensions */
1244 ExtensionListEntry
*firstExt
= NULL
;
1245 ExtensionListEntry
*ext
;
1246 AttributeListEntry
*firstAttr
= NULL
;
1247 AttributeListEntry
*attr
;
1248 icu::MemoryPool
<icu::CharString
> extBufPool
;
1249 const char *bcpKey
=nullptr, *bcpValue
=nullptr;
1250 UErrorCode tmpStatus
= U_ZERO_ERROR
;
1255 icu::CharString buf
;
1256 key
= uenum_next(keywordEnum
.getAlias(), NULL
, status
);
1261 int32_t resultCapacity
= ULOC_KEYWORD_AND_VALUES_CAPACITY
;
1264 buffer
= buf
.getAppendBuffer(
1265 /*minCapacity=*/resultCapacity
,
1266 /*desiredCapacityHint=*/resultCapacity
,
1270 if (U_FAILURE(tmpStatus
)) {
1274 len
= uloc_getKeywordValue(
1275 localeID
, key
, buffer
, resultCapacity
, &tmpStatus
);
1277 if (tmpStatus
!= U_BUFFER_OVERFLOW_ERROR
) {
1281 resultCapacity
= len
;
1282 tmpStatus
= U_ZERO_ERROR
;
1285 if (U_FAILURE(tmpStatus
)) {
1286 if (tmpStatus
== U_MEMORY_ALLOCATION_ERROR
) {
1287 *status
= U_MEMORY_ALLOCATION_ERROR
;
1291 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1294 /* ignore this keyword */
1295 tmpStatus
= U_ZERO_ERROR
;
1299 buf
.append(buffer
, len
, tmpStatus
);
1300 if (tmpStatus
== U_STRING_NOT_TERMINATED_WARNING
) {
1301 tmpStatus
= U_ZERO_ERROR
; // Terminators provided by CharString.
1304 keylen
= (int32_t)uprv_strlen(key
);
1305 isBcpUExt
= (keylen
> 1);
1307 /* special keyword used for representing Unicode locale attributes */
1308 if (uprv_strcmp(key
, LOCALE_ATTRIBUTE_KEY
) == 0) {
1313 for (; i
< len
; i
++) {
1314 if (buf
[i
] != '-') {
1315 attrBuf
[attrBufLength
++] = buf
[i
];
1321 if (attrBufLength
> 0) {
1322 attrBuf
[attrBufLength
] = 0;
1324 } else if (i
>= len
){
1328 /* create AttributeListEntry */
1329 attr
= attrPool
.create();
1331 *status
= U_MEMORY_ALLOCATION_ERROR
;
1334 icu::CharString
* attrValue
=
1335 strPool
.create(attrBuf
, attrBufLength
, *status
);
1336 if (attrValue
== NULL
) {
1337 *status
= U_MEMORY_ALLOCATION_ERROR
;
1340 if (U_FAILURE(*status
)) {
1343 attr
->attribute
= attrValue
->data();
1345 if (!_addAttributeToList(&firstAttr
, attr
)) {
1347 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1352 /* for a place holder ExtensionListEntry */
1353 bcpKey
= LOCALE_ATTRIBUTE_KEY
;
1356 } else if (isBcpUExt
) {
1357 bcpKey
= uloc_toUnicodeLocaleKey(key
);
1358 if (bcpKey
== NULL
) {
1360 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1366 /* we've checked buf is null-terminated above */
1367 bcpValue
= uloc_toUnicodeLocaleType(key
, buf
.data());
1368 if (bcpValue
== NULL
) {
1370 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1375 if (bcpValue
== buf
.data()) {
1377 When uloc_toUnicodeLocaleType(key, buf) returns the
1378 input value as is, the value is well-formed, but has
1379 no known mapping. This implementation normalizes the
1382 icu::CharString
* extBuf
= extBufPool
.create();
1383 if (extBuf
== nullptr) {
1384 *status
= U_MEMORY_ALLOCATION_ERROR
;
1387 int32_t bcpValueLen
= static_cast<int32_t>(uprv_strlen(bcpValue
));
1388 int32_t resultCapacity
;
1389 char* pExtBuf
= extBuf
->getAppendBuffer(
1390 /*minCapacity=*/bcpValueLen
,
1391 /*desiredCapacityHint=*/bcpValueLen
,
1394 if (U_FAILURE(tmpStatus
)) {
1395 *status
= tmpStatus
;
1399 uprv_strcpy(pExtBuf
, bcpValue
);
1400 T_CString_toLowerCase(pExtBuf
);
1402 extBuf
->append(pExtBuf
, bcpValueLen
, tmpStatus
);
1403 if (U_FAILURE(tmpStatus
)) {
1404 *status
= tmpStatus
;
1408 bcpValue
= extBuf
->data();
1411 if (*key
== PRIVATEUSE
) {
1412 if (!ultag_isPrivateuseValueSubtags(buf
.data(), len
)) {
1414 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1420 if (!_isExtensionSingleton(key
, keylen
) || !ultag_isExtensionSubtags(buf
.data(), len
)) {
1422 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1429 icu::CharString
* extBuf
=
1430 extBufPool
.create(buf
.data(), len
, tmpStatus
);
1431 if (extBuf
== nullptr) {
1432 *status
= U_MEMORY_ALLOCATION_ERROR
;
1435 if (U_FAILURE(tmpStatus
)) {
1436 *status
= tmpStatus
;
1439 bcpValue
= extBuf
->data();
1442 /* create ExtensionListEntry */
1443 ext
= extPool
.create();
1445 *status
= U_MEMORY_ALLOCATION_ERROR
;
1449 ext
->value
= bcpValue
;
1451 if (!_addExtensionToList(&firstExt
, ext
, TRUE
)) {
1453 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1459 /* Special handling for POSIX variant - add the keywords for POSIX */
1461 /* create ExtensionListEntry for POSIX */
1462 ext
= extPool
.create();
1464 *status
= U_MEMORY_ALLOCATION_ERROR
;
1467 ext
->key
= POSIX_KEY
;
1468 ext
->value
= POSIX_VALUE
;
1470 if (!_addExtensionToList(&firstExt
, ext
, TRUE
)) {
1471 // Silently ignore errors.
1475 if (U_SUCCESS(*status
) && (firstExt
!= NULL
|| firstAttr
!= NULL
)) {
1476 UBool startLDMLExtension
= FALSE
;
1477 for (ext
= firstExt
; ext
; ext
= ext
->next
) {
1478 if (!startLDMLExtension
&& uprv_strlen(ext
->key
) > 1) {
1479 /* first LDML u singlton extension */
1480 sink
.Append("-u", 2);
1481 startLDMLExtension
= TRUE
;
1484 /* write out the sorted BCP47 attributes, extensions and private use */
1485 if (uprv_strcmp(ext
->key
, LOCALE_ATTRIBUTE_KEY
) == 0) {
1486 /* write the value for the attributes */
1487 for (attr
= firstAttr
; attr
; attr
= attr
->next
) {
1488 sink
.Append("-", 1);
1490 attr
->attribute
, static_cast<int32_t>(uprv_strlen(attr
->attribute
)));
1493 sink
.Append("-", 1);
1494 sink
.Append(ext
->key
, static_cast<int32_t>(uprv_strlen(ext
->key
)));
1495 sink
.Append("-", 1);
1496 sink
.Append(ext
->value
, static_cast<int32_t>(uprv_strlen(ext
->value
)));
1504 * Append keywords parsed from LDML extension value
1505 * e.g. "u-ca-gregory-co-trad" -> {calendar = gregorian} {collation = traditional}
1506 * Note: char* buf is used for storing keywords
1509 _appendLDMLExtensionAsKeywords(const char* ldmlext
, ExtensionListEntry
** appendTo
, icu::MemoryPool
<ExtensionListEntry
>& extPool
, icu::MemoryPool
<icu::CharString
>& kwdBuf
, UBool
*posixVariant
, UErrorCode
*status
) {
1510 const char *pTag
; /* beginning of current subtag */
1511 const char *pKwds
; /* beginning of key-type pairs */
1512 UBool variantExists
= *posixVariant
;
1514 ExtensionListEntry
*kwdFirst
= NULL
; /* first LDML keyword */
1515 ExtensionListEntry
*kwd
, *nextKwd
;
1519 /* Reset the posixVariant value */
1520 *posixVariant
= FALSE
;
1526 AttributeListEntry
*attrFirst
= NULL
; /* first attribute */
1527 AttributeListEntry
*attr
, *nextAttr
;
1529 char attrBuf
[ULOC_KEYWORD_AND_VALUES_CAPACITY
];
1530 int32_t attrBufIdx
= 0;
1532 icu::MemoryPool
<AttributeListEntry
> attrPool
;
1534 /* Iterate through u extension attributes */
1536 /* locate next separator char */
1537 for (len
= 0; *(pTag
+ len
) && *(pTag
+ len
) != SEP
; len
++);
1539 if (ultag_isUnicodeLocaleKey(pTag
, len
)) {
1544 /* add this attribute to the list */
1545 attr
= attrPool
.create();
1547 *status
= U_MEMORY_ALLOCATION_ERROR
;
1551 if (len
< (int32_t)sizeof(attrBuf
) - attrBufIdx
) {
1552 uprv_memcpy(&attrBuf
[attrBufIdx
], pTag
, len
);
1553 attrBuf
[attrBufIdx
+ len
] = 0;
1554 attr
->attribute
= &attrBuf
[attrBufIdx
];
1555 attrBufIdx
+= (len
+ 1);
1557 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1561 // duplicate attribute is ignored, causes no error.
1562 _addAttributeToList(&attrFirst
, attr
);
1567 /* next to the separator */
1573 /* emit attributes as an LDML keyword, e.g. attribute=attr1-attr2 */
1575 kwd
= extPool
.create();
1577 *status
= U_MEMORY_ALLOCATION_ERROR
;
1581 icu::CharString
* value
= kwdBuf
.create();
1582 if (value
== NULL
) {
1583 *status
= U_MEMORY_ALLOCATION_ERROR
;
1587 /* attribute subtags sorted in alphabetical order as type */
1589 while (attr
!= NULL
) {
1590 nextAttr
= attr
->next
;
1591 if (attr
!= attrFirst
) {
1592 value
->append('-', *status
);
1594 value
->append(attr
->attribute
, *status
);
1597 if (U_FAILURE(*status
)) {
1601 kwd
->key
= LOCALE_ATTRIBUTE_KEY
;
1602 kwd
->value
= value
->data();
1604 if (!_addExtensionToList(&kwdFirst
, kwd
, FALSE
)) {
1605 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1612 const char *pBcpKey
= NULL
; /* u extenstion key subtag */
1613 const char *pBcpType
= NULL
; /* beginning of u extension type subtag(s) */
1614 int32_t bcpKeyLen
= 0;
1615 int32_t bcpTypeLen
= 0;
1616 UBool isDone
= FALSE
;
1619 /* BCP47 representation of LDML key/type pairs */
1621 const char *pNextBcpKey
= NULL
;
1622 int32_t nextBcpKeyLen
= 0;
1623 UBool emitKeyword
= FALSE
;
1626 /* locate next separator char */
1627 for (len
= 0; *(pTag
+ len
) && *(pTag
+ len
) != SEP
; len
++);
1629 if (ultag_isUnicodeLocaleKey(pTag
, len
)) {
1633 nextBcpKeyLen
= len
;
1639 U_ASSERT(pBcpKey
!= NULL
);
1640 /* within LDML type subtags */
1642 bcpTypeLen
+= (len
+ 1);
1652 /* next to the separator */
1656 /* processing last one */
1662 const char *pKey
= NULL
; /* LDML key */
1663 const char *pType
= NULL
; /* LDML type */
1665 char bcpKeyBuf
[9]; /* BCP key length is always 2 for now */
1667 U_ASSERT(pBcpKey
!= NULL
);
1669 if (bcpKeyLen
>= (int32_t)sizeof(bcpKeyBuf
)) {
1670 /* the BCP key is invalid */
1671 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1675 uprv_strncpy(bcpKeyBuf
, pBcpKey
, bcpKeyLen
);
1676 bcpKeyBuf
[bcpKeyLen
] = 0;
1678 /* u extension key to LDML key */
1679 pKey
= uloc_toLegacyKey(bcpKeyBuf
);
1681 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1684 if (pKey
== bcpKeyBuf
) {
1686 The key returned by toLegacyKey points to the input buffer.
1687 We normalize the result key to lower case.
1689 T_CString_toLowerCase(bcpKeyBuf
);
1690 icu::CharString
* key
= kwdBuf
.create(bcpKeyBuf
, bcpKeyLen
, *status
);
1692 *status
= U_MEMORY_ALLOCATION_ERROR
;
1695 if (U_FAILURE(*status
)) {
1702 char bcpTypeBuf
[128]; /* practically long enough even considering multiple subtag type */
1703 if (bcpTypeLen
>= (int32_t)sizeof(bcpTypeBuf
)) {
1704 /* the BCP type is too long */
1705 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1709 uprv_strncpy(bcpTypeBuf
, pBcpType
, bcpTypeLen
);
1710 bcpTypeBuf
[bcpTypeLen
] = 0;
1712 /* BCP type to locale type */
1713 pType
= uloc_toLegacyType(pKey
, bcpTypeBuf
);
1714 if (pType
== NULL
) {
1715 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1718 if (pType
== bcpTypeBuf
) {
1720 The type returned by toLegacyType points to the input buffer.
1721 We normalize the result type to lower case.
1723 /* normalize to lower case */
1724 T_CString_toLowerCase(bcpTypeBuf
);
1725 icu::CharString
* type
= kwdBuf
.create(bcpTypeBuf
, bcpTypeLen
, *status
);
1727 *status
= U_MEMORY_ALLOCATION_ERROR
;
1730 if (U_FAILURE(*status
)) {
1733 pType
= type
->data();
1736 /* typeless - default type value is "yes" */
1737 pType
= LOCALE_TYPE_YES
;
1740 /* Special handling for u-va-posix, since we want to treat this as a variant,
1742 if (!variantExists
&& !uprv_strcmp(pKey
, POSIX_KEY
) && !uprv_strcmp(pType
, POSIX_VALUE
) ) {
1743 *posixVariant
= TRUE
;
1745 /* create an ExtensionListEntry for this keyword */
1746 kwd
= extPool
.create();
1748 *status
= U_MEMORY_ALLOCATION_ERROR
;
1755 if (!_addExtensionToList(&kwdFirst
, kwd
, FALSE
)) {
1756 // duplicate keyword is allowed, Only the first
1761 pBcpKey
= pNextBcpKey
;
1762 bcpKeyLen
= pNextBcpKey
!= NULL
? nextBcpKeyLen
: 0;
1770 while (kwd
!= NULL
) {
1771 nextKwd
= kwd
->next
;
1772 _addExtensionToList(appendTo
, kwd
, FALSE
);
1779 _appendKeywords(ULanguageTag
* langtag
, icu::ByteSink
& sink
, UErrorCode
* status
) {
1782 ExtensionListEntry
*kwdFirst
= NULL
;
1783 ExtensionListEntry
*kwd
;
1784 const char *key
, *type
;
1785 icu::MemoryPool
<ExtensionListEntry
> extPool
;
1786 icu::MemoryPool
<icu::CharString
> kwdBuf
;
1787 UBool posixVariant
= FALSE
;
1789 if (U_FAILURE(*status
)) {
1793 /* Determine if variants already exists */
1794 if (ultag_getVariantsSize(langtag
)) {
1795 posixVariant
= TRUE
;
1798 n
= ultag_getExtensionsSize(langtag
);
1800 /* resolve locale keywords and reordering keys */
1801 for (i
= 0; i
< n
; i
++) {
1802 key
= ultag_getExtensionKey(langtag
, i
);
1803 type
= ultag_getExtensionValue(langtag
, i
);
1804 if (*key
== LDMLEXT
) {
1805 _appendLDMLExtensionAsKeywords(type
, &kwdFirst
, extPool
, kwdBuf
, &posixVariant
, status
);
1806 if (U_FAILURE(*status
)) {
1810 kwd
= extPool
.create();
1812 *status
= U_MEMORY_ALLOCATION_ERROR
;
1817 if (!_addExtensionToList(&kwdFirst
, kwd
, FALSE
)) {
1818 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1824 if (U_SUCCESS(*status
)) {
1825 type
= ultag_getPrivateUse(langtag
);
1826 if ((int32_t)uprv_strlen(type
) > 0) {
1827 /* add private use as a keyword */
1828 kwd
= extPool
.create();
1830 *status
= U_MEMORY_ALLOCATION_ERROR
;
1832 kwd
->key
= PRIVATEUSE_KEY
;
1834 if (!_addExtensionToList(&kwdFirst
, kwd
, FALSE
)) {
1835 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1841 /* If a POSIX variant was in the extensions, write it out before writing the keywords. */
1843 if (U_SUCCESS(*status
) && posixVariant
) {
1844 len
= (int32_t) uprv_strlen(_POSIX
);
1845 sink
.Append(_POSIX
, len
);
1848 if (U_SUCCESS(*status
) && kwdFirst
!= NULL
) {
1849 /* write out the sorted keywords */
1850 UBool firstValue
= TRUE
;
1854 sink
.Append("@", 1);
1857 sink
.Append(";", 1);
1861 len
= (int32_t)uprv_strlen(kwd
->key
);
1862 sink
.Append(kwd
->key
, len
);
1863 sink
.Append("=", 1);
1866 len
= (int32_t)uprv_strlen(kwd
->value
);
1867 sink
.Append(kwd
->value
, len
);
1875 _appendPrivateuseToLanguageTag(const char* localeID
, icu::ByteSink
& sink
, UBool strict
, UBool hadPosix
, UErrorCode
* status
) {
1877 char buf
[ULOC_FULLNAME_CAPACITY
];
1878 char tmpAppend
[ULOC_FULLNAME_CAPACITY
];
1879 UErrorCode tmpStatus
= U_ZERO_ERROR
;
1882 int32_t capacity
= sizeof tmpAppend
;
1884 if (U_FAILURE(*status
)) {
1888 len
= uloc_getVariant(localeID
, buf
, sizeof(buf
), &tmpStatus
);
1889 if (U_FAILURE(tmpStatus
) || tmpStatus
== U_STRING_NOT_TERMINATED_WARNING
) {
1891 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1899 UBool firstValue
= TRUE
;
1906 if (*p
== SEP
|| *p
== LOCALE_SEP
|| *p
== 0) {
1910 *p
= 0; /* terminate */
1912 if (pPriv
!= NULL
) {
1913 /* Private use in the canonical format is lowercase in BCP47 */
1914 for (i
= 0; *(pPriv
+ i
) != 0; i
++) {
1915 *(pPriv
+ i
) = uprv_tolower(*(pPriv
+ i
));
1919 if (_isPrivateuseValueSubtag(pPriv
, -1)) {
1921 if (!_isVariantSubtag(pPriv
, -1)) {
1927 } else if (strict
) {
1928 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
1935 if (reslen
< capacity
) {
1936 tmpAppend
[reslen
++] = SEP
;
1940 if (reslen
< capacity
) {
1941 tmpAppend
[reslen
++] = *PRIVATEUSE_KEY
;
1944 if (reslen
< capacity
) {
1945 tmpAppend
[reslen
++] = SEP
;
1948 len
= (int32_t)uprv_strlen(PRIVUSE_VARIANT_PREFIX
);
1949 if (reslen
< capacity
) {
1950 uprv_memcpy(tmpAppend
+ reslen
, PRIVUSE_VARIANT_PREFIX
, uprv_min(len
, capacity
- reslen
));
1954 if (reslen
< capacity
) {
1955 tmpAppend
[reslen
++] = SEP
;
1961 len
= (int32_t)uprv_strlen(pPriv
);
1962 if (reslen
< capacity
) {
1963 uprv_memcpy(tmpAppend
+ reslen
, pPriv
, uprv_min(len
, capacity
- reslen
));
1968 /* reset private use starting position */
1970 } else if (pPriv
== NULL
) {
1976 if (U_FAILURE(*status
)) {
1981 if (U_SUCCESS(*status
)) {
1983 sink
.Append(tmpAppend
, len
);
1988 * -------------------------------------------------
1992 * -------------------------------------------------
1995 /* Bit flags used by the parser */
2006 * Ticket #12705 - Visual Studio 2015 Update 3 contains a new code optimizer which has problems optimizing
2007 * this function. (See https://blogs.msdn.microsoft.com/vcblog/2016/05/04/new-code-optimizer/ )
2008 * As a workaround, we will turn off optimization just for this function on VS2015 Update 3 and above.
2010 #if (defined(_MSC_VER) && (_MSC_VER >= 1900) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190024210))
2011 #pragma optimize( "", off )
2014 static ULanguageTag
*
2015 ultag_parse(const char* tag
, int32_t tagLen
, int32_t* parsedLen
, UErrorCode
* status
) {
2018 char *pSubtag
, *pNext
, *pLastGoodPosition
;
2021 ExtensionListEntry
*pExtension
;
2022 char *pExtValueSubtag
, *pExtValueSubtagEnd
;
2024 UBool privateuseVar
= FALSE
;
2025 int32_t grandfatheredLen
= 0;
2027 if (parsedLen
!= NULL
) {
2031 if (U_FAILURE(*status
)) {
2036 tagLen
= (int32_t)uprv_strlen(tag
);
2039 /* copy the entire string */
2040 tagBuf
= (char*)uprv_malloc(tagLen
+ 1);
2041 if (tagBuf
== NULL
) {
2042 *status
= U_MEMORY_ALLOCATION_ERROR
;
2045 uprv_memcpy(tagBuf
, tag
, tagLen
);
2046 *(tagBuf
+ tagLen
) = 0;
2048 /* create a ULanguageTag */
2049 icu::LocalULanguageTagPointer
t(
2050 (ULanguageTag
*)uprv_malloc(sizeof(ULanguageTag
)));
2053 *status
= U_MEMORY_ALLOCATION_ERROR
;
2056 _initializeULanguageTag(t
.getAlias());
2059 if (tagLen
< MINLEN
) {
2060 /* the input tag is too short - return empty ULanguageTag */
2064 size_t parsedLenDelta
= 0;
2065 // Grandfathered tag will be consider together. Grandfathered tag with intervening
2066 // script and region such as art-DE-lojban or art-Latn-lojban won't be
2068 /* check if the tag is grandfathered */
2069 for (i
= 0; i
< UPRV_LENGTHOF(GRANDFATHERED
); i
+= 2) {
2070 int32_t checkGrandfatheredLen
= static_cast<int32_t>(uprv_strlen(GRANDFATHERED
[i
]));
2071 if (tagLen
< checkGrandfatheredLen
) {
2074 if (tagLen
> checkGrandfatheredLen
&& tagBuf
[checkGrandfatheredLen
] != '-') {
2075 // make sure next char is '-'.
2078 if (uprv_strnicmp(GRANDFATHERED
[i
], tagBuf
, checkGrandfatheredLen
) == 0) {
2079 int32_t newTagLength
;
2081 grandfatheredLen
= checkGrandfatheredLen
; /* back up for output parsedLen */
2082 int32_t replacementLen
= static_cast<int32_t>(uprv_strlen(GRANDFATHERED
[i
+1]));
2083 newTagLength
= replacementLen
+ tagLen
- checkGrandfatheredLen
;
2084 if (tagLen
< newTagLength
) {
2086 tagBuf
= (char*)uprv_malloc(newTagLength
+ 1);
2087 if (tagBuf
== NULL
) {
2088 *status
= U_MEMORY_ALLOCATION_ERROR
;
2092 tagLen
= newTagLength
;
2094 parsedLenDelta
= checkGrandfatheredLen
- replacementLen
;
2095 uprv_strcpy(t
->buf
, GRANDFATHERED
[i
+ 1]);
2096 if (checkGrandfatheredLen
!= tagLen
) {
2097 uprv_strcpy(t
->buf
+ replacementLen
, tag
+ checkGrandfatheredLen
);
2103 if (grandfatheredLen
== 0) {
2104 for (i
= 0; i
< UPRV_LENGTHOF(REDUNDANT
); i
+= 2) {
2105 const char* redundantTag
= REDUNDANT
[i
];
2106 size_t redundantTagLen
= uprv_strlen(redundantTag
);
2107 // The preferred tag for a redundant tag is always shorter than redundant
2108 // tag. A redundant tag may or may not be followed by other subtags.
2109 // (i.e. "zh-yue" or "zh-yue-u-co-pinyin").
2110 if (uprv_strnicmp(redundantTag
, tagBuf
, static_cast<uint32_t>(redundantTagLen
)) == 0) {
2111 const char* redundantTagEnd
= tagBuf
+ redundantTagLen
;
2112 if (*redundantTagEnd
== '\0' || *redundantTagEnd
== SEP
) {
2113 const char* preferredTag
= REDUNDANT
[i
+ 1];
2114 size_t preferredTagLen
= uprv_strlen(preferredTag
);
2115 uprv_strncpy(t
->buf
, preferredTag
, preferredTagLen
);
2116 if (*redundantTagEnd
== SEP
) {
2117 uprv_memmove(tagBuf
+ preferredTagLen
,
2119 tagLen
- redundantTagLen
+ 1);
2121 tagBuf
[preferredTagLen
] = '\0';
2123 // parsedLen should be the length of the input
2124 // before redundantTag is replaced by preferredTag.
2125 // Save the delta to add it back later.
2126 parsedLenDelta
= redundantTagLen
- preferredTagLen
;
2134 * langtag = language
2143 pNext
= pLastGoodPosition
= tagBuf
;
2146 pExtValueSubtag
= NULL
;
2147 pExtValueSubtagEnd
= NULL
;
2154 /* locate next separator char */
2168 subtagLen
= (int32_t)(pSep
- pSubtag
);
2171 if (ultag_isLanguageSubtag(pSubtag
, subtagLen
)) {
2172 *pSep
= 0; /* terminate */
2173 // TODO: move deprecated language code handling here.
2174 t
->language
= T_CString_toLowerCase(pSubtag
);
2176 pLastGoodPosition
= pSep
;
2177 next
= SCRT
| REGN
| VART
| EXTS
| PRIV
;
2184 if (_isExtlangSubtag(pSubtag
, subtagLen
)) {
2186 t
->extlang
[extlangIdx
++] = T_CString_toLowerCase(pSubtag
);
2188 pLastGoodPosition
= pSep
;
2189 if (extlangIdx
< 3) {
2190 next
= EXTL
| SCRT
| REGN
| VART
| EXTS
| PRIV
;
2192 next
= SCRT
| REGN
| VART
| EXTS
| PRIV
;
2198 if (ultag_isScriptSubtag(pSubtag
, subtagLen
)) {
2204 *p
= uprv_toupper(*p
);
2207 *p
= uprv_tolower(*p
);
2210 t
->script
= pSubtag
;
2212 pLastGoodPosition
= pSep
;
2213 next
= REGN
| VART
| EXTS
| PRIV
;
2218 if (ultag_isRegionSubtag(pSubtag
, subtagLen
)) {
2220 // TODO: move deprecated region code handling here.
2221 t
->region
= T_CString_toUpperCase(pSubtag
);
2223 pLastGoodPosition
= pSep
;
2224 next
= VART
| EXTS
| PRIV
;
2229 if (_isVariantSubtag(pSubtag
, subtagLen
) ||
2230 (privateuseVar
&& _isPrivateuseVariantSubtag(pSubtag
, subtagLen
))) {
2231 VariantListEntry
*var
;
2234 var
= (VariantListEntry
*)uprv_malloc(sizeof(VariantListEntry
));
2236 *status
= U_MEMORY_ALLOCATION_ERROR
;
2240 var
->variant
= T_CString_toUpperCase(pSubtag
);
2241 isAdded
= _addVariantToList(&(t
->variants
), var
);
2243 /* duplicated variant entry */
2247 pLastGoodPosition
= pSep
;
2248 next
= VART
| EXTS
| PRIV
;
2253 if (_isExtensionSingleton(pSubtag
, subtagLen
)) {
2254 if (pExtension
!= NULL
) {
2255 if (pExtValueSubtag
== NULL
|| pExtValueSubtagEnd
== NULL
) {
2256 /* the previous extension is incomplete */
2257 uprv_free(pExtension
);
2262 /* terminate the previous extension value */
2263 *pExtValueSubtagEnd
= 0;
2264 pExtension
->value
= T_CString_toLowerCase(pExtValueSubtag
);
2266 /* insert the extension to the list */
2267 if (_addExtensionToList(&(t
->extensions
), pExtension
, FALSE
)) {
2268 pLastGoodPosition
= pExtValueSubtagEnd
;
2270 /* stop parsing here */
2271 uprv_free(pExtension
);
2277 /* create a new extension */
2278 pExtension
= (ExtensionListEntry
*)uprv_malloc(sizeof(ExtensionListEntry
));
2279 if (pExtension
== NULL
) {
2280 *status
= U_MEMORY_ALLOCATION_ERROR
;
2284 pExtension
->key
= T_CString_toLowerCase(pSubtag
);
2285 pExtension
->value
= NULL
; /* will be set later */
2288 * reset the start and the end location of extension value
2289 * subtags for this extension
2291 pExtValueSubtag
= NULL
;
2292 pExtValueSubtagEnd
= NULL
;
2299 if (_isExtensionSubtag(pSubtag
, subtagLen
)) {
2300 if (pExtValueSubtag
== NULL
) {
2301 /* if the start postion of this extension's value is not yet,
2302 this one is the first value subtag */
2303 pExtValueSubtag
= pSubtag
;
2306 /* Mark the end of this subtag */
2307 pExtValueSubtagEnd
= pSep
;
2308 next
= EXTS
| EXTV
| PRIV
;
2314 if (uprv_tolower(*pSubtag
) == PRIVATEUSE
&& subtagLen
== 1) {
2317 if (pExtension
!= NULL
) {
2318 /* Process the last extension */
2319 if (pExtValueSubtag
== NULL
|| pExtValueSubtagEnd
== NULL
) {
2320 /* the previous extension is incomplete */
2321 uprv_free(pExtension
);
2325 /* terminate the previous extension value */
2326 *pExtValueSubtagEnd
= 0;
2327 pExtension
->value
= T_CString_toLowerCase(pExtValueSubtag
);
2329 /* insert the extension to the list */
2330 if (_addExtensionToList(&(t
->extensions
), pExtension
, FALSE
)) {
2331 pLastGoodPosition
= pExtValueSubtagEnd
;
2334 /* stop parsing here */
2335 uprv_free(pExtension
);
2342 /* The rest of part will be private use value subtags */
2343 if (pNext
== NULL
) {
2344 /* empty private use subtag */
2347 /* back up the private use value start position */
2348 pPrivuseVal
= pNext
;
2350 /* validate private use value subtags */
2366 subtagLen
= (int32_t)(pSep
- pSubtag
);
2368 if (uprv_strncmp(pSubtag
, PRIVUSE_VARIANT_PREFIX
, uprv_strlen(PRIVUSE_VARIANT_PREFIX
)) == 0) {
2371 privateuseVar
= TRUE
;
2373 } else if (_isPrivateuseValueSubtag(pSubtag
, subtagLen
)) {
2374 pLastGoodPosition
= pSep
;
2384 if (pLastGoodPosition
- pPrivuseVal
> 0) {
2385 *pLastGoodPosition
= 0;
2386 t
->privateuse
= T_CString_toLowerCase(pPrivuseVal
);
2388 /* No more subtags, exiting the parse loop */
2394 /* If we fell through here, it means this subtag is illegal - quit parsing */
2398 if (pExtension
!= NULL
) {
2399 /* Process the last extension */
2400 if (pExtValueSubtag
== NULL
|| pExtValueSubtagEnd
== NULL
) {
2401 /* the previous extension is incomplete */
2402 uprv_free(pExtension
);
2404 /* terminate the previous extension value */
2405 *pExtValueSubtagEnd
= 0;
2406 pExtension
->value
= T_CString_toLowerCase(pExtValueSubtag
);
2407 /* insert the extension to the list */
2408 if (_addExtensionToList(&(t
->extensions
), pExtension
, FALSE
)) {
2409 pLastGoodPosition
= pExtValueSubtagEnd
;
2411 uprv_free(pExtension
);
2416 if (parsedLen
!= NULL
) {
2417 *parsedLen
= (int32_t)(pLastGoodPosition
- t
->buf
+ parsedLenDelta
);
2424 * Ticket #12705 - Turn optimization back on.
2426 #if (defined(_MSC_VER) && (_MSC_VER >= 1900) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190024210))
2427 #pragma optimize( "", on )
2431 ultag_close(ULanguageTag
* langtag
) {
2433 if (langtag
== NULL
) {
2437 uprv_free(langtag
->buf
);
2439 if (langtag
->variants
) {
2440 VariantListEntry
*curVar
= langtag
->variants
;
2442 VariantListEntry
*nextVar
= curVar
->next
;
2448 if (langtag
->extensions
) {
2449 ExtensionListEntry
*curExt
= langtag
->extensions
;
2451 ExtensionListEntry
*nextExt
= curExt
->next
;
2461 ultag_getLanguage(const ULanguageTag
* langtag
) {
2462 return langtag
->language
;
2467 ultag_getJDKLanguage(const ULanguageTag
* langtag
) {
2469 for (i
= 0; DEPRECATEDLANGS
[i
] != NULL
; i
+= 2) {
2470 if (uprv_compareInvCharsAsAscii(DEPRECATEDLANGS
[i
], langtag
->language
) == 0) {
2471 return DEPRECATEDLANGS
[i
+ 1];
2474 return langtag
->language
;
2479 ultag_getExtlang(const ULanguageTag
* langtag
, int32_t idx
) {
2480 if (idx
>= 0 && idx
< MAXEXTLANG
) {
2481 return langtag
->extlang
[idx
];
2487 ultag_getExtlangSize(const ULanguageTag
* langtag
) {
2490 for (i
= 0; i
< MAXEXTLANG
; i
++) {
2491 if (langtag
->extlang
[i
]) {
2499 ultag_getScript(const ULanguageTag
* langtag
) {
2500 return langtag
->script
;
2504 ultag_getRegion(const ULanguageTag
* langtag
) {
2505 return langtag
->region
;
2509 ultag_getVariant(const ULanguageTag
* langtag
, int32_t idx
) {
2510 const char *var
= NULL
;
2511 VariantListEntry
*cur
= langtag
->variants
;
2525 ultag_getVariantsSize(const ULanguageTag
* langtag
) {
2527 VariantListEntry
*cur
= langtag
->variants
;
2539 ultag_getExtensionKey(const ULanguageTag
* langtag
, int32_t idx
) {
2540 const char *key
= NULL
;
2541 ExtensionListEntry
*cur
= langtag
->extensions
;
2555 ultag_getExtensionValue(const ULanguageTag
* langtag
, int32_t idx
) {
2556 const char *val
= NULL
;
2557 ExtensionListEntry
*cur
= langtag
->extensions
;
2571 ultag_getExtensionsSize(const ULanguageTag
* langtag
) {
2573 ExtensionListEntry
*cur
= langtag
->extensions
;
2585 ultag_getPrivateUse(const ULanguageTag
* langtag
) {
2586 return langtag
->privateuse
;
2591 ultag_getGrandfathered(const ULanguageTag
* langtag
) {
2592 return langtag
->grandfathered
;
2598 * -------------------------------------------------
2600 * Locale/BCP47 conversion APIs, exposed as uloc_*
2602 * -------------------------------------------------
2604 U_CAPI
int32_t U_EXPORT2
2605 uloc_toLanguageTag(const char* localeID
,
2607 int32_t langtagCapacity
,
2609 UErrorCode
* status
) {
2610 if (U_FAILURE(*status
)) {
2614 icu::CheckedArrayByteSink
sink(langtag
, langtagCapacity
);
2615 ulocimp_toLanguageTag(localeID
, sink
, strict
, status
);
2617 int32_t reslen
= sink
.NumberOfBytesAppended();
2619 if (U_FAILURE(*status
)) {
2623 if (sink
.Overflowed()) {
2624 *status
= U_BUFFER_OVERFLOW_ERROR
;
2626 u_terminateChars(langtag
, langtagCapacity
, reslen
, status
);
2633 U_CAPI
void U_EXPORT2
2634 ulocimp_toLanguageTag(const char* localeID
,
2635 icu::ByteSink
& sink
,
2637 UErrorCode
* status
) {
2638 icu::CharString canonical
;
2640 UErrorCode tmpStatus
= U_ZERO_ERROR
;
2641 UBool hadPosix
= FALSE
;
2642 const char* pKeywordStart
;
2644 /* Note: uloc_canonicalize returns "en_US_POSIX" for input locale ID "". See #6835 */
2645 int32_t resultCapacity
= static_cast<int32_t>(uprv_strlen(localeID
));
2646 if (resultCapacity
> 0) {
2650 buffer
= canonical
.getAppendBuffer(
2651 /*minCapacity=*/resultCapacity
,
2652 /*desiredCapacityHint=*/resultCapacity
,
2656 if (U_FAILURE(tmpStatus
)) {
2657 *status
= tmpStatus
;
2662 uloc_canonicalize(localeID
, buffer
, resultCapacity
, &tmpStatus
);
2664 if (tmpStatus
!= U_BUFFER_OVERFLOW_ERROR
) {
2668 resultCapacity
= reslen
;
2669 tmpStatus
= U_ZERO_ERROR
;
2672 if (U_FAILURE(tmpStatus
)) {
2673 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
2677 canonical
.append(buffer
, reslen
, tmpStatus
);
2678 if (tmpStatus
== U_STRING_NOT_TERMINATED_WARNING
) {
2679 tmpStatus
= U_ZERO_ERROR
; // Terminators provided by CharString.
2682 if (U_FAILURE(tmpStatus
)) {
2683 *status
= tmpStatus
;
2688 /* For handling special case - private use only tag */
2689 pKeywordStart
= locale_getKeywordsStart(canonical
.data());
2690 if (pKeywordStart
== canonical
.data()) {
2694 icu::LocalUEnumerationPointer
kwdEnum(uloc_openKeywords(canonical
.data(), &tmpStatus
));
2695 if (U_SUCCESS(tmpStatus
)) {
2696 kwdCnt
= uenum_count(kwdEnum
.getAlias(), &tmpStatus
);
2701 key
= uenum_next(kwdEnum
.getAlias(), &len
, &tmpStatus
);
2702 if (len
== 1 && *key
== PRIVATEUSE
) {
2703 char buf
[ULOC_KEYWORD_AND_VALUES_CAPACITY
];
2704 buf
[0] = PRIVATEUSE
;
2706 len
= uloc_getKeywordValue(localeID
, key
, &buf
[2], sizeof(buf
) - 2, &tmpStatus
);
2707 if (U_SUCCESS(tmpStatus
)) {
2708 if (ultag_isPrivateuseValueSubtags(&buf
[2], len
)) {
2709 /* return private use only tag */
2710 sink
.Append(buf
, len
+ 2);
2712 } else if (strict
) {
2713 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
2716 /* if not strict mode, then "und" will be returned */
2718 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
2729 _appendLanguageToLanguageTag(canonical
.data(), sink
, strict
, status
);
2730 _appendScriptToLanguageTag(canonical
.data(), sink
, strict
, status
);
2731 _appendRegionToLanguageTag(canonical
.data(), sink
, strict
, status
);
2732 _appendVariantsToLanguageTag(canonical
.data(), sink
, strict
, &hadPosix
, status
);
2733 _appendKeywordsToLanguageTag(canonical
.data(), sink
, strict
, hadPosix
, status
);
2734 _appendPrivateuseToLanguageTag(canonical
.data(), sink
, strict
, hadPosix
, status
);
2738 U_CAPI
int32_t U_EXPORT2
2739 uloc_forLanguageTag(const char* langtag
,
2741 int32_t localeIDCapacity
,
2742 int32_t* parsedLength
,
2743 UErrorCode
* status
) {
2744 if (U_FAILURE(*status
)) {
2748 icu::CheckedArrayByteSink
sink(localeID
, localeIDCapacity
);
2749 ulocimp_forLanguageTag(langtag
, -1, sink
, parsedLength
, status
);
2751 int32_t reslen
= sink
.NumberOfBytesAppended();
2753 if (U_FAILURE(*status
)) {
2757 if (sink
.Overflowed()) {
2758 *status
= U_BUFFER_OVERFLOW_ERROR
;
2760 u_terminateChars(localeID
, localeIDCapacity
, reslen
, status
);
2767 U_CAPI
void U_EXPORT2
2768 ulocimp_forLanguageTag(const char* langtag
,
2770 icu::ByteSink
& sink
,
2771 int32_t* parsedLength
,
2772 UErrorCode
* status
) {
2773 UBool isEmpty
= TRUE
;
2774 const char *subtag
, *p
;
2777 UBool noRegion
= TRUE
;
2779 icu::LocalULanguageTagPointer
lt(ultag_parse(langtag
, tagLen
, parsedLength
, status
));
2780 if (U_FAILURE(*status
)) {
2785 subtag
= ultag_getExtlangSize(lt
.getAlias()) > 0 ? ultag_getExtlang(lt
.getAlias(), 0) : ultag_getLanguage(lt
.getAlias());
2786 if (uprv_compareInvCharsAsAscii(subtag
, LANG_UND
) != 0) {
2787 len
= (int32_t)uprv_strlen(subtag
);
2789 sink
.Append(subtag
, len
);
2795 subtag
= ultag_getScript(lt
.getAlias());
2796 len
= (int32_t)uprv_strlen(subtag
);
2798 sink
.Append("_", 1);
2801 /* write out the script in title case */
2802 char c
= uprv_toupper(*subtag
);
2804 sink
.Append(subtag
+ 1, len
- 1);
2808 subtag
= ultag_getRegion(lt
.getAlias());
2809 len
= (int32_t)uprv_strlen(subtag
);
2811 sink
.Append("_", 1);
2814 /* write out the region in upper case */
2817 char c
= uprv_toupper(*p
);
2825 n
= ultag_getVariantsSize(lt
.getAlias());
2828 sink
.Append("_", 1);
2832 for (i
= 0; i
< n
; i
++) {
2833 subtag
= ultag_getVariant(lt
.getAlias(), i
);
2834 sink
.Append("_", 1);
2836 /* write out the variant in upper case */
2839 char c
= uprv_toupper(*p
);
2847 n
= ultag_getExtensionsSize(lt
.getAlias());
2848 subtag
= ultag_getPrivateUse(lt
.getAlias());
2849 if (n
> 0 || uprv_strlen(subtag
) > 0) {
2850 if (isEmpty
&& n
> 0) {
2851 /* need a language */
2852 sink
.Append(LANG_UND
, LANG_UND_LEN
);
2854 _appendKeywords(lt
.getAlias(), sink
, status
);