]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/numsys.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / numsys.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
729e4ab9
A
3/*
4*******************************************************************************
2ca993e8 5* Copyright (C) 2010-2015, International Business Machines Corporation and
729e4ab9
A
6* others. All Rights Reserved.
7*******************************************************************************
8*
9*
10* File NUMSYS.CPP
11*
12* Modification History:*
13* Date Name Description
14*
15********************************************************************************
16*/
17
18#include "unicode/utypes.h"
19#include "unicode/localpointer.h"
20#include "unicode/uchar.h"
21#include "unicode/unistr.h"
22#include "unicode/ures.h"
23#include "unicode/ustring.h"
24#include "unicode/uloc.h"
25#include "unicode/schriter.h"
26#include "unicode/numsys.h"
27#include "cstring.h"
0f5d89e8 28#include "uassert.h"
3d1f044b
A
29#include "ucln_in.h"
30#include "umutex.h"
729e4ab9 31#include "uresimp.h"
4388f060 32#include "numsys_impl.h"
729e4ab9
A
33
34#if !UCONFIG_NO_FORMATTING
35
36U_NAMESPACE_BEGIN
37
38// Useful constants
39
40#define DEFAULT_DIGITS UNICODE_STRING_SIMPLE("0123456789");
41static const char gNumberingSystems[] = "numberingSystems";
42static const char gNumberElements[] = "NumberElements";
43static const char gDefault[] = "default";
4388f060
A
44static const char gNative[] = "native";
45static const char gTraditional[] = "traditional";
46static const char gFinance[] = "finance";
729e4ab9
A
47static const char gDesc[] = "desc";
48static const char gRadix[] = "radix";
49static const char gAlgorithmic[] = "algorithmic";
50static const char gLatn[] = "latn";
51
52
53UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumberingSystem)
4388f060 54UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumsysNameEnumeration)
729e4ab9
A
55
56 /**
57 * Default Constructor.
58 *
59 * @draft ICU 4.2
60 */
61
62NumberingSystem::NumberingSystem() {
63 radix = 10;
64 algorithmic = FALSE;
65 UnicodeString defaultDigits = DEFAULT_DIGITS;
66 desc.setTo(defaultDigits);
67 uprv_strcpy(name,gLatn);
68}
69
70 /**
71 * Copy constructor.
72 * @draft ICU 4.2
73 */
74
75NumberingSystem::NumberingSystem(const NumberingSystem& other)
76: UObject(other) {
77 *this=other;
78}
79
80NumberingSystem* U_EXPORT2
81NumberingSystem::createInstance(int32_t radix_in, UBool isAlgorithmic_in, const UnicodeString & desc_in, UErrorCode &status) {
82
83 if (U_FAILURE(status)) {
3d1f044b 84 return nullptr;
729e4ab9
A
85 }
86
87 if ( radix_in < 2 ) {
88 status = U_ILLEGAL_ARGUMENT_ERROR;
3d1f044b 89 return nullptr;
729e4ab9
A
90 }
91
92 if ( !isAlgorithmic_in ) {
2ca993e8 93 if ( desc_in.countChar32() != radix_in ) {
729e4ab9 94 status = U_ILLEGAL_ARGUMENT_ERROR;
3d1f044b 95 return nullptr;
729e4ab9
A
96 }
97 }
98
3d1f044b
A
99 LocalPointer<NumberingSystem> ns(new NumberingSystem(), status);
100 if (U_FAILURE(status)) {
101 return nullptr;
102 }
729e4ab9
A
103
104 ns->setRadix(radix_in);
105 ns->setDesc(desc_in);
106 ns->setAlgorithmic(isAlgorithmic_in);
3d1f044b 107 ns->setName(nullptr);
729e4ab9 108
3d1f044b
A
109 return ns.orphan();
110}
729e4ab9
A
111
112NumberingSystem* U_EXPORT2
113NumberingSystem::createInstance(const Locale & inLocale, UErrorCode& status) {
114
115 if (U_FAILURE(status)) {
3d1f044b 116 return nullptr;
4388f060 117 }
729e4ab9 118
4388f060
A
119 UBool nsResolved = TRUE;
120 UBool usingFallback = FALSE;
729e4ab9 121 char buffer[ULOC_KEYWORDS_CAPACITY];
3d1f044b 122 int32_t count = inLocale.getKeywordValue("numbers", buffer, sizeof(buffer), status);
0f5d89e8
A
123 if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
124 // the "numbers" keyword exceeds ULOC_KEYWORDS_CAPACITY; ignore and use default.
125 count = 0;
126 status = U_ZERO_ERROR;
127 }
729e4ab9 128 if ( count > 0 ) { // @numbers keyword was specified in the locale
0f5d89e8 129 U_ASSERT(count < ULOC_KEYWORDS_CAPACITY);
729e4ab9 130 buffer[count] = '\0'; // Make sure it is null terminated.
4388f060
A
131 if ( !uprv_strcmp(buffer,gDefault) || !uprv_strcmp(buffer,gNative) ||
132 !uprv_strcmp(buffer,gTraditional) || !uprv_strcmp(buffer,gFinance)) {
133 nsResolved = FALSE;
134 }
135 } else {
3d1f044b 136 uprv_strcpy(buffer, gDefault);
4388f060
A
137 nsResolved = FALSE;
138 }
139
140 if (!nsResolved) { // Resolve the numbering system ( default, native, traditional or finance ) into a "real" numbering system
141 UErrorCode localStatus = U_ZERO_ERROR;
3d1f044b
A
142 LocalUResourceBundlePointer resource(ures_open(nullptr, inLocale.getName(), &localStatus));
143 LocalUResourceBundlePointer numberElementsRes(ures_getByKey(resource.getAlias(), gNumberElements, nullptr, &localStatus));
144 // Don't stomp on the catastrophic failure of OOM.
145 if (localStatus == U_MEMORY_ALLOCATION_ERROR) {
146 status = U_MEMORY_ALLOCATION_ERROR;
147 return nullptr;
148 }
4388f060
A
149 while (!nsResolved) {
150 localStatus = U_ZERO_ERROR;
151 count = 0;
3d1f044b
A
152 const UChar *nsName = ures_getStringByKeyWithFallback(numberElementsRes.getAlias(), buffer, &count, &localStatus);
153 // Don't stomp on the catastrophic failure of OOM.
154 if (localStatus == U_MEMORY_ALLOCATION_ERROR) {
155 status = U_MEMORY_ALLOCATION_ERROR;
156 return nullptr;
157 }
4388f060 158 if ( count > 0 && count < ULOC_KEYWORDS_CAPACITY ) { // numbering system found
3d1f044b 159 u_UCharsToChars(nsName, buffer, count);
4388f060
A
160 buffer[count] = '\0'; // Make sure it is null terminated.
161 nsResolved = TRUE;
162 }
163
164 if (!nsResolved) { // Fallback behavior per TR35 - traditional falls back to native, finance and native fall back to default
165 if (!uprv_strcmp(buffer,gNative) || !uprv_strcmp(buffer,gFinance)) {
166 uprv_strcpy(buffer,gDefault);
167 } else if (!uprv_strcmp(buffer,gTraditional)) {
168 uprv_strcpy(buffer,gNative);
169 } else { // If we get here we couldn't find even the default numbering system
170 usingFallback = TRUE;
171 nsResolved = TRUE;
172 }
173 }
174 }
4388f060 175 }
729e4ab9 176
4388f060
A
177 if (usingFallback) {
178 status = U_USING_FALLBACK_WARNING;
179 NumberingSystem *ns = new NumberingSystem();
3d1f044b
A
180 if (ns == nullptr) {
181 status = U_MEMORY_ALLOCATION_ERROR;
182 }
4388f060
A
183 return ns;
184 } else {
3d1f044b 185 return NumberingSystem::createInstanceByName(buffer, status);
729e4ab9 186 }
4388f060 187 }
729e4ab9
A
188
189NumberingSystem* U_EXPORT2
190NumberingSystem::createInstance(UErrorCode& status) {
191 return NumberingSystem::createInstance(Locale::getDefault(), status);
192}
193
194NumberingSystem* U_EXPORT2
195NumberingSystem::createInstanceByName(const char *name, UErrorCode& status) {
4388f060
A
196 int32_t radix = 10;
197 int32_t algorithmic = 0;
729e4ab9 198
3d1f044b
A
199 LocalUResourceBundlePointer numberingSystemsInfo(ures_openDirect(nullptr, gNumberingSystems, &status));
200 LocalUResourceBundlePointer nsCurrent(ures_getByKey(numberingSystemsInfo.getAlias(), gNumberingSystems, nullptr, &status));
201 LocalUResourceBundlePointer nsTop(ures_getByKey(nsCurrent.getAlias(), name, nullptr, &status));
729e4ab9 202
3d1f044b 203 UnicodeString nsd = ures_getUnicodeStringByKey(nsTop.getAlias(), gDesc, &status);
729e4ab9 204
3d1f044b
A
205 ures_getByKey(nsTop.getAlias(), gRadix, nsCurrent.getAlias(), &status);
206 radix = ures_getInt(nsCurrent.getAlias(), &status);
729e4ab9 207
3d1f044b
A
208 ures_getByKey(nsTop.getAlias(), gAlgorithmic, nsCurrent.getAlias(), &status);
209 algorithmic = ures_getInt(nsCurrent.getAlias(), &status);
729e4ab9 210
3d1f044b 211 UBool isAlgorithmic = ( algorithmic == 1 );
729e4ab9 212
4388f060 213 if (U_FAILURE(status)) {
3d1f044b
A
214 // Don't stomp on the catastrophic failure of OOM.
215 if (status != U_MEMORY_ALLOCATION_ERROR) {
216 status = U_UNSUPPORTED_ERROR;
217 }
218 return nullptr;
4388f060 219 }
729e4ab9 220
3d1f044b
A
221 LocalPointer<NumberingSystem> ns(NumberingSystem::createInstance(radix, isAlgorithmic, nsd, status), status);
222 if (U_FAILURE(status)) {
223 return nullptr;
224 }
4388f060 225 ns->setName(name);
3d1f044b 226 return ns.orphan();
729e4ab9
A
227}
228
229 /**
230 * Destructor.
231 * @draft ICU 4.2
232 */
233NumberingSystem::~NumberingSystem() {
234}
235
57a6839d 236int32_t NumberingSystem::getRadix() const {
729e4ab9
A
237 return radix;
238}
239
57a6839d 240UnicodeString NumberingSystem::getDescription() const {
729e4ab9
A
241 return desc;
242}
243
57a6839d 244const char * NumberingSystem::getName() const {
729e4ab9
A
245 return name;
246}
247
248void NumberingSystem::setRadix(int32_t r) {
249 radix = r;
250}
251
252void NumberingSystem::setAlgorithmic(UBool c) {
253 algorithmic = c;
254}
255
f3c0d7a5 256void NumberingSystem::setDesc(const UnicodeString &d) {
729e4ab9
A
257 desc.setTo(d);
258}
259void NumberingSystem::setName(const char *n) {
3d1f044b 260 if ( n == nullptr ) {
729e4ab9
A
261 name[0] = (char) 0;
262 } else {
3d1f044b
A
263 uprv_strncpy(name,n,kInternalNumSysNameCapacity);
264 name[kInternalNumSysNameCapacity] = '\0'; // Make sure it is null terminated.
729e4ab9
A
265 }
266}
267UBool NumberingSystem::isAlgorithmic() const {
268 return ( algorithmic );
269}
270
3d1f044b 271namespace {
4388f060 272
3d1f044b
A
273UVector* gNumsysNames = nullptr;
274UInitOnce gNumSysInitOnce = U_INITONCE_INITIALIZER;
275
276U_CFUNC UBool U_CALLCONV numSysCleanup() {
277 delete gNumsysNames;
278 gNumsysNames = nullptr;
279 gNumSysInitOnce.reset();
280 return true;
281}
282
283U_CFUNC void initNumsysNames(UErrorCode &status) {
284 U_ASSERT(gNumsysNames == nullptr);
285 ucln_i18n_registerCleanup(UCLN_I18N_NUMSYS, numSysCleanup);
286
287 // TODO: Simple array of UnicodeString objects, based on length of table resource?
288 LocalPointer<UVector> numsysNames(new UVector(uprv_deleteUObject, nullptr, status), status);
4388f060 289 if (U_FAILURE(status)) {
3d1f044b 290 return;
4388f060
A
291 }
292
3d1f044b
A
293 UErrorCode rbstatus = U_ZERO_ERROR;
294 UResourceBundle *numberingSystemsInfo = ures_openDirect(nullptr, "numberingSystems", &rbstatus);
295 numberingSystemsInfo =
296 ures_getByKey(numberingSystemsInfo, "numberingSystems", numberingSystemsInfo, &rbstatus);
297 if (U_FAILURE(rbstatus)) {
298 // Don't stomp on the catastrophic failure of OOM.
299 if (rbstatus == U_MEMORY_ALLOCATION_ERROR) {
300 status = rbstatus;
301 } else {
4388f060 302 status = U_MISSING_RESOURCE_ERROR;
4388f060 303 }
4388f060 304 ures_close(numberingSystemsInfo);
3d1f044b
A
305 return;
306 }
307
308 while ( ures_hasNext(numberingSystemsInfo) && U_SUCCESS(status) ) {
309 LocalUResourceBundlePointer nsCurrent(ures_getNextResource(numberingSystemsInfo, nullptr, &rbstatus));
310 if (rbstatus == U_MEMORY_ALLOCATION_ERROR) {
311 status = rbstatus; // we want to report OOM failure back to the caller.
312 break;
2ca993e8 313 }
3d1f044b
A
314 const char *nsName = ures_getKey(nsCurrent.getAlias());
315 LocalPointer<UnicodeString> newElem(new UnicodeString(nsName, -1, US_INV), status);
316 if (U_SUCCESS(status)) {
317 numsysNames->addElement(newElem.getAlias(), status);
318 if (U_SUCCESS(status)) {
319 newElem.orphan(); // on success, the numsysNames vector owns newElem.
320 }
2ca993e8 321 }
4388f060
A
322 }
323
3d1f044b
A
324 ures_close(numberingSystemsInfo);
325 if (U_SUCCESS(status)) {
326 gNumsysNames = numsysNames.orphan();
327 }
328 return;
4388f060 329}
729e4ab9 330
3d1f044b
A
331} // end anonymous namespace
332
333StringEnumeration* NumberingSystem::getAvailableNames(UErrorCode &status) {
334 umtx_initOnce(gNumSysInitOnce, &initNumsysNames, status);
335 LocalPointer<StringEnumeration> result(new NumsysNameEnumeration(status), status);
336 return result.orphan();
337}
338
339NumsysNameEnumeration::NumsysNameEnumeration(UErrorCode& status) : pos(0) {
340 (void)status;
4388f060
A
341}
342
343const UnicodeString*
344NumsysNameEnumeration::snext(UErrorCode& status) {
3d1f044b
A
345 if (U_SUCCESS(status) && (gNumsysNames != nullptr) && (pos < gNumsysNames->size())) {
346 return (const UnicodeString*)gNumsysNames->elementAt(pos++);
4388f060 347 }
3d1f044b 348 return nullptr;
4388f060
A
349}
350
351void
352NumsysNameEnumeration::reset(UErrorCode& /*status*/) {
353 pos=0;
354}
355
356int32_t
357NumsysNameEnumeration::count(UErrorCode& /*status*/) const {
3d1f044b 358 return (gNumsysNames==nullptr) ? 0 : gNumsysNames->size();
4388f060
A
359}
360
361NumsysNameEnumeration::~NumsysNameEnumeration() {
4388f060 362}
729e4ab9
A
363U_NAMESPACE_END
364
365#endif /* #if !UCONFIG_NO_FORMATTING */
366
367//eof