1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1997-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: loclikely.cpp
12 * tab size: 8 (not used)
15 * created on: 2010feb25
16 * created by: Markus W. Scherer
18 * Code for likely and minimized locale subtags, separated out from other .cpp files
19 * that then do not depend on resource bundle code and likely-subtags data.
22 #include "unicode/utypes.h"
23 #include "unicode/locid.h"
24 #include "unicode/putil.h"
25 #include "unicode/uloc.h"
26 #include "unicode/ures.h"
27 #include "unicode/uscript.h"
34 * This function looks for the localeID in the likelySubtags resource.
36 * @param localeID The tag to find.
37 * @param buffer A buffer to hold the matching entry
38 * @param bufferLength The length of the output buffer
39 * @return A pointer to "buffer" if found, or a null pointer if not.
41 static const char* U_CALLCONV
42 findLikelySubtags(const char* localeID,
46 const char* result = NULL;
48 if (!U_FAILURE(*err)) {
50 const UChar* s = NULL;
51 UErrorCode tmpErr = U_ZERO_ERROR;
52 UResourceBundle* subtags = ures_openDirect(NULL, "likelySubtags", &tmpErr);
53 if (U_SUCCESS(tmpErr)) {
54 s = ures_getStringByKey(subtags, localeID, &resLen, &tmpErr);
56 if (U_FAILURE(tmpErr)) {
58 * If a resource is missing, it's not really an error, it's
59 * just that we don't have any data for that particular locale ID.
61 if (tmpErr != U_MISSING_RESOURCE_ERROR) {
65 else if (resLen >= bufferLength) {
66 /* The buffer should never overflow. */
67 *err = U_INTERNAL_PROGRAM_ERROR;
70 u_UCharsToChars(s, buffer, resLen + 1);
84 * Append a tag to a buffer, adding the separator if necessary. The buffer
85 * must be large enough to contain the resulting tag plus any separator
86 * necessary. The tag must not be a zero-length string.
88 * @param tag The tag to add.
89 * @param tagLength The length of the tag.
90 * @param buffer The output buffer.
91 * @param bufferLength The length of the output buffer. This is an input/ouput parameter.
93 static void U_CALLCONV
98 int32_t* bufferLength) {
100 if (*bufferLength > 0) {
101 buffer[*bufferLength] = '_';
106 &buffer[*bufferLength],
110 *bufferLength += tagLength;
114 * These are the canonical strings for unknown languages, scripts and regions.
116 static const char* const unknownLanguage = "und";
117 static const char* const unknownScript = "Zzzz";
118 static const char* const unknownRegion = "ZZ";
121 * Create a tag string from the supplied parameters. The lang, script and region
122 * parameters may be NULL pointers. If they are, their corresponding length parameters
123 * must be less than or equal to 0.
125 * If any of the language, script or region parameters are empty, and the alternateTags
126 * parameter is not NULL, it will be parsed for potential language, script and region tags
127 * to be used when constructing the new tag. If the alternateTags parameter is NULL, or
128 * it contains no language tag, the default tag for the unknown language is used.
130 * If the length of the new string exceeds the capacity of the output buffer,
131 * the function copies as many bytes to the output buffer as it can, and returns
132 * the error U_BUFFER_OVERFLOW_ERROR.
134 * If an illegal argument is provided, the function returns the error
135 * U_ILLEGAL_ARGUMENT_ERROR.
137 * Note that this function can return the warning U_STRING_NOT_TERMINATED_WARNING if
138 * the tag string fits in the output buffer, but the null terminator doesn't.
140 * @param lang The language tag to use.
141 * @param langLength The length of the language tag.
142 * @param script The script tag to use.
143 * @param scriptLength The length of the script tag.
144 * @param region The region tag to use.
145 * @param regionLength The length of the region tag.
146 * @param trailing Any trailing data to append to the new tag.
147 * @param trailingLength The length of the trailing data.
148 * @param alternateTags A string containing any alternate tags.
149 * @param tag The output buffer.
150 * @param tagCapacity The capacity of the output buffer.
151 * @param err A pointer to a UErrorCode for error reporting.
152 * @return The length of the tag string, which may be greater than tagCapacity, or -1 on error.
154 static int32_t U_CALLCONV
155 createTagStringWithAlternates(
159 int32_t scriptLength,
161 int32_t regionLength,
162 const char* trailing,
163 int32_t trailingLength,
164 const char* alternateTags,
169 if (U_FAILURE(*err)) {
172 else if (tag == NULL ||
174 langLength >= ULOC_LANG_CAPACITY ||
175 scriptLength >= ULOC_SCRIPT_CAPACITY ||
176 regionLength >= ULOC_COUNTRY_CAPACITY) {
181 * ULOC_FULLNAME_CAPACITY will provide enough capacity
182 * that we can build a string that contains the language,
183 * script and region code without worrying about overrunning
184 * the user-supplied buffer.
186 char tagBuffer[ULOC_FULLNAME_CAPACITY];
187 int32_t tagLength = 0;
188 int32_t capacityRemaining = tagCapacity;
189 UBool regionAppended = FALSE;
191 if (langLength > 0) {
198 else if (alternateTags == NULL) {
200 * Append the value for an unknown language, if
201 * we found no language.
205 (int32_t)uprv_strlen(unknownLanguage),
211 * Parse the alternateTags string for the language.
213 char alternateLang[ULOC_LANG_CAPACITY];
214 int32_t alternateLangLength = sizeof(alternateLang);
216 alternateLangLength =
222 if(U_FAILURE(*err) ||
223 alternateLangLength >= ULOC_LANG_CAPACITY) {
226 else if (alternateLangLength == 0) {
228 * Append the value for an unknown language, if
229 * we found no language.
233 (int32_t)uprv_strlen(unknownLanguage),
246 if (scriptLength > 0) {
253 else if (alternateTags != NULL) {
255 * Parse the alternateTags string for the script.
257 char alternateScript[ULOC_SCRIPT_CAPACITY];
259 const int32_t alternateScriptLength =
263 sizeof(alternateScript),
266 if (U_FAILURE(*err) ||
267 alternateScriptLength >= ULOC_SCRIPT_CAPACITY) {
270 else if (alternateScriptLength > 0) {
273 alternateScriptLength,
279 if (regionLength > 0) {
286 regionAppended = TRUE;
288 else if (alternateTags != NULL) {
290 * Parse the alternateTags string for the region.
292 char alternateRegion[ULOC_COUNTRY_CAPACITY];
294 const int32_t alternateRegionLength =
298 sizeof(alternateRegion),
300 if (U_FAILURE(*err) ||
301 alternateRegionLength >= ULOC_COUNTRY_CAPACITY) {
304 else if (alternateRegionLength > 0) {
307 alternateRegionLength,
311 regionAppended = TRUE;
316 const int32_t toCopy =
317 tagLength >= tagCapacity ? tagCapacity : tagLength;
320 * Copy the partial tag from our internal buffer to the supplied
328 capacityRemaining -= toCopy;
331 if (trailingLength > 0) {
332 if (*trailing != '@' && capacityRemaining > 0) {
333 tag[tagLength++] = '_';
335 if (capacityRemaining > 0 && !regionAppended) {
336 /* extra separator is required */
337 tag[tagLength++] = '_';
342 if (capacityRemaining > 0) {
344 * Copy the trailing data into the supplied buffer. Use uprv_memmove, since we
345 * don't know if the user-supplied buffers overlap.
347 const int32_t toCopy =
348 trailingLength >= capacityRemaining ? capacityRemaining : trailingLength;
357 tagLength += trailingLength;
359 return u_terminateChars(
369 * An overflow indicates the locale ID passed in
370 * is ill-formed. If we got here, and there was
371 * no previous error, it's an implicit overflow.
373 if (*err == U_BUFFER_OVERFLOW_ERROR ||
375 *err = U_ILLEGAL_ARGUMENT_ERROR;
382 * Create a tag string from the supplied parameters. The lang, script and region
383 * parameters may be NULL pointers. If they are, their corresponding length parameters
384 * must be less than or equal to 0. If the lang parameter is an empty string, the
385 * default value for an unknown language is written to the output buffer.
387 * If the length of the new string exceeds the capacity of the output buffer,
388 * the function copies as many bytes to the output buffer as it can, and returns
389 * the error U_BUFFER_OVERFLOW_ERROR.
391 * If an illegal argument is provided, the function returns the error
392 * U_ILLEGAL_ARGUMENT_ERROR.
394 * @param lang The language tag to use.
395 * @param langLength The length of the language tag.
396 * @param script The script tag to use.
397 * @param scriptLength The length of the script tag.
398 * @param region The region tag to use.
399 * @param regionLength The length of the region tag.
400 * @param trailing Any trailing data to append to the new tag.
401 * @param trailingLength The length of the trailing data.
402 * @param tag The output buffer.
403 * @param tagCapacity The capacity of the output buffer.
404 * @param err A pointer to a UErrorCode for error reporting.
405 * @return The length of the tag string, which may be greater than tagCapacity.
407 static int32_t U_CALLCONV
412 int32_t scriptLength,
414 int32_t regionLength,
415 const char* trailing,
416 int32_t trailingLength,
421 return createTagStringWithAlternates(
437 * Parse the language, script, and region subtags from a tag string, and copy the
438 * results into the corresponding output parameters. The buffers are null-terminated,
439 * unless overflow occurs.
441 * The langLength, scriptLength, and regionLength parameters are input/output
442 * parameters, and must contain the capacity of their corresponding buffers on
443 * input. On output, they will contain the actual length of the buffers, not
444 * including the null terminator.
446 * If the length of any of the output subtags exceeds the capacity of the corresponding
447 * buffer, the function copies as many bytes to the output buffer as it can, and returns
448 * the error U_BUFFER_OVERFLOW_ERROR. It will not parse any more subtags once overflow
451 * If an illegal argument is provided, the function returns the error
452 * U_ILLEGAL_ARGUMENT_ERROR.
454 * @param localeID The locale ID to parse.
455 * @param lang The language tag buffer.
456 * @param langLength The length of the language tag.
457 * @param script The script tag buffer.
458 * @param scriptLength The length of the script tag.
459 * @param region The region tag buffer.
460 * @param regionLength The length of the region tag.
461 * @param err A pointer to a UErrorCode for error reporting.
462 * @return The number of chars of the localeID parameter consumed.
464 static int32_t U_CALLCONV
466 const char* localeID,
470 int32_t* scriptLength,
472 int32_t* regionLength,
475 const char* position = localeID;
476 int32_t subtagLength = 0;
478 if(U_FAILURE(*err) ||
481 langLength == NULL ||
483 scriptLength == NULL ||
485 regionLength == NULL) {
489 subtagLength = ulocimp_getLanguage(position, lang, *langLength, &position);
490 u_terminateChars(lang, *langLength, subtagLength, err);
493 * Note that we explicit consider U_STRING_NOT_TERMINATED_WARNING
494 * to be an error, because it indicates the user-supplied tag is
497 if(U_FAILURE(*err)) {
501 *langLength = subtagLength;
504 * If no language was present, use the value of unknownLanguage
505 * instead. Otherwise, move past any separator.
507 if (*langLength == 0) {
511 *langLength = (int32_t)uprv_strlen(lang);
513 else if (_isIDSeparator(*position)) {
517 subtagLength = ulocimp_getScript(position, script, *scriptLength, &position);
518 u_terminateChars(script, *scriptLength, subtagLength, err);
520 if(U_FAILURE(*err)) {
524 *scriptLength = subtagLength;
526 if (*scriptLength > 0) {
527 if (uprv_strnicmp(script, unknownScript, *scriptLength) == 0) {
529 * If the script part is the "unknown" script, then don't return it.
535 * Move past any separator.
537 if (_isIDSeparator(*position)) {
542 subtagLength = ulocimp_getCountry(position, region, *regionLength, &position);
543 u_terminateChars(region, *regionLength, subtagLength, err);
545 if(U_FAILURE(*err)) {
549 *regionLength = subtagLength;
551 if (*regionLength > 0) {
552 if (uprv_strnicmp(region, unknownRegion, *regionLength) == 0) {
554 * If the region part is the "unknown" region, then don't return it.
558 } else if (*position != 0 && *position != '@') {
559 /* back up over consumed trailing separator */
565 return (int32_t)(position - localeID);
570 * If we get here, we have no explicit error, it's the result of an
573 if (!U_FAILURE(*err)) {
574 *err = U_ILLEGAL_ARGUMENT_ERROR;
580 static int32_t U_CALLCONV
581 createLikelySubtagsString(
585 int32_t scriptLength,
587 int32_t regionLength,
588 const char* variants,
589 int32_t variantsLength,
595 * ULOC_FULLNAME_CAPACITY will provide enough capacity
596 * that we can build a string that contains the language,
597 * script and region code without worrying about overrunning
598 * the user-supplied buffer.
600 char tagBuffer[ULOC_FULLNAME_CAPACITY];
601 char likelySubtagsBuffer[ULOC_FULLNAME_CAPACITY];
603 if(U_FAILURE(*err)) {
608 * Try the language with the script and region first.
610 if (scriptLength > 0 && regionLength > 0) {
612 const char* likelySubtags = NULL;
626 if(U_FAILURE(*err)) {
634 sizeof(likelySubtagsBuffer),
636 if(U_FAILURE(*err)) {
640 if (likelySubtags != NULL) {
641 /* Always use the language tag from the
642 maximal string, since it may be more
643 specific than the one provided. */
644 return createTagStringWithAlternates(
661 * Try the language with just the script.
663 if (scriptLength > 0) {
665 const char* likelySubtags = NULL;
679 if(U_FAILURE(*err)) {
687 sizeof(likelySubtagsBuffer),
689 if(U_FAILURE(*err)) {
693 if (likelySubtags != NULL) {
694 /* Always use the language tag from the
695 maximal string, since it may be more
696 specific than the one provided. */
697 return createTagStringWithAlternates(
714 * Try the language with just the region.
716 if (regionLength > 0) {
718 const char* likelySubtags = NULL;
732 if(U_FAILURE(*err)) {
740 sizeof(likelySubtagsBuffer),
742 if(U_FAILURE(*err)) {
746 if (likelySubtags != NULL) {
747 /* Always use the language tag from the
748 maximal string, since it may be more
749 specific than the one provided. */
750 return createTagStringWithAlternates(
767 * Finally, try just the language.
770 const char* likelySubtags = NULL;
784 if(U_FAILURE(*err)) {
792 sizeof(likelySubtagsBuffer),
794 if(U_FAILURE(*err)) {
798 if (likelySubtags != NULL) {
799 /* Always use the language tag from the
800 maximal string, since it may be more
801 specific than the one provided. */
802 return createTagStringWithAlternates(
818 return u_terminateChars(
826 if (!U_FAILURE(*err)) {
827 *err = U_ILLEGAL_ARGUMENT_ERROR;
833 #define CHECK_TRAILING_VARIANT_SIZE(trailing, trailingLength) \
834 { int32_t count = 0; \
836 for (i = 0; i < trailingLength; i++) { \
837 if (trailing[i] == '-' || trailing[i] == '_') { \
842 } else if (trailing[i] == '@') { \
844 } else if (count > 8) { \
853 _uloc_addLikelySubtags(const char* localeID,
854 char* maximizedLocaleID,
855 int32_t maximizedLocaleIDCapacity,
858 char lang[ULOC_LANG_CAPACITY];
859 int32_t langLength = sizeof(lang);
860 char script[ULOC_SCRIPT_CAPACITY];
861 int32_t scriptLength = sizeof(script);
862 char region[ULOC_COUNTRY_CAPACITY];
863 int32_t regionLength = sizeof(region);
864 const char* trailing = "";
865 int32_t trailingLength = 0;
866 int32_t trailingIndex = 0;
867 int32_t resultLength = 0;
869 if(U_FAILURE(*err)) {
872 else if (localeID == NULL ||
873 maximizedLocaleID == NULL ||
874 maximizedLocaleIDCapacity <= 0) {
878 trailingIndex = parseTagString(
887 if(U_FAILURE(*err)) {
888 /* Overflow indicates an illegal argument error */
889 if (*err == U_BUFFER_OVERFLOW_ERROR) {
890 *err = U_ILLEGAL_ARGUMENT_ERROR;
896 /* Find the length of the trailing portion. */
897 while (_isIDSeparator(localeID[trailingIndex])) {
900 trailing = &localeID[trailingIndex];
901 trailingLength = (int32_t)uprv_strlen(trailing);
903 CHECK_TRAILING_VARIANT_SIZE(trailing, trailingLength);
906 createLikelySubtagsString(
916 maximizedLocaleIDCapacity,
919 if (resultLength == 0) {
920 const int32_t localIDLength = (int32_t)uprv_strlen(localeID);
923 * If we get here, we need to return localeID.
928 localIDLength <= maximizedLocaleIDCapacity ?
929 localIDLength : maximizedLocaleIDCapacity);
934 maximizedLocaleIDCapacity,
943 if (!U_FAILURE(*err)) {
944 *err = U_ILLEGAL_ARGUMENT_ERROR;
951 _uloc_minimizeSubtags(const char* localeID,
952 char* minimizedLocaleID,
953 int32_t minimizedLocaleIDCapacity,
957 * ULOC_FULLNAME_CAPACITY will provide enough capacity
958 * that we can build a string that contains the language,
959 * script and region code without worrying about overrunning
960 * the user-supplied buffer.
962 char maximizedTagBuffer[ULOC_FULLNAME_CAPACITY];
963 int32_t maximizedTagBufferLength = sizeof(maximizedTagBuffer);
965 char lang[ULOC_LANG_CAPACITY];
966 int32_t langLength = sizeof(lang);
967 char script[ULOC_SCRIPT_CAPACITY];
968 int32_t scriptLength = sizeof(script);
969 char region[ULOC_COUNTRY_CAPACITY];
970 int32_t regionLength = sizeof(region);
971 const char* trailing = "";
972 int32_t trailingLength = 0;
973 int32_t trailingIndex = 0;
975 if(U_FAILURE(*err)) {
978 else if (localeID == NULL ||
979 minimizedLocaleID == NULL ||
980 minimizedLocaleIDCapacity <= 0) {
994 if(U_FAILURE(*err)) {
996 /* Overflow indicates an illegal argument error */
997 if (*err == U_BUFFER_OVERFLOW_ERROR) {
998 *err = U_ILLEGAL_ARGUMENT_ERROR;
1004 /* Find the spot where the variants or the keywords begin, if any. */
1005 while (_isIDSeparator(localeID[trailingIndex])) {
1008 trailing = &localeID[trailingIndex];
1009 trailingLength = (int32_t)uprv_strlen(trailing);
1011 CHECK_TRAILING_VARIANT_SIZE(trailing, trailingLength);
1023 maximizedTagBufferLength,
1025 if(U_FAILURE(*err)) {
1030 * First, we need to first get the maximization
1031 * from AddLikelySubtags.
1033 maximizedTagBufferLength =
1034 uloc_addLikelySubtags(
1037 maximizedTagBufferLength,
1040 if(U_FAILURE(*err)) {
1045 * Start first with just the language.
1048 char tagBuffer[ULOC_FULLNAME_CAPACITY];
1050 const int32_t tagBufferLength =
1051 createLikelySubtagsString(
1064 if(U_FAILURE(*err)) {
1067 else if (uprv_strnicmp(
1070 tagBufferLength) == 0) {
1072 return createTagString(
1082 minimizedLocaleIDCapacity,
1088 * Next, try the language and region.
1090 if (regionLength > 0) {
1092 char tagBuffer[ULOC_FULLNAME_CAPACITY];
1094 const int32_t tagBufferLength =
1095 createLikelySubtagsString(
1108 if(U_FAILURE(*err)) {
1111 else if (uprv_strnicmp(
1114 tagBufferLength) == 0) {
1116 return createTagString(
1126 minimizedLocaleIDCapacity,
1132 * Finally, try the language and script. This is our last chance,
1133 * since trying with all three subtags would only yield the
1134 * maximal version that we already have.
1136 if (scriptLength > 0 && regionLength > 0) {
1137 char tagBuffer[ULOC_FULLNAME_CAPACITY];
1139 const int32_t tagBufferLength =
1140 createLikelySubtagsString(
1153 if(U_FAILURE(*err)) {
1156 else if (uprv_strnicmp(
1159 tagBufferLength) == 0) {
1161 return createTagString(
1171 minimizedLocaleIDCapacity,
1178 * If we got here, return the locale ID parameter.
1180 const int32_t localeIDLength = (int32_t)uprv_strlen(localeID);
1185 localeIDLength <= minimizedLocaleIDCapacity ?
1186 localeIDLength : minimizedLocaleIDCapacity);
1188 return u_terminateChars(
1190 minimizedLocaleIDCapacity,
1197 if (!U_FAILURE(*err)) {
1198 *err = U_ILLEGAL_ARGUMENT_ERROR;
1207 do_canonicalize(const char* localeID,
1209 int32_t bufferCapacity,
1218 if (*err == U_STRING_NOT_TERMINATED_WARNING ||
1219 *err == U_BUFFER_OVERFLOW_ERROR) {
1220 *err = U_ILLEGAL_ARGUMENT_ERROR;
1224 else if (U_FAILURE(*err)) {
1233 U_CAPI int32_t U_EXPORT2
1234 uloc_addLikelySubtags(const char* localeID,
1235 char* maximizedLocaleID,
1236 int32_t maximizedLocaleIDCapacity,
1239 char localeBuffer[ULOC_FULLNAME_CAPACITY];
1241 if (!do_canonicalize(
1244 sizeof(localeBuffer),
1249 return _uloc_addLikelySubtags(
1252 maximizedLocaleIDCapacity,
1257 U_CAPI int32_t U_EXPORT2
1258 uloc_minimizeSubtags(const char* localeID,
1259 char* minimizedLocaleID,
1260 int32_t minimizedLocaleIDCapacity,
1263 char localeBuffer[ULOC_FULLNAME_CAPACITY];
1265 if (!do_canonicalize(
1268 sizeof(localeBuffer),
1273 return _uloc_minimizeSubtags(
1276 minimizedLocaleIDCapacity,
1281 // Pairs of (language subtag, + or -) for finding out fast if common languages
1282 // are LTR (minus) or RTL (plus).
1283 static const char* LANG_DIR_STRING =
1284 "root-en-es-pt-zh-ja-ko-de-fr-it-ar+he+fa+ru-nl-pl-th-tr-";
1286 // Implemented here because this calls uloc_addLikelySubtags().
1287 U_CAPI UBool U_EXPORT2
1288 uloc_isRightToLeft(const char *locale) {
1289 UErrorCode errorCode = U_ZERO_ERROR;
1291 int32_t scriptLength = uloc_getScript(locale, script, UPRV_LENGTHOF(script), &errorCode);
1292 if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING ||
1293 scriptLength == 0) {
1294 // Fastpath: We know the likely scripts and their writing direction
1295 // for some common languages.
1296 errorCode = U_ZERO_ERROR;
1298 int32_t langLength = uloc_getLanguage(locale, lang, UPRV_LENGTHOF(lang), &errorCode);
1299 if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING ||
1303 const char* langPtr = uprv_strstr(LANG_DIR_STRING, lang);
1304 if (langPtr != NULL) {
1305 switch (langPtr[langLength]) {
1306 case '-': return FALSE;
1307 case '+': return TRUE;
1308 default: break; // partial match of a longer code
1311 // Otherwise, find the likely script.
1312 errorCode = U_ZERO_ERROR;
1313 char likely[ULOC_FULLNAME_CAPACITY];
1314 (void)uloc_addLikelySubtags(locale, likely, UPRV_LENGTHOF(likely), &errorCode);
1315 if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING) {
1318 scriptLength = uloc_getScript(likely, script, UPRV_LENGTHOF(script), &errorCode);
1319 if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING ||
1320 scriptLength == 0) {
1324 UScriptCode scriptCode = (UScriptCode)u_getPropertyValueEnum(UCHAR_SCRIPT, script);
1325 return uscript_isRightToLeft(scriptCode);
1331 Locale::isRightToLeft() const {
1332 return uloc_isRightToLeft(getBaseName());
1337 // The following must at least allow for rg key value (6) plus terminator (1).
1338 #define ULOC_RG_BUFLEN 8
1340 U_CAPI int32_t U_EXPORT2
1341 ulocimp_getRegionForSupplementalData(const char *localeID, UBool inferRegion,
1342 char *region, int32_t regionCapacity, UErrorCode* status) {
1343 if (U_FAILURE(*status)) {
1346 char rgBuf[ULOC_RG_BUFLEN];
1347 UErrorCode rgStatus = U_ZERO_ERROR;
1349 // First check for rg keyword value
1350 int32_t rgLen = uloc_getKeywordValue(localeID, "rg", rgBuf, ULOC_RG_BUFLEN, &rgStatus);
1351 if (U_FAILURE(rgStatus) || rgLen != 6) {
1354 // rgBuf guaranteed to be zero terminated here, with text len 6
1355 char *rgPtr = rgBuf;
1356 for (; *rgPtr!= 0; rgPtr++) {
1357 *rgPtr = uprv_toupper(*rgPtr);
1359 rgLen = (uprv_strcmp(rgBuf+2, "ZZZZ") == 0)? 2: 0;
1363 // No valid rg keyword value, try for unicode_region_subtag
1364 rgLen = uloc_getCountry(localeID, rgBuf, ULOC_RG_BUFLEN, status);
1365 if (U_FAILURE(*status)) {
1367 } else if (rgLen == 0 && inferRegion) {
1368 // no unicode_region_subtag but inferRegion TRUE, try likely subtags
1369 char locBuf[ULOC_FULLNAME_CAPACITY];
1370 rgStatus = U_ZERO_ERROR;
1371 (void)uloc_addLikelySubtags(localeID, locBuf, ULOC_FULLNAME_CAPACITY, &rgStatus);
1372 if (U_SUCCESS(rgStatus)) {
1373 rgLen = uloc_getCountry(locBuf, rgBuf, ULOC_RG_BUFLEN, status);
1374 if (U_FAILURE(*status)) {
1382 uprv_strncpy(region, rgBuf, regionCapacity);
1383 return u_terminateChars(region, regionCapacity, rgLen, status);