• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2014-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 *
9 *
10 * File REGION.CPP
11 *
12 * Modification History:*
13 *   Date        Name        Description
14 * 01/15/13      Emmons      Original Port from ICU4J
15 ********************************************************************************
16 */
17 
18 /**
19  * \file
20  * \brief C++ API: Region classes (territory containment)
21  */
22 
23 #include "unicode/region.h"
24 #include "unicode/utypes.h"
25 #include "unicode/uobject.h"
26 #include "unicode/unistr.h"
27 #include "unicode/ures.h"
28 #include "ucln_in.h"
29 #include "cstring.h"
30 #include "mutex.h"
31 #include "uhash.h"
32 #include "umutex.h"
33 #include "uresimp.h"
34 #include "region_impl.h"
35 #include "util.h"
36 
37 #if !UCONFIG_NO_FORMATTING
38 
39 
40 U_CDECL_BEGIN
41 
42 /**
43  * Cleanup callback func
44  */
region_cleanup()45 static UBool U_CALLCONV region_cleanup()
46 {
47     icu::Region::cleanupRegionData();
48 
49     return true;
50 }
51 
52 U_CDECL_END
53 
54 U_NAMESPACE_BEGIN
55 
56 static UInitOnce gRegionDataInitOnce {};
57 static UVector* availableRegions[URGN_LIMIT];
58 
59 static UHashtable *regionAliases = nullptr;
60 static UHashtable *regionIDMap = nullptr;
61 static UHashtable *numericCodeMap = nullptr;
62 static UVector *allRegions = nullptr;
63 
64 static const char16_t UNKNOWN_REGION_ID [] = { 0x5A, 0x5A, 0 };  /* "ZZ" */
65 static const char16_t OUTLYING_OCEANIA_REGION_ID [] = { 0x51, 0x4F, 0 };  /* "QO" */
66 static const char16_t WORLD_ID [] = { 0x30, 0x30, 0x31, 0 };  /* "001" */
67 static const char16_t RANGE_MARKER = 0x7E; /* '~' */
68 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RegionNameEnumeration)69 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RegionNameEnumeration)
70 
71 /*
72  * Initializes the region data from the ICU resource bundles.  The region data
73  * contains the basic relationships such as which regions are known, what the numeric
74  * codes are, any known aliases, and the territory containment data.
75  *
76  * If the region data has already loaded, then this method simply returns without doing
77  * anything meaningful.
78  */
79 void U_CALLCONV Region::loadRegionData(UErrorCode &status) {
80 
81     // Construct service objs first
82     LocalUHashtablePointer newRegionIDMap(uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, nullptr, &status));
83     LocalUHashtablePointer newNumericCodeMap(uhash_open(uhash_hashLong,uhash_compareLong,nullptr,&status));
84     LocalUHashtablePointer newRegionAliases(uhash_open(uhash_hashUnicodeString,uhash_compareUnicodeString,nullptr,&status));
85 
86     LocalPointer<UVector> continents(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
87     LocalPointer<UVector> groupings(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
88     LocalPointer<UVector> lpAllRegions(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
89     allRegions = lpAllRegions.orphan();
90 
91     LocalUResourceBundlePointer metadata(ures_openDirect(nullptr,"metadata",&status));
92     LocalUResourceBundlePointer metadataAlias(ures_getByKey(metadata.getAlias(),"alias",nullptr,&status));
93     LocalUResourceBundlePointer territoryAlias(ures_getByKey(metadataAlias.getAlias(),"territory",nullptr,&status));
94 
95     LocalUResourceBundlePointer supplementalData(ures_openDirect(nullptr,"supplementalData",&status));
96     LocalUResourceBundlePointer codeMappings(ures_getByKey(supplementalData.getAlias(),"codeMappings",nullptr,&status));
97 
98     LocalUResourceBundlePointer idValidity(ures_getByKey(supplementalData.getAlias(),"idValidity",nullptr,&status));
99     LocalUResourceBundlePointer regionList(ures_getByKey(idValidity.getAlias(),"region",nullptr,&status));
100     LocalUResourceBundlePointer regionRegular(ures_getByKey(regionList.getAlias(),"regular",nullptr,&status));
101     LocalUResourceBundlePointer regionMacro(ures_getByKey(regionList.getAlias(),"macroregion",nullptr,&status));
102     LocalUResourceBundlePointer regionUnknown(ures_getByKey(regionList.getAlias(),"unknown",nullptr,&status));
103 
104     LocalUResourceBundlePointer territoryContainment(ures_getByKey(supplementalData.getAlias(),"territoryContainment",nullptr,&status));
105     LocalUResourceBundlePointer worldContainment(ures_getByKey(territoryContainment.getAlias(),"001",nullptr,&status));
106     LocalUResourceBundlePointer groupingContainment(ures_getByKey(territoryContainment.getAlias(),"grouping",nullptr,&status));
107 
108     ucln_i18n_registerCleanup(UCLN_I18N_REGION, region_cleanup);
109     if (U_FAILURE(status)) {
110         return;
111     }
112 
113     // now, initialize
114     uhash_setValueDeleter(newRegionIDMap.getAlias(), uprv_deleteUObject);  // regionIDMap owns objs
115     uhash_setKeyDeleter(newRegionAliases.getAlias(), uprv_deleteUObject);  // regionAliases owns the string keys
116 
117 
118     while (U_SUCCESS(status) && ures_hasNext(regionRegular.getAlias())) {
119         UnicodeString regionName = ures_getNextUnicodeString(regionRegular.getAlias(),nullptr,&status);
120         int32_t rangeMarkerLocation = regionName.indexOf(RANGE_MARKER);
121         char16_t buf[6];
122         regionName.extract(buf,6,status);
123         if ( rangeMarkerLocation > 0 ) {
124             char16_t endRange = regionName.charAt(rangeMarkerLocation+1);
125             buf[rangeMarkerLocation] = 0;
126             while (U_SUCCESS(status) && buf[rangeMarkerLocation-1] <= endRange) {
127                 LocalPointer<UnicodeString> newRegion(new UnicodeString(buf), status);
128                 allRegions->adoptElement(newRegion.orphan(), status);
129                 buf[rangeMarkerLocation-1]++;
130             }
131         } else {
132             LocalPointer<UnicodeString> newRegion(new UnicodeString(regionName), status);
133             allRegions->adoptElement(newRegion.orphan(), status);
134         }
135     }
136 
137     while (U_SUCCESS(status) && ures_hasNext(regionMacro.getAlias())) {
138         UnicodeString regionName = ures_getNextUnicodeString(regionMacro.getAlias(),nullptr,&status);
139         int32_t rangeMarkerLocation = regionName.indexOf(RANGE_MARKER);
140         char16_t buf[6];
141         regionName.extract(buf,6,status);
142         if ( rangeMarkerLocation > 0 ) {
143             char16_t endRange = regionName.charAt(rangeMarkerLocation+1);
144             buf[rangeMarkerLocation] = 0;
145             while ( buf[rangeMarkerLocation-1] <= endRange && U_SUCCESS(status)) {
146                 LocalPointer<UnicodeString> newRegion(new UnicodeString(buf), status);
147                 allRegions->adoptElement(newRegion.orphan(),status);
148                 buf[rangeMarkerLocation-1]++;
149             }
150         } else {
151             LocalPointer<UnicodeString> newRegion(new UnicodeString(regionName), status);
152             allRegions->adoptElement(newRegion.orphan(),status);
153         }
154     }
155 
156     while (U_SUCCESS(status) && ures_hasNext(regionUnknown.getAlias())) {
157         LocalPointer<UnicodeString> regionName (
158             new UnicodeString(ures_getNextUnicodeString(regionUnknown.getAlias(), nullptr, &status), status));
159         allRegions->adoptElement(regionName.orphan(),status);
160     }
161 
162     while (U_SUCCESS(status) && ures_hasNext(worldContainment.getAlias())) {
163         UnicodeString *continentName = new UnicodeString(ures_getNextUnicodeString(worldContainment.getAlias(),nullptr,&status));
164         continents->adoptElement(continentName,status);
165     }
166     if (U_FAILURE(status)) {
167         return;
168     }
169 
170     for ( int32_t i = 0 ; i < allRegions->size() ; i++ ) {
171         LocalPointer<Region> r(new Region(), status);
172         if ( U_FAILURE(status) ) {
173            return;
174         }
175         UnicodeString *regionName = (UnicodeString *)allRegions->elementAt(i);
176         r->idStr = *regionName;
177 
178         r->idStr.extract(0,r->idStr.length(),r->id,sizeof(r->id),US_INV);
179         r->fType = URGN_TERRITORY; // Only temporary - figure out the real type later once the aliases are known.
180 
181         int32_t pos = 0;
182         int32_t result = ICU_Utility::parseAsciiInteger(r->idStr, pos);
183         if (pos > 0) {
184             r->code = result; // Convert string to number
185             uhash_iput(newNumericCodeMap.getAlias(),r->code,(void *)(r.getAlias()),&status);
186             r->fType = URGN_SUBCONTINENT;
187         } else {
188             r->code = -1;
189         }
190         void* idStrAlias = (void*)&(r->idStr); // about to orphan 'r'. Save this off.
191         uhash_put(newRegionIDMap.getAlias(),idStrAlias,(void *)(r.orphan()),&status); // regionIDMap takes ownership
192     }
193 
194     UResourceBundle *groupingBundle = nullptr;
195     while (U_SUCCESS(status) && ures_hasNext(groupingContainment.getAlias())) {
196         groupingBundle = ures_getNextResource(groupingContainment.getAlias(), groupingBundle, &status);
197         if (U_FAILURE(status)) {
198             break;
199         }
200         UnicodeString *groupingName = new UnicodeString(ures_getKey(groupingBundle), -1, US_INV);
201         LocalPointer<UnicodeString> lpGroupingName(groupingName, status);
202         groupings->adoptElement(lpGroupingName.orphan(), status);
203         if (U_FAILURE(status)) {
204             break;
205         }
206         Region *grouping = (Region *) uhash_get(newRegionIDMap.getAlias(), groupingName);
207         if (grouping != nullptr) {
208             for (int32_t i = 0; i < ures_getSize(groupingBundle) && U_SUCCESS(status); i++) {
209                 UnicodeString child = ures_getUnicodeStringByIndex(groupingBundle, i, &status);
210                 if (U_SUCCESS(status)) {
211                     if (grouping->containedRegions == nullptr) {
212                         LocalPointer<UVector> lpContainedRegions(
213                             new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
214                         grouping->containedRegions = lpContainedRegions.orphan();
215                         if (U_FAILURE(status)) {
216                             break;
217                         }
218                     }
219                     LocalPointer<UnicodeString> lpChildCopy(new UnicodeString(child), status);
220                     grouping->containedRegions->adoptElement(lpChildCopy.orphan(), status);
221                 }
222             }
223         }
224     }
225     ures_close(groupingBundle);
226 
227     // Process the territory aliases
228     while (U_SUCCESS(status) && ures_hasNext(territoryAlias.getAlias())) {
229         LocalUResourceBundlePointer res(ures_getNextResource(territoryAlias.getAlias(),nullptr,&status));
230         const char *aliasFrom = ures_getKey(res.getAlias());
231         LocalPointer<UnicodeString> aliasFromStr(new UnicodeString(aliasFrom, -1, US_INV), status);
232         UnicodeString aliasTo = ures_getUnicodeStringByKey(res.getAlias(),"replacement",&status);
233         res.adoptInstead(nullptr);
234 
235         const Region *aliasToRegion = (Region *) uhash_get(newRegionIDMap.getAlias(),&aliasTo);
236         Region *aliasFromRegion = (Region *)uhash_get(newRegionIDMap.getAlias(),aliasFromStr.getAlias());
237 
238         if ( aliasToRegion != nullptr && aliasFromRegion == nullptr ) { // This is just an alias from some string to a region
239             uhash_put(newRegionAliases.getAlias(),(void *)aliasFromStr.orphan(), (void *)aliasToRegion,&status);
240         } else {
241             if ( aliasFromRegion == nullptr ) { // Deprecated region code not in the primary codes list - so need to create a deprecated region for it.
242                 LocalPointer<Region> newRgn(new Region, status);
243                 if ( U_SUCCESS(status) ) {
244                     aliasFromRegion = newRgn.orphan();
245                 } else {
246                     return; // error out
247                 }
248                 aliasFromRegion->idStr.setTo(*aliasFromStr);
249                 aliasFromRegion->idStr.extract(0,aliasFromRegion->idStr.length(),aliasFromRegion->id,sizeof(aliasFromRegion->id),US_INV);
250                 uhash_put(newRegionIDMap.getAlias(),(void *)&(aliasFromRegion->idStr),(void *)aliasFromRegion,&status);
251                 int32_t pos = 0;
252                 int32_t result = ICU_Utility::parseAsciiInteger(aliasFromRegion->idStr, pos);
253                 if ( pos > 0 ) {
254                     aliasFromRegion->code = result; // Convert string to number
255                     uhash_iput(newNumericCodeMap.getAlias(),aliasFromRegion->code,(void *)aliasFromRegion,&status);
256                 } else {
257                     aliasFromRegion->code = -1;
258                 }
259                 aliasFromRegion->fType = URGN_DEPRECATED;
260             } else {
261                 aliasFromRegion->fType = URGN_DEPRECATED;
262             }
263 
264             {
265                 LocalPointer<UVector> newPreferredValues(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
266                 aliasFromRegion->preferredValues = newPreferredValues.orphan();
267             }
268             if( U_FAILURE(status)) {
269                 return;
270             }
271             UnicodeString currentRegion;
272             //currentRegion.remove();   TODO: was already 0 length?
273             for (int32_t i = 0 ; i < aliasTo.length() && U_SUCCESS(status); i++ ) {
274                 if ( aliasTo.charAt(i) != 0x0020 ) {
275                     currentRegion.append(aliasTo.charAt(i));
276                 }
277                 if ( aliasTo.charAt(i) == 0x0020 || i+1 == aliasTo.length() ) {
278                     Region *target = (Region *)uhash_get(newRegionIDMap.getAlias(),(void *)&currentRegion);
279                     if (target) {
280                         LocalPointer<UnicodeString> preferredValue(new UnicodeString(target->idStr), status);
281                         aliasFromRegion->preferredValues->adoptElement(preferredValue.orphan(),status);  // may add null if err
282                     }
283                     currentRegion.remove();
284                 }
285             }
286         }
287     }
288 
289     // Process the code mappings - This will allow us to assign numeric codes to most of the territories.
290     while (U_SUCCESS(status) && ures_hasNext(codeMappings.getAlias())) {
291         UResourceBundle *mapping = ures_getNextResource(codeMappings.getAlias(),nullptr,&status);
292         if (U_SUCCESS(status) && ures_getType(mapping) == URES_ARRAY && ures_getSize(mapping) == 3) {
293             UnicodeString codeMappingID = ures_getUnicodeStringByIndex(mapping,0,&status);
294             UnicodeString codeMappingNumber = ures_getUnicodeStringByIndex(mapping,1,&status);
295             UnicodeString codeMapping3Letter = ures_getUnicodeStringByIndex(mapping,2,&status);
296 
297             Region *r = (Region *)uhash_get(newRegionIDMap.getAlias(),(void *)&codeMappingID);
298             if ( r ) {
299                 int32_t pos = 0;
300                 int32_t result = ICU_Utility::parseAsciiInteger(codeMappingNumber, pos);
301                 if ( pos > 0 ) {
302                     r->code = result; // Convert string to number
303                     uhash_iput(newNumericCodeMap.getAlias(),r->code,(void *)r,&status);
304                 }
305                 LocalPointer<UnicodeString> code3(new UnicodeString(codeMapping3Letter), status);
306                 uhash_put(newRegionAliases.getAlias(),(void *)code3.orphan(), (void *)r,&status);
307             }
308         }
309         ures_close(mapping);
310     }
311 
312     // Now fill in the special cases for WORLD, UNKNOWN, CONTINENTS, and GROUPINGS
313     Region *r;
314     UnicodeString WORLD_ID_STRING(WORLD_ID);
315     r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&WORLD_ID_STRING);
316     if ( r ) {
317         r->fType = URGN_WORLD;
318     }
319 
320     UnicodeString UNKNOWN_REGION_ID_STRING(UNKNOWN_REGION_ID);
321     r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&UNKNOWN_REGION_ID_STRING);
322     if ( r ) {
323         r->fType = URGN_UNKNOWN;
324     }
325 
326     for ( int32_t i = 0 ; i < continents->size() ; i++ ) {
327         r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)continents->elementAt(i));
328         if ( r ) {
329             r->fType = URGN_CONTINENT;
330         }
331     }
332 
333     for ( int32_t i = 0 ; i < groupings->size() ; i++ ) {
334         r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)groupings->elementAt(i));
335         if ( r ) {
336             r->fType = URGN_GROUPING;
337         }
338     }
339 
340     // Special case: The region code "QO" (Outlying Oceania) is a subcontinent code added by CLDR
341     // even though it looks like a territory code.  Need to handle it here.
342 
343     UnicodeString OUTLYING_OCEANIA_REGION_ID_STRING(OUTLYING_OCEANIA_REGION_ID);
344     r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&OUTLYING_OCEANIA_REGION_ID_STRING);
345     if ( r ) {
346         r->fType = URGN_SUBCONTINENT;
347     }
348 
349     // Load territory containment info from the supplemental data.
350     while ( ures_hasNext(territoryContainment.getAlias()) ) {
351         LocalUResourceBundlePointer mapping(ures_getNextResource(territoryContainment.getAlias(),nullptr,&status));
352         if( U_FAILURE(status) ) {
353             return;  // error out
354         }
355         const char *parent = ures_getKey(mapping.getAlias());
356         if (uprv_strcmp(parent, "containedGroupings") == 0 || uprv_strcmp(parent, "deprecated") == 0) {
357             continue; // handle new pseudo-parent types added in ICU data per cldrbug 7808; for now just skip.
358             // #11232 is to do something useful with these.
359         }
360         UnicodeString parentStr = UnicodeString(parent, -1 , US_INV);
361         Region *parentRegion = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&parentStr);
362 
363         for ( int j = 0 ; j < ures_getSize(mapping.getAlias()); j++ ) {
364             UnicodeString child = ures_getUnicodeStringByIndex(mapping.getAlias(),j,&status);
365             Region *childRegion = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&child);
366             if ( parentRegion != nullptr && childRegion != nullptr ) {
367 
368                 // Add the child region to the set of regions contained by the parent
369                 if (parentRegion->containedRegions == nullptr) {
370                     LocalPointer<UVector> lpContainedRegions(
371                         new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
372                     parentRegion->containedRegions = lpContainedRegions.orphan();
373                     if (U_FAILURE(status)) {
374                         return;
375                     }
376                 }
377 
378                 LocalPointer<UnicodeString> childStr(new UnicodeString(), status);
379                 if (U_FAILURE(status)) {
380                     return;  // error out
381                 }
382                 childStr->fastCopyFrom(childRegion->idStr);
383                 parentRegion->containedRegions->adoptElement(childStr.orphan(),status);
384                 if (U_FAILURE(status)) {
385                     return;
386                 }
387 
388                 // Set the parent region to be the containing region of the child.
389                 // Regions of type GROUPING can't be set as the parent, since another region
390                 // such as a SUBCONTINENT, CONTINENT, or WORLD must always be the parent.
391                 if ( parentRegion->fType != URGN_GROUPING) {
392                     childRegion->containingRegion = parentRegion;
393                 }
394             }
395         }
396     }
397 
398     // Create the availableRegions lists
399     int32_t pos = UHASH_FIRST;
400     while ( const UHashElement* element = uhash_nextElement(newRegionIDMap.getAlias(),&pos)) {
401         Region *ar = (Region *)element->value.pointer;
402         if ( availableRegions[ar->fType] == nullptr ) {
403             LocalPointer<UVector> newAr(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
404             availableRegions[ar->fType] = newAr.orphan();
405         }
406         LocalPointer<UnicodeString> arString(new UnicodeString(ar->idStr), status);
407         if( U_FAILURE(status) ) {
408             return;  // error out
409         }
410         availableRegions[ar->fType]->adoptElement(arString.orphan(), status);
411     }
412 
413     // copy hashtables
414     numericCodeMap = newNumericCodeMap.orphan();
415     regionIDMap = newRegionIDMap.orphan();
416     regionAliases = newRegionAliases.orphan();
417 }
418 
cleanupRegionData()419 void Region::cleanupRegionData() {
420     for (int32_t i = 0 ; i < URGN_LIMIT ; i++ ) {
421         if ( availableRegions[i] ) {
422             delete availableRegions[i];
423             availableRegions[i] = nullptr;
424         }
425     }
426 
427     if (regionAliases) {
428         uhash_close(regionAliases);
429     }
430 
431     if (numericCodeMap) {
432         uhash_close(numericCodeMap);
433     }
434 
435     if (regionIDMap) {
436         uhash_close(regionIDMap);
437     }
438     if (allRegions) {
439         delete allRegions;
440         allRegions = nullptr;
441     }
442 
443     regionAliases = numericCodeMap = regionIDMap = nullptr;
444 
445     gRegionDataInitOnce.reset();
446 }
447 
Region()448 Region::Region ()
449         : code(-1),
450           fType(URGN_UNKNOWN),
451           containingRegion(nullptr),
452           containedRegions(nullptr),
453           preferredValues(nullptr) {
454     id[0] = 0;
455 }
456 
~Region()457 Region::~Region () {
458         if (containedRegions) {
459             delete containedRegions;
460         }
461         if (preferredValues) {
462             delete preferredValues;
463         }
464 }
465 
466 /**
467  * Returns true if the two regions are equal.
468  * Per PMC, just use pointer compare, since we have at most one instance of each Region.
469  */
470 bool
operator ==(const Region & that) const471 Region::operator==(const Region &that) const {
472     return (idStr == that.idStr);
473 }
474 
475 /**
476  * Returns true if the two regions are NOT equal; that is, if operator ==() returns false.
477  * Per PMC, just use pointer compare, since we have at most one instance of each Region.
478  */
479 bool
operator !=(const Region & that) const480 Region::operator!=(const Region &that) const {
481         return (idStr != that.idStr);
482 }
483 
484 /**
485  * Returns a pointer to a Region using the given region code.  The region code can be either 2-letter ISO code,
486  * 3-letter ISO code,  UNM.49 numeric code, or other valid Unicode Region Code as defined by the LDML specification.
487  * The identifier will be canonicalized internally using the supplemental metadata as defined in the CLDR.
488  * If the region code is nullptr or not recognized, the appropriate error code will be set ( U_ILLEGAL_ARGUMENT_ERROR )
489  */
490 const Region* U_EXPORT2
getInstance(const char * region_code,UErrorCode & status)491 Region::getInstance(const char *region_code, UErrorCode &status) {
492 
493     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
494     if (U_FAILURE(status)) {
495         return nullptr;
496     }
497 
498     if ( !region_code ) {
499         status = U_ILLEGAL_ARGUMENT_ERROR;
500         return nullptr;
501     }
502 
503     UnicodeString regionCodeString = UnicodeString(region_code, -1, US_INV);
504     Region *r = (Region *)uhash_get(regionIDMap,(void *)&regionCodeString);
505 
506     if ( !r ) {
507         r = (Region *)uhash_get(regionAliases,(void *)&regionCodeString);
508     }
509 
510     if ( !r ) { // Unknown region code
511         status = U_ILLEGAL_ARGUMENT_ERROR;
512         return nullptr;
513     }
514 
515     if ( r->fType == URGN_DEPRECATED && r->preferredValues->size() == 1) {
516         StringEnumeration *pv = r->getPreferredValues(status);
517         pv->reset(status);
518         const UnicodeString *ustr = pv->snext(status);
519         r = (Region *)uhash_get(regionIDMap,(void *)ustr);
520         delete pv;
521     }
522 
523     return r;
524 
525 }
526 
527 /**
528  * Returns a pointer to a Region using the given numeric region code. If the numeric region code is not recognized,
529  * the appropriate error code will be set ( U_ILLEGAL_ARGUMENT_ERROR ).
530  */
531 const Region* U_EXPORT2
getInstance(int32_t code,UErrorCode & status)532 Region::getInstance (int32_t code, UErrorCode &status) {
533 
534     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
535     if (U_FAILURE(status)) {
536         return nullptr;
537     }
538 
539     Region *r = (Region *)uhash_iget(numericCodeMap,code);
540 
541     if ( !r ) { // Just in case there's an alias that's numeric, try to find it.
542         UnicodeString id;
543         ICU_Utility::appendNumber(id, code, 10, 1);
544         r = (Region *)uhash_get(regionAliases,&id);
545     }
546 
547     if( U_FAILURE(status) ) {
548         return nullptr;
549     }
550 
551     if ( !r ) {
552         status = U_ILLEGAL_ARGUMENT_ERROR;
553         return nullptr;
554     }
555 
556     if ( r->fType == URGN_DEPRECATED && r->preferredValues->size() == 1) {
557         StringEnumeration *pv = r->getPreferredValues(status);
558         pv->reset(status);
559         const UnicodeString *ustr = pv->snext(status);
560         r = (Region *)uhash_get(regionIDMap,(void *)ustr);
561         delete pv;
562     }
563 
564     return r;
565 }
566 
567 
568 /**
569  * Returns an enumeration over the IDs of all known regions that match the given type.
570  */
571 StringEnumeration* U_EXPORT2
getAvailable(URegionType type,UErrorCode & status)572 Region::getAvailable(URegionType type, UErrorCode &status) {
573     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
574     if (U_FAILURE(status)) {
575         return nullptr;
576     }
577     return new RegionNameEnumeration(availableRegions[type],status);
578 }
579 
580 /**
581  * Returns a pointer to the region that contains this region.  Returns nullptr if this region is code "001" (World)
582  * or "ZZ" (Unknown region). For example, calling this method with region "IT" (Italy) returns the
583  * region "039" (Southern Europe).
584  */
585 const Region*
getContainingRegion() const586 Region::getContainingRegion() const {
587     UErrorCode status = U_ZERO_ERROR;
588     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
589     return containingRegion;
590 }
591 
592 /**
593  * Return a pointer to the region that geographically contains this region and matches the given type,
594  * moving multiple steps up the containment chain if necessary.  Returns nullptr if no containing region can be found
595  * that matches the given type. Note: The URegionTypes = "URGN_GROUPING", "URGN_DEPRECATED", or "URGN_UNKNOWN"
596  * are not appropriate for use in this API. nullptr will be returned in this case. For example, calling this method
597  * with region "IT" (Italy) for type "URGN_CONTINENT" returns the region "150" ( Europe ).
598  */
599 const Region*
getContainingRegion(URegionType type) const600 Region::getContainingRegion(URegionType type) const {
601     UErrorCode status = U_ZERO_ERROR;
602     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
603     if ( containingRegion == nullptr ) {
604         return nullptr;
605     }
606 
607     return ( containingRegion->fType == type)? containingRegion: containingRegion->getContainingRegion(type);
608 }
609 
610 /**
611  * Return an enumeration over the IDs of all the regions that are immediate children of this region in the
612  * region hierarchy. These returned regions could be either macro regions, territories, or a mixture of the two,
613  * depending on the containment data as defined in CLDR.  This API may return nullptr if this region doesn't have
614  * any sub-regions. For example, calling this method with region "150" (Europe) returns an enumeration containing
615  * the various sub regions of Europe - "039" (Southern Europe) - "151" (Eastern Europe) - "154" (Northern Europe)
616  * and "155" (Western Europe).
617  */
618 StringEnumeration*
getContainedRegions(UErrorCode & status) const619 Region::getContainedRegions(UErrorCode &status) const {
620     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
621     if (U_FAILURE(status)) {
622         return nullptr;
623     }
624     return new RegionNameEnumeration(containedRegions,status);
625 }
626 
627 /**
628  * Returns an enumeration over the IDs of all the regions that are children of this region anywhere in the region
629  * hierarchy and match the given type.  This API may return an empty enumeration if this region doesn't have any
630  * sub-regions that match the given type. For example, calling this method with region "150" (Europe) and type
631  * "URGN_TERRITORY" returns a set containing all the territories in Europe ( "FR" (France) - "IT" (Italy) - "DE" (Germany) etc. )
632  */
633 StringEnumeration*
getContainedRegions(URegionType type,UErrorCode & status) const634 Region::getContainedRegions( URegionType type, UErrorCode &status ) const {
635     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
636 
637     UVector result(nullptr, uhash_compareChars, status);
638     LocalPointer<StringEnumeration> cr(getContainedRegions(status), status);
639     if (U_FAILURE(status)) {
640         return nullptr;
641     }
642 
643     const char *regionId;
644     while((regionId = cr->next(nullptr, status)) != nullptr && U_SUCCESS(status)) {
645         const Region *r = Region::getInstance(regionId, status);
646         if ( r->getType() == type) {
647             result.addElement(const_cast<UnicodeString *>(&r->idStr), status);
648         } else {
649             LocalPointer<StringEnumeration> children(r->getContainedRegions(type, status));
650             const char *id2;
651             while(U_SUCCESS(status) && ((id2 = children->next(nullptr, status)) != nullptr)) {
652                 const Region *r2 = Region::getInstance(id2,status);
653                 result.addElement(const_cast<UnicodeString *>(&r2->idStr), status);
654             }
655         }
656     }
657     LocalPointer<StringEnumeration> resultEnumeration(
658         new RegionNameEnumeration(&result, status), status);
659     return U_SUCCESS(status) ? resultEnumeration.orphan() : nullptr;
660 }
661 
662 /**
663  * Returns true if this region contains the supplied other region anywhere in the region hierarchy.
664  */
665 UBool
contains(const Region & other) const666 Region::contains(const Region &other) const {
667     UErrorCode status = U_ZERO_ERROR;
668     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
669 
670     if (!containedRegions) {
671           return false;
672     }
673     if (containedRegions->contains((void *)&other.idStr)) {
674         return true;
675     } else {
676         for ( int32_t i = 0 ; i < containedRegions->size() ; i++ ) {
677             UnicodeString *crStr = (UnicodeString *)containedRegions->elementAt(i);
678             Region *cr = (Region *) uhash_get(regionIDMap,(void *)crStr);
679             if ( cr && cr->contains(other) ) {
680                 return true;
681             }
682         }
683     }
684 
685     return false;
686 }
687 
688 /**
689  * For deprecated regions, return an enumeration over the IDs of the regions that are the preferred replacement
690  * regions for this region.  Returns nullptr for a non-deprecated region.  For example, calling this method with region
691  * "SU" (Soviet Union) would return a list of the regions containing "RU" (Russia), "AM" (Armenia), "AZ" (Azerbaijan), etc...
692  */
693 StringEnumeration*
getPreferredValues(UErrorCode & status) const694 Region::getPreferredValues(UErrorCode &status) const {
695     umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
696     if (U_FAILURE(status) || fType != URGN_DEPRECATED) {
697         return nullptr;
698     }
699     return new RegionNameEnumeration(preferredValues,status);
700 }
701 
702 
703 /**
704  * Return this region's canonical region code.
705  */
706 const char*
getRegionCode() const707 Region::getRegionCode() const {
708     return id;
709 }
710 
711 int32_t
getNumericCode() const712 Region::getNumericCode() const {
713     return code;
714 }
715 
716 /**
717  * Returns the region type of this region.
718  */
719 URegionType
getType() const720 Region::getType() const {
721     return fType;
722 }
723 
RegionNameEnumeration(UVector * nameList,UErrorCode & status)724 RegionNameEnumeration::RegionNameEnumeration(UVector *nameList, UErrorCode& status) :
725         pos(0), fRegionNames(nullptr) {
726     // TODO: https://unicode-org.atlassian.net/browse/ICU-21829
727     // Is all of the copying going on here really necessary?
728     if (nameList && U_SUCCESS(status)) {
729         LocalPointer<UVector> regionNames(
730             new UVector(uprv_deleteUObject, uhash_compareUnicodeString, nameList->size(), status), status);
731         for ( int32_t i = 0 ; U_SUCCESS(status) && i < nameList->size() ; i++ ) {
732             UnicodeString* this_region_name = (UnicodeString *)nameList->elementAt(i);
733             LocalPointer<UnicodeString> new_region_name(new UnicodeString(*this_region_name), status);
734             regionNames->adoptElement(new_region_name.orphan(), status);
735         }
736         if (U_SUCCESS(status)) {
737             fRegionNames = regionNames.orphan();
738         }
739     }
740 }
741 
742 const UnicodeString*
snext(UErrorCode & status)743 RegionNameEnumeration::snext(UErrorCode& status) {
744   if (U_FAILURE(status) || (fRegionNames==nullptr)) {
745     return nullptr;
746   }
747   const UnicodeString* nextStr = (const UnicodeString *)fRegionNames->elementAt(pos);
748   if (nextStr!=nullptr) {
749     pos++;
750   }
751   return nextStr;
752 }
753 
754 void
reset(UErrorCode &)755 RegionNameEnumeration::reset(UErrorCode& /*status*/) {
756     pos=0;
757 }
758 
759 int32_t
count(UErrorCode &) const760 RegionNameEnumeration::count(UErrorCode& /*status*/) const {
761     return (fRegionNames==nullptr) ? 0 : fRegionNames->size();
762 }
763 
~RegionNameEnumeration()764 RegionNameEnumeration::~RegionNameEnumeration() {
765     delete fRegionNames;
766 }
767 
768 U_NAMESPACE_END
769 
770 #endif /* #if !UCONFIG_NO_FORMATTING */
771 
772 //eof
773