OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * Copyright (C) 2010-2013, International Business Machines Corporation and | 3 * Copyright (C) 2010-2015, International Business Machines Corporation and |
4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
5 ******************************************************************************* | 5 ******************************************************************************* |
6 * | 6 * |
7 * | 7 * |
8 * File NUMSYS.CPP | 8 * File NUMSYS.CPP |
9 * | 9 * |
10 * Modification History:* | 10 * Modification History:* |
11 * Date Name Description | 11 * Date Name Description |
12 * | 12 * |
13 ******************************************************************************** | 13 ******************************************************************************** |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 if (U_FAILURE(status)) { | 78 if (U_FAILURE(status)) { |
79 return NULL; | 79 return NULL; |
80 } | 80 } |
81 | 81 |
82 if ( radix_in < 2 ) { | 82 if ( radix_in < 2 ) { |
83 status = U_ILLEGAL_ARGUMENT_ERROR; | 83 status = U_ILLEGAL_ARGUMENT_ERROR; |
84 return NULL; | 84 return NULL; |
85 } | 85 } |
86 | 86 |
87 if ( !isAlgorithmic_in ) { | 87 if ( !isAlgorithmic_in ) { |
88 if ( desc_in.countChar32() != radix_in || !isValidDigitString(desc_in)) { | 88 if ( desc_in.countChar32() != radix_in ) { |
89 status = U_ILLEGAL_ARGUMENT_ERROR; | 89 status = U_ILLEGAL_ARGUMENT_ERROR; |
90 return NULL; | 90 return NULL; |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 NumberingSystem *ns = new NumberingSystem(); | 94 NumberingSystem *ns = new NumberingSystem(); |
95 | 95 |
96 ns->setRadix(radix_in); | 96 ns->setRadix(radix_in); |
97 ns->setDesc(desc_in); | 97 ns->setDesc(desc_in); |
98 ns->setAlgorithmic(isAlgorithmic_in); | 98 ns->setAlgorithmic(isAlgorithmic_in); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 } else { | 237 } else { |
238 uprv_strncpy(name,n,NUMSYS_NAME_CAPACITY); | 238 uprv_strncpy(name,n,NUMSYS_NAME_CAPACITY); |
239 name[NUMSYS_NAME_CAPACITY] = (char)0; // Make sure it is null terminated
. | 239 name[NUMSYS_NAME_CAPACITY] = (char)0; // Make sure it is null terminated
. |
240 } | 240 } |
241 } | 241 } |
242 UBool NumberingSystem::isAlgorithmic() const { | 242 UBool NumberingSystem::isAlgorithmic() const { |
243 return ( algorithmic ); | 243 return ( algorithmic ); |
244 } | 244 } |
245 | 245 |
246 StringEnumeration* NumberingSystem::getAvailableNames(UErrorCode &status) { | 246 StringEnumeration* NumberingSystem::getAvailableNames(UErrorCode &status) { |
247 | 247 // TODO(ticket #11908): Init-once static cache, with u_cleanup() callback. |
248 static StringEnumeration* availableNames = NULL; | 248 static StringEnumeration* availableNames = NULL; |
249 | 249 |
250 if (U_FAILURE(status)) { | 250 if (U_FAILURE(status)) { |
251 return NULL; | 251 return NULL; |
252 } | 252 } |
253 | 253 |
254 if ( availableNames == NULL ) { | 254 if ( availableNames == NULL ) { |
255 UVector *fNumsysNames = new UVector(uprv_deleteUObject, NULL, status); | 255 // TODO: Simple array of UnicodeString objects, based on length of table
resource? |
| 256 LocalPointer<UVector> numsysNames(new UVector(uprv_deleteUObject, NULL,
status), status); |
256 if (U_FAILURE(status)) { | 257 if (U_FAILURE(status)) { |
257 status = U_MEMORY_ALLOCATION_ERROR; | |
258 return NULL; | 258 return NULL; |
259 } | 259 } |
260 | 260 |
261 UErrorCode rbstatus = U_ZERO_ERROR; | 261 UErrorCode rbstatus = U_ZERO_ERROR; |
262 UResourceBundle *numberingSystemsInfo = ures_openDirect(NULL, "numbering
Systems", &rbstatus); | 262 UResourceBundle *numberingSystemsInfo = ures_openDirect(NULL, "numbering
Systems", &rbstatus); |
263 numberingSystemsInfo = ures_getByKey(numberingSystemsInfo,"numberingSyst
ems",numberingSystemsInfo,&rbstatus); | 263 numberingSystemsInfo = ures_getByKey(numberingSystemsInfo,"numberingSyst
ems",numberingSystemsInfo,&rbstatus); |
264 if(U_FAILURE(rbstatus)) { | 264 if(U_FAILURE(rbstatus)) { |
265 status = U_MISSING_RESOURCE_ERROR; | 265 status = U_MISSING_RESOURCE_ERROR; |
266 ures_close(numberingSystemsInfo); | 266 ures_close(numberingSystemsInfo); |
267 return NULL; | 267 return NULL; |
268 } | 268 } |
269 | 269 |
270 while ( ures_hasNext(numberingSystemsInfo) ) { | 270 while ( ures_hasNext(numberingSystemsInfo) ) { |
271 UResourceBundle *nsCurrent = ures_getNextResource(numberingSystemsIn
fo,NULL,&rbstatus); | 271 UResourceBundle *nsCurrent = ures_getNextResource(numberingSystemsIn
fo,NULL,&rbstatus); |
272 const char *nsName = ures_getKey(nsCurrent); | 272 const char *nsName = ures_getKey(nsCurrent); |
273 fNumsysNames->addElement(new UnicodeString(nsName, -1, US_INV),statu
s); | 273 numsysNames->addElement(new UnicodeString(nsName, -1, US_INV),status
); |
274 ures_close(nsCurrent); | 274 ures_close(nsCurrent); |
275 } | 275 } |
276 | 276 |
277 ures_close(numberingSystemsInfo); | 277 ures_close(numberingSystemsInfo); |
278 availableNames = new NumsysNameEnumeration(fNumsysNames,status); | 278 if (U_FAILURE(status)) { |
279 | 279 return NULL; |
| 280 } |
| 281 availableNames = new NumsysNameEnumeration(numsysNames.getAlias(), statu
s); |
| 282 if (availableNames == NULL) { |
| 283 status = U_MEMORY_ALLOCATION_ERROR; |
| 284 return NULL; |
| 285 } |
| 286 numsysNames.orphan(); // The names got adopted. |
280 } | 287 } |
281 | 288 |
282 return availableNames; | 289 return availableNames; |
283 } | 290 } |
284 | 291 |
285 UBool NumberingSystem::isValidDigitString(const UnicodeString& str) { | 292 NumsysNameEnumeration::NumsysNameEnumeration(UVector *numsysNames, UErrorCode& /
*status*/) { |
286 | |
287 StringCharacterIterator it(str); | |
288 UChar32 c; | |
289 int32_t i = 0; | |
290 | |
291 for ( it.setToStart(); it.hasNext(); ) { | |
292 c = it.next32PostInc(); | |
293 if ( c > 0xFFFF ) { // Digits outside the BMP are not currently supported | |
294 return FALSE; | |
295 } | |
296 i++; | |
297 } | |
298 return TRUE; | |
299 } | |
300 | |
301 NumsysNameEnumeration::NumsysNameEnumeration(UVector *fNameList, UErrorCode& /*s
tatus*/) { | |
302 pos=0; | 293 pos=0; |
303 fNumsysNames = fNameList; | 294 fNumsysNames = numsysNames; |
304 } | 295 } |
305 | 296 |
306 const UnicodeString* | 297 const UnicodeString* |
307 NumsysNameEnumeration::snext(UErrorCode& status) { | 298 NumsysNameEnumeration::snext(UErrorCode& status) { |
308 if (U_SUCCESS(status) && pos < fNumsysNames->size()) { | 299 if (U_SUCCESS(status) && pos < fNumsysNames->size()) { |
309 return (const UnicodeString*)fNumsysNames->elementAt(pos++); | 300 return (const UnicodeString*)fNumsysNames->elementAt(pos++); |
310 } | 301 } |
311 return NULL; | 302 return NULL; |
312 } | 303 } |
313 | 304 |
314 void | 305 void |
315 NumsysNameEnumeration::reset(UErrorCode& /*status*/) { | 306 NumsysNameEnumeration::reset(UErrorCode& /*status*/) { |
316 pos=0; | 307 pos=0; |
317 } | 308 } |
318 | 309 |
319 int32_t | 310 int32_t |
320 NumsysNameEnumeration::count(UErrorCode& /*status*/) const { | 311 NumsysNameEnumeration::count(UErrorCode& /*status*/) const { |
321 return (fNumsysNames==NULL) ? 0 : fNumsysNames->size(); | 312 return (fNumsysNames==NULL) ? 0 : fNumsysNames->size(); |
322 } | 313 } |
323 | 314 |
324 NumsysNameEnumeration::~NumsysNameEnumeration() { | 315 NumsysNameEnumeration::~NumsysNameEnumeration() { |
325 delete fNumsysNames; | 316 delete fNumsysNames; |
326 } | 317 } |
327 U_NAMESPACE_END | 318 U_NAMESPACE_END |
328 | 319 |
329 #endif /* #if !UCONFIG_NO_FORMATTING */ | 320 #endif /* #if !UCONFIG_NO_FORMATTING */ |
330 | 321 |
331 //eof | 322 //eof |
OLD | NEW |