1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/rbnf.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1624 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* Copyright (C) 1997-2013, International Business Machines Corporation 1.7 +* and others. All Rights Reserved. 1.8 +******************************************************************************* 1.9 +*/ 1.10 + 1.11 +#include "utypeinfo.h" // for 'typeid' to work 1.12 + 1.13 +#include "unicode/rbnf.h" 1.14 + 1.15 +#if U_HAVE_RBNF 1.16 + 1.17 +#include "unicode/normlzr.h" 1.18 +#include "unicode/tblcoll.h" 1.19 +#include "unicode/uchar.h" 1.20 +#include "unicode/ucol.h" 1.21 +#include "unicode/uloc.h" 1.22 +#include "unicode/unum.h" 1.23 +#include "unicode/ures.h" 1.24 +#include "unicode/ustring.h" 1.25 +#include "unicode/utf16.h" 1.26 +#include "unicode/udata.h" 1.27 +#include "nfrs.h" 1.28 + 1.29 +#include "cmemory.h" 1.30 +#include "cstring.h" 1.31 +#include "patternprops.h" 1.32 +#include "uresimp.h" 1.33 + 1.34 +// debugging 1.35 +// #define DEBUG 1.36 + 1.37 +#ifdef DEBUG 1.38 +#include "stdio.h" 1.39 +#endif 1.40 + 1.41 +#define U_ICUDATA_RBNF U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "rbnf" 1.42 + 1.43 +static const UChar gPercentPercent[] = 1.44 +{ 1.45 + 0x25, 0x25, 0 1.46 +}; /* "%%" */ 1.47 + 1.48 +// All urbnf objects are created through openRules, so we init all of the 1.49 +// Unicode string constants required by rbnf, nfrs, or nfr here. 1.50 +static const UChar gLenientParse[] = 1.51 +{ 1.52 + 0x25, 0x25, 0x6C, 0x65, 0x6E, 0x69, 0x65, 0x6E, 0x74, 0x2D, 0x70, 0x61, 0x72, 0x73, 0x65, 0x3A, 0 1.53 +}; /* "%%lenient-parse:" */ 1.54 +static const UChar gSemiColon = 0x003B; 1.55 +static const UChar gSemiPercent[] = 1.56 +{ 1.57 + 0x3B, 0x25, 0 1.58 +}; /* ";%" */ 1.59 + 1.60 +#define kSomeNumberOfBitsDiv2 22 1.61 +#define kHalfMaxDouble (double)(1 << kSomeNumberOfBitsDiv2) 1.62 +#define kMaxDouble (kHalfMaxDouble * kHalfMaxDouble) 1.63 + 1.64 +U_NAMESPACE_BEGIN 1.65 + 1.66 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedNumberFormat) 1.67 + 1.68 +/* 1.69 +This is a utility class. It does not use ICU's RTTI. 1.70 +If ICU's RTTI is needed again, you can uncomment the RTTI code and derive from UObject. 1.71 +Please make sure that intltest passes on Windows in Release mode, 1.72 +since the string pooling per compilation unit will mess up how RTTI works. 1.73 +The RTTI code was also removed due to lack of code coverage. 1.74 +*/ 1.75 +class LocalizationInfo : public UMemory { 1.76 +protected: 1.77 + virtual ~LocalizationInfo(); 1.78 + uint32_t refcount; 1.79 + 1.80 +public: 1.81 + LocalizationInfo() : refcount(0) {} 1.82 + 1.83 + LocalizationInfo* ref(void) { 1.84 + ++refcount; 1.85 + return this; 1.86 + } 1.87 + 1.88 + LocalizationInfo* unref(void) { 1.89 + if (refcount && --refcount == 0) { 1.90 + delete this; 1.91 + } 1.92 + return NULL; 1.93 + } 1.94 + 1.95 + virtual UBool operator==(const LocalizationInfo* rhs) const; 1.96 + inline UBool operator!=(const LocalizationInfo* rhs) const { return !operator==(rhs); } 1.97 + 1.98 + virtual int32_t getNumberOfRuleSets(void) const = 0; 1.99 + virtual const UChar* getRuleSetName(int32_t index) const = 0; 1.100 + virtual int32_t getNumberOfDisplayLocales(void) const = 0; 1.101 + virtual const UChar* getLocaleName(int32_t index) const = 0; 1.102 + virtual const UChar* getDisplayName(int32_t localeIndex, int32_t ruleIndex) const = 0; 1.103 + 1.104 + virtual int32_t indexForLocale(const UChar* locale) const; 1.105 + virtual int32_t indexForRuleSet(const UChar* ruleset) const; 1.106 + 1.107 +// virtual UClassID getDynamicClassID() const = 0; 1.108 +// static UClassID getStaticClassID(void); 1.109 +}; 1.110 + 1.111 +LocalizationInfo::~LocalizationInfo() {} 1.112 + 1.113 +//UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(LocalizationInfo) 1.114 + 1.115 +// if both strings are NULL, this returns TRUE 1.116 +static UBool 1.117 +streq(const UChar* lhs, const UChar* rhs) { 1.118 + if (rhs == lhs) { 1.119 + return TRUE; 1.120 + } 1.121 + if (lhs && rhs) { 1.122 + return u_strcmp(lhs, rhs) == 0; 1.123 + } 1.124 + return FALSE; 1.125 +} 1.126 + 1.127 +UBool 1.128 +LocalizationInfo::operator==(const LocalizationInfo* rhs) const { 1.129 + if (rhs) { 1.130 + if (this == rhs) { 1.131 + return TRUE; 1.132 + } 1.133 + 1.134 + int32_t rsc = getNumberOfRuleSets(); 1.135 + if (rsc == rhs->getNumberOfRuleSets()) { 1.136 + for (int i = 0; i < rsc; ++i) { 1.137 + if (!streq(getRuleSetName(i), rhs->getRuleSetName(i))) { 1.138 + return FALSE; 1.139 + } 1.140 + } 1.141 + int32_t dlc = getNumberOfDisplayLocales(); 1.142 + if (dlc == rhs->getNumberOfDisplayLocales()) { 1.143 + for (int i = 0; i < dlc; ++i) { 1.144 + const UChar* locale = getLocaleName(i); 1.145 + int32_t ix = rhs->indexForLocale(locale); 1.146 + // if no locale, ix is -1, getLocaleName returns null, so streq returns false 1.147 + if (!streq(locale, rhs->getLocaleName(ix))) { 1.148 + return FALSE; 1.149 + } 1.150 + for (int j = 0; j < rsc; ++j) { 1.151 + if (!streq(getDisplayName(i, j), rhs->getDisplayName(ix, j))) { 1.152 + return FALSE; 1.153 + } 1.154 + } 1.155 + } 1.156 + return TRUE; 1.157 + } 1.158 + } 1.159 + } 1.160 + return FALSE; 1.161 +} 1.162 + 1.163 +int32_t 1.164 +LocalizationInfo::indexForLocale(const UChar* locale) const { 1.165 + for (int i = 0; i < getNumberOfDisplayLocales(); ++i) { 1.166 + if (streq(locale, getLocaleName(i))) { 1.167 + return i; 1.168 + } 1.169 + } 1.170 + return -1; 1.171 +} 1.172 + 1.173 +int32_t 1.174 +LocalizationInfo::indexForRuleSet(const UChar* ruleset) const { 1.175 + if (ruleset) { 1.176 + for (int i = 0; i < getNumberOfRuleSets(); ++i) { 1.177 + if (streq(ruleset, getRuleSetName(i))) { 1.178 + return i; 1.179 + } 1.180 + } 1.181 + } 1.182 + return -1; 1.183 +} 1.184 + 1.185 + 1.186 +typedef void (*Fn_Deleter)(void*); 1.187 + 1.188 +class VArray { 1.189 + void** buf; 1.190 + int32_t cap; 1.191 + int32_t size; 1.192 + Fn_Deleter deleter; 1.193 +public: 1.194 + VArray() : buf(NULL), cap(0), size(0), deleter(NULL) {} 1.195 + 1.196 + VArray(Fn_Deleter del) : buf(NULL), cap(0), size(0), deleter(del) {} 1.197 + 1.198 + ~VArray() { 1.199 + if (deleter) { 1.200 + for (int i = 0; i < size; ++i) { 1.201 + (*deleter)(buf[i]); 1.202 + } 1.203 + } 1.204 + uprv_free(buf); 1.205 + } 1.206 + 1.207 + int32_t length() { 1.208 + return size; 1.209 + } 1.210 + 1.211 + void add(void* elem, UErrorCode& status) { 1.212 + if (U_SUCCESS(status)) { 1.213 + if (size == cap) { 1.214 + if (cap == 0) { 1.215 + cap = 1; 1.216 + } else if (cap < 256) { 1.217 + cap *= 2; 1.218 + } else { 1.219 + cap += 256; 1.220 + } 1.221 + if (buf == NULL) { 1.222 + buf = (void**)uprv_malloc(cap * sizeof(void*)); 1.223 + } else { 1.224 + buf = (void**)uprv_realloc(buf, cap * sizeof(void*)); 1.225 + } 1.226 + if (buf == NULL) { 1.227 + // if we couldn't realloc, we leak the memory we've already allocated, but we're in deep trouble anyway 1.228 + status = U_MEMORY_ALLOCATION_ERROR; 1.229 + return; 1.230 + } 1.231 + void* start = &buf[size]; 1.232 + size_t count = (cap - size) * sizeof(void*); 1.233 + uprv_memset(start, 0, count); // fill with nulls, just because 1.234 + } 1.235 + buf[size++] = elem; 1.236 + } 1.237 + } 1.238 + 1.239 + void** release(void) { 1.240 + void** result = buf; 1.241 + buf = NULL; 1.242 + cap = 0; 1.243 + size = 0; 1.244 + return result; 1.245 + } 1.246 +}; 1.247 + 1.248 +class LocDataParser; 1.249 + 1.250 +class StringLocalizationInfo : public LocalizationInfo { 1.251 + UChar* info; 1.252 + UChar*** data; 1.253 + int32_t numRuleSets; 1.254 + int32_t numLocales; 1.255 + 1.256 +friend class LocDataParser; 1.257 + 1.258 + StringLocalizationInfo(UChar* i, UChar*** d, int32_t numRS, int32_t numLocs) 1.259 + : info(i), data(d), numRuleSets(numRS), numLocales(numLocs) 1.260 + { 1.261 + } 1.262 + 1.263 +public: 1.264 + static StringLocalizationInfo* create(const UnicodeString& info, UParseError& perror, UErrorCode& status); 1.265 + 1.266 + virtual ~StringLocalizationInfo(); 1.267 + virtual int32_t getNumberOfRuleSets(void) const { return numRuleSets; } 1.268 + virtual const UChar* getRuleSetName(int32_t index) const; 1.269 + virtual int32_t getNumberOfDisplayLocales(void) const { return numLocales; } 1.270 + virtual const UChar* getLocaleName(int32_t index) const; 1.271 + virtual const UChar* getDisplayName(int32_t localeIndex, int32_t ruleIndex) const; 1.272 + 1.273 +// virtual UClassID getDynamicClassID() const; 1.274 +// static UClassID getStaticClassID(void); 1.275 + 1.276 +private: 1.277 + void init(UErrorCode& status) const; 1.278 +}; 1.279 + 1.280 + 1.281 +enum { 1.282 + OPEN_ANGLE = 0x003c, /* '<' */ 1.283 + CLOSE_ANGLE = 0x003e, /* '>' */ 1.284 + COMMA = 0x002c, 1.285 + TICK = 0x0027, 1.286 + QUOTE = 0x0022, 1.287 + SPACE = 0x0020 1.288 +}; 1.289 + 1.290 +/** 1.291 + * Utility for parsing a localization string and returning a StringLocalizationInfo*. 1.292 + */ 1.293 +class LocDataParser { 1.294 + UChar* data; 1.295 + const UChar* e; 1.296 + UChar* p; 1.297 + UChar ch; 1.298 + UParseError& pe; 1.299 + UErrorCode& ec; 1.300 + 1.301 +public: 1.302 + LocDataParser(UParseError& parseError, UErrorCode& status) 1.303 + : data(NULL), e(NULL), p(NULL), ch(0xffff), pe(parseError), ec(status) {} 1.304 + ~LocDataParser() {} 1.305 + 1.306 + /* 1.307 + * On a successful parse, return a StringLocalizationInfo*, otherwise delete locData, set perror and status, 1.308 + * and return NULL. The StringLocalizationInfo will adopt locData if it is created. 1.309 + */ 1.310 + StringLocalizationInfo* parse(UChar* data, int32_t len); 1.311 + 1.312 +private: 1.313 + 1.314 + void inc(void) { ++p; ch = 0xffff; } 1.315 + UBool checkInc(UChar c) { if (p < e && (ch == c || *p == c)) { inc(); return TRUE; } return FALSE; } 1.316 + UBool check(UChar c) { return p < e && (ch == c || *p == c); } 1.317 + void skipWhitespace(void) { while (p < e && PatternProps::isWhiteSpace(ch != 0xffff ? ch : *p)) inc();} 1.318 + UBool inList(UChar c, const UChar* list) const { 1.319 + if (*list == SPACE && PatternProps::isWhiteSpace(c)) return TRUE; 1.320 + while (*list && *list != c) ++list; return *list == c; 1.321 + } 1.322 + void parseError(const char* msg); 1.323 + 1.324 + StringLocalizationInfo* doParse(void); 1.325 + 1.326 + UChar** nextArray(int32_t& requiredLength); 1.327 + UChar* nextString(void); 1.328 +}; 1.329 + 1.330 +#ifdef DEBUG 1.331 +#define ERROR(msg) parseError(msg); return NULL; 1.332 +#else 1.333 +#define ERROR(msg) parseError(NULL); return NULL; 1.334 +#endif 1.335 + 1.336 + 1.337 +static const UChar DQUOTE_STOPLIST[] = { 1.338 + QUOTE, 0 1.339 +}; 1.340 + 1.341 +static const UChar SQUOTE_STOPLIST[] = { 1.342 + TICK, 0 1.343 +}; 1.344 + 1.345 +static const UChar NOQUOTE_STOPLIST[] = { 1.346 + SPACE, COMMA, CLOSE_ANGLE, OPEN_ANGLE, TICK, QUOTE, 0 1.347 +}; 1.348 + 1.349 +static void 1.350 +DeleteFn(void* p) { 1.351 + uprv_free(p); 1.352 +} 1.353 + 1.354 +StringLocalizationInfo* 1.355 +LocDataParser::parse(UChar* _data, int32_t len) { 1.356 + if (U_FAILURE(ec)) { 1.357 + if (_data) uprv_free(_data); 1.358 + return NULL; 1.359 + } 1.360 + 1.361 + pe.line = 0; 1.362 + pe.offset = -1; 1.363 + pe.postContext[0] = 0; 1.364 + pe.preContext[0] = 0; 1.365 + 1.366 + if (_data == NULL) { 1.367 + ec = U_ILLEGAL_ARGUMENT_ERROR; 1.368 + return NULL; 1.369 + } 1.370 + 1.371 + if (len <= 0) { 1.372 + ec = U_ILLEGAL_ARGUMENT_ERROR; 1.373 + uprv_free(_data); 1.374 + return NULL; 1.375 + } 1.376 + 1.377 + data = _data; 1.378 + e = data + len; 1.379 + p = _data; 1.380 + ch = 0xffff; 1.381 + 1.382 + return doParse(); 1.383 +} 1.384 + 1.385 + 1.386 +StringLocalizationInfo* 1.387 +LocDataParser::doParse(void) { 1.388 + skipWhitespace(); 1.389 + if (!checkInc(OPEN_ANGLE)) { 1.390 + ERROR("Missing open angle"); 1.391 + } else { 1.392 + VArray array(DeleteFn); 1.393 + UBool mightHaveNext = TRUE; 1.394 + int32_t requiredLength = -1; 1.395 + while (mightHaveNext) { 1.396 + mightHaveNext = FALSE; 1.397 + UChar** elem = nextArray(requiredLength); 1.398 + skipWhitespace(); 1.399 + UBool haveComma = check(COMMA); 1.400 + if (elem) { 1.401 + array.add(elem, ec); 1.402 + if (haveComma) { 1.403 + inc(); 1.404 + mightHaveNext = TRUE; 1.405 + } 1.406 + } else if (haveComma) { 1.407 + ERROR("Unexpected character"); 1.408 + } 1.409 + } 1.410 + 1.411 + skipWhitespace(); 1.412 + if (!checkInc(CLOSE_ANGLE)) { 1.413 + if (check(OPEN_ANGLE)) { 1.414 + ERROR("Missing comma in outer array"); 1.415 + } else { 1.416 + ERROR("Missing close angle bracket in outer array"); 1.417 + } 1.418 + } 1.419 + 1.420 + skipWhitespace(); 1.421 + if (p != e) { 1.422 + ERROR("Extra text after close of localization data"); 1.423 + } 1.424 + 1.425 + array.add(NULL, ec); 1.426 + if (U_SUCCESS(ec)) { 1.427 + int32_t numLocs = array.length() - 2; // subtract first, NULL 1.428 + UChar*** result = (UChar***)array.release(); 1.429 + 1.430 + return new StringLocalizationInfo(data, result, requiredLength-2, numLocs); // subtract first, NULL 1.431 + } 1.432 + } 1.433 + 1.434 + ERROR("Unknown error"); 1.435 +} 1.436 + 1.437 +UChar** 1.438 +LocDataParser::nextArray(int32_t& requiredLength) { 1.439 + if (U_FAILURE(ec)) { 1.440 + return NULL; 1.441 + } 1.442 + 1.443 + skipWhitespace(); 1.444 + if (!checkInc(OPEN_ANGLE)) { 1.445 + ERROR("Missing open angle"); 1.446 + } 1.447 + 1.448 + VArray array; 1.449 + UBool mightHaveNext = TRUE; 1.450 + while (mightHaveNext) { 1.451 + mightHaveNext = FALSE; 1.452 + UChar* elem = nextString(); 1.453 + skipWhitespace(); 1.454 + UBool haveComma = check(COMMA); 1.455 + if (elem) { 1.456 + array.add(elem, ec); 1.457 + if (haveComma) { 1.458 + inc(); 1.459 + mightHaveNext = TRUE; 1.460 + } 1.461 + } else if (haveComma) { 1.462 + ERROR("Unexpected comma"); 1.463 + } 1.464 + } 1.465 + skipWhitespace(); 1.466 + if (!checkInc(CLOSE_ANGLE)) { 1.467 + if (check(OPEN_ANGLE)) { 1.468 + ERROR("Missing close angle bracket in inner array"); 1.469 + } else { 1.470 + ERROR("Missing comma in inner array"); 1.471 + } 1.472 + } 1.473 + 1.474 + array.add(NULL, ec); 1.475 + if (U_SUCCESS(ec)) { 1.476 + if (requiredLength == -1) { 1.477 + requiredLength = array.length() + 1; 1.478 + } else if (array.length() != requiredLength) { 1.479 + ec = U_ILLEGAL_ARGUMENT_ERROR; 1.480 + ERROR("Array not of required length"); 1.481 + } 1.482 + 1.483 + return (UChar**)array.release(); 1.484 + } 1.485 + ERROR("Unknown Error"); 1.486 +} 1.487 + 1.488 +UChar* 1.489 +LocDataParser::nextString() { 1.490 + UChar* result = NULL; 1.491 + 1.492 + skipWhitespace(); 1.493 + if (p < e) { 1.494 + const UChar* terminators; 1.495 + UChar c = *p; 1.496 + UBool haveQuote = c == QUOTE || c == TICK; 1.497 + if (haveQuote) { 1.498 + inc(); 1.499 + terminators = c == QUOTE ? DQUOTE_STOPLIST : SQUOTE_STOPLIST; 1.500 + } else { 1.501 + terminators = NOQUOTE_STOPLIST; 1.502 + } 1.503 + UChar* start = p; 1.504 + while (p < e && !inList(*p, terminators)) ++p; 1.505 + if (p == e) { 1.506 + ERROR("Unexpected end of data"); 1.507 + } 1.508 + 1.509 + UChar x = *p; 1.510 + if (p > start) { 1.511 + ch = x; 1.512 + *p = 0x0; // terminate by writing to data 1.513 + result = start; // just point into data 1.514 + } 1.515 + if (haveQuote) { 1.516 + if (x != c) { 1.517 + ERROR("Missing matching quote"); 1.518 + } else if (p == start) { 1.519 + ERROR("Empty string"); 1.520 + } 1.521 + inc(); 1.522 + } else if (x == OPEN_ANGLE || x == TICK || x == QUOTE) { 1.523 + ERROR("Unexpected character in string"); 1.524 + } 1.525 + } 1.526 + 1.527 + // ok for there to be no next string 1.528 + return result; 1.529 +} 1.530 + 1.531 +void 1.532 +LocDataParser::parseError(const char* /*str*/) { 1.533 + if (!data) { 1.534 + return; 1.535 + } 1.536 + 1.537 + const UChar* start = p - U_PARSE_CONTEXT_LEN - 1; 1.538 + if (start < data) { 1.539 + start = data; 1.540 + } 1.541 + for (UChar* x = p; --x >= start;) { 1.542 + if (!*x) { 1.543 + start = x+1; 1.544 + break; 1.545 + } 1.546 + } 1.547 + const UChar* limit = p + U_PARSE_CONTEXT_LEN - 1; 1.548 + if (limit > e) { 1.549 + limit = e; 1.550 + } 1.551 + u_strncpy(pe.preContext, start, (int32_t)(p-start)); 1.552 + pe.preContext[p-start] = 0; 1.553 + u_strncpy(pe.postContext, p, (int32_t)(limit-p)); 1.554 + pe.postContext[limit-p] = 0; 1.555 + pe.offset = (int32_t)(p - data); 1.556 + 1.557 +#ifdef DEBUG 1.558 + fprintf(stderr, "%s at or near character %d: ", str, p-data); 1.559 + 1.560 + UnicodeString msg; 1.561 + msg.append(start, p - start); 1.562 + msg.append((UChar)0x002f); /* SOLIDUS/SLASH */ 1.563 + msg.append(p, limit-p); 1.564 + msg.append("'"); 1.565 + 1.566 + char buf[128]; 1.567 + int32_t len = msg.extract(0, msg.length(), buf, 128); 1.568 + if (len >= 128) { 1.569 + buf[127] = 0; 1.570 + } else { 1.571 + buf[len] = 0; 1.572 + } 1.573 + fprintf(stderr, "%s\n", buf); 1.574 + fflush(stderr); 1.575 +#endif 1.576 + 1.577 + uprv_free(data); 1.578 + data = NULL; 1.579 + p = NULL; 1.580 + e = NULL; 1.581 + 1.582 + if (U_SUCCESS(ec)) { 1.583 + ec = U_PARSE_ERROR; 1.584 + } 1.585 +} 1.586 + 1.587 +//UOBJECT_DEFINE_RTTI_IMPLEMENTATION(StringLocalizationInfo) 1.588 + 1.589 +StringLocalizationInfo* 1.590 +StringLocalizationInfo::create(const UnicodeString& info, UParseError& perror, UErrorCode& status) { 1.591 + if (U_FAILURE(status)) { 1.592 + return NULL; 1.593 + } 1.594 + 1.595 + int32_t len = info.length(); 1.596 + if (len == 0) { 1.597 + return NULL; // no error; 1.598 + } 1.599 + 1.600 + UChar* p = (UChar*)uprv_malloc(len * sizeof(UChar)); 1.601 + if (!p) { 1.602 + status = U_MEMORY_ALLOCATION_ERROR; 1.603 + return NULL; 1.604 + } 1.605 + info.extract(p, len, status); 1.606 + if (!U_FAILURE(status)) { 1.607 + status = U_ZERO_ERROR; // clear warning about non-termination 1.608 + } 1.609 + 1.610 + LocDataParser parser(perror, status); 1.611 + return parser.parse(p, len); 1.612 +} 1.613 + 1.614 +StringLocalizationInfo::~StringLocalizationInfo() { 1.615 + for (UChar*** p = (UChar***)data; *p; ++p) { 1.616 + // remaining data is simply pointer into our unicode string data. 1.617 + if (*p) uprv_free(*p); 1.618 + } 1.619 + if (data) uprv_free(data); 1.620 + if (info) uprv_free(info); 1.621 +} 1.622 + 1.623 + 1.624 +const UChar* 1.625 +StringLocalizationInfo::getRuleSetName(int32_t index) const { 1.626 + if (index >= 0 && index < getNumberOfRuleSets()) { 1.627 + return data[0][index]; 1.628 + } 1.629 + return NULL; 1.630 +} 1.631 + 1.632 +const UChar* 1.633 +StringLocalizationInfo::getLocaleName(int32_t index) const { 1.634 + if (index >= 0 && index < getNumberOfDisplayLocales()) { 1.635 + return data[index+1][0]; 1.636 + } 1.637 + return NULL; 1.638 +} 1.639 + 1.640 +const UChar* 1.641 +StringLocalizationInfo::getDisplayName(int32_t localeIndex, int32_t ruleIndex) const { 1.642 + if (localeIndex >= 0 && localeIndex < getNumberOfDisplayLocales() && 1.643 + ruleIndex >= 0 && ruleIndex < getNumberOfRuleSets()) { 1.644 + return data[localeIndex+1][ruleIndex+1]; 1.645 + } 1.646 + return NULL; 1.647 +} 1.648 + 1.649 +// ---------- 1.650 + 1.651 +RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description, 1.652 + const UnicodeString& locs, 1.653 + const Locale& alocale, UParseError& perror, UErrorCode& status) 1.654 + : ruleSets(NULL) 1.655 + , ruleSetDescriptions(NULL) 1.656 + , numRuleSets(0) 1.657 + , defaultRuleSet(NULL) 1.658 + , locale(alocale) 1.659 + , collator(NULL) 1.660 + , decimalFormatSymbols(NULL) 1.661 + , lenient(FALSE) 1.662 + , lenientParseRules(NULL) 1.663 + , localizations(NULL) 1.664 +{ 1.665 + LocalizationInfo* locinfo = StringLocalizationInfo::create(locs, perror, status); 1.666 + init(description, locinfo, perror, status); 1.667 +} 1.668 + 1.669 +RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description, 1.670 + const UnicodeString& locs, 1.671 + UParseError& perror, UErrorCode& status) 1.672 + : ruleSets(NULL) 1.673 + , ruleSetDescriptions(NULL) 1.674 + , numRuleSets(0) 1.675 + , defaultRuleSet(NULL) 1.676 + , locale(Locale::getDefault()) 1.677 + , collator(NULL) 1.678 + , decimalFormatSymbols(NULL) 1.679 + , lenient(FALSE) 1.680 + , lenientParseRules(NULL) 1.681 + , localizations(NULL) 1.682 +{ 1.683 + LocalizationInfo* locinfo = StringLocalizationInfo::create(locs, perror, status); 1.684 + init(description, locinfo, perror, status); 1.685 +} 1.686 + 1.687 +RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description, 1.688 + LocalizationInfo* info, 1.689 + const Locale& alocale, UParseError& perror, UErrorCode& status) 1.690 + : ruleSets(NULL) 1.691 + , ruleSetDescriptions(NULL) 1.692 + , numRuleSets(0) 1.693 + , defaultRuleSet(NULL) 1.694 + , locale(alocale) 1.695 + , collator(NULL) 1.696 + , decimalFormatSymbols(NULL) 1.697 + , lenient(FALSE) 1.698 + , lenientParseRules(NULL) 1.699 + , localizations(NULL) 1.700 +{ 1.701 + init(description, info, perror, status); 1.702 +} 1.703 + 1.704 +RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description, 1.705 + UParseError& perror, 1.706 + UErrorCode& status) 1.707 + : ruleSets(NULL) 1.708 + , ruleSetDescriptions(NULL) 1.709 + , numRuleSets(0) 1.710 + , defaultRuleSet(NULL) 1.711 + , locale(Locale::getDefault()) 1.712 + , collator(NULL) 1.713 + , decimalFormatSymbols(NULL) 1.714 + , lenient(FALSE) 1.715 + , lenientParseRules(NULL) 1.716 + , localizations(NULL) 1.717 +{ 1.718 + init(description, NULL, perror, status); 1.719 +} 1.720 + 1.721 +RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description, 1.722 + const Locale& aLocale, 1.723 + UParseError& perror, 1.724 + UErrorCode& status) 1.725 + : ruleSets(NULL) 1.726 + , ruleSetDescriptions(NULL) 1.727 + , numRuleSets(0) 1.728 + , defaultRuleSet(NULL) 1.729 + , locale(aLocale) 1.730 + , collator(NULL) 1.731 + , decimalFormatSymbols(NULL) 1.732 + , lenient(FALSE) 1.733 + , lenientParseRules(NULL) 1.734 + , localizations(NULL) 1.735 +{ 1.736 + init(description, NULL, perror, status); 1.737 +} 1.738 + 1.739 +RuleBasedNumberFormat::RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& alocale, UErrorCode& status) 1.740 + : ruleSets(NULL) 1.741 + , ruleSetDescriptions(NULL) 1.742 + , numRuleSets(0) 1.743 + , defaultRuleSet(NULL) 1.744 + , locale(alocale) 1.745 + , collator(NULL) 1.746 + , decimalFormatSymbols(NULL) 1.747 + , lenient(FALSE) 1.748 + , lenientParseRules(NULL) 1.749 + , localizations(NULL) 1.750 +{ 1.751 + if (U_FAILURE(status)) { 1.752 + return; 1.753 + } 1.754 + 1.755 + const char* rules_tag = "RBNFRules"; 1.756 + const char* fmt_tag = ""; 1.757 + switch (tag) { 1.758 + case URBNF_SPELLOUT: fmt_tag = "SpelloutRules"; break; 1.759 + case URBNF_ORDINAL: fmt_tag = "OrdinalRules"; break; 1.760 + case URBNF_DURATION: fmt_tag = "DurationRules"; break; 1.761 + case URBNF_NUMBERING_SYSTEM: fmt_tag = "NumberingSystemRules"; break; 1.762 + default: status = U_ILLEGAL_ARGUMENT_ERROR; return; 1.763 + } 1.764 + 1.765 + // TODO: read localization info from resource 1.766 + LocalizationInfo* locinfo = NULL; 1.767 + 1.768 + UResourceBundle* nfrb = ures_open(U_ICUDATA_RBNF, locale.getName(), &status); 1.769 + if (U_SUCCESS(status)) { 1.770 + setLocaleIDs(ures_getLocaleByType(nfrb, ULOC_VALID_LOCALE, &status), 1.771 + ures_getLocaleByType(nfrb, ULOC_ACTUAL_LOCALE, &status)); 1.772 + 1.773 + UResourceBundle* rbnfRules = ures_getByKeyWithFallback(nfrb, rules_tag, NULL, &status); 1.774 + if (U_FAILURE(status)) { 1.775 + ures_close(nfrb); 1.776 + } 1.777 + UResourceBundle* ruleSets = ures_getByKeyWithFallback(rbnfRules, fmt_tag, NULL, &status); 1.778 + if (U_FAILURE(status)) { 1.779 + ures_close(rbnfRules); 1.780 + ures_close(nfrb); 1.781 + return; 1.782 + } 1.783 + 1.784 + UnicodeString desc; 1.785 + while (ures_hasNext(ruleSets)) { 1.786 + desc.append(ures_getNextUnicodeString(ruleSets,NULL,&status)); 1.787 + } 1.788 + UParseError perror; 1.789 + 1.790 + init (desc, locinfo, perror, status); 1.791 + 1.792 + ures_close(ruleSets); 1.793 + ures_close(rbnfRules); 1.794 + } 1.795 + ures_close(nfrb); 1.796 +} 1.797 + 1.798 +RuleBasedNumberFormat::RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs) 1.799 + : NumberFormat(rhs) 1.800 + , ruleSets(NULL) 1.801 + , ruleSetDescriptions(NULL) 1.802 + , numRuleSets(0) 1.803 + , defaultRuleSet(NULL) 1.804 + , locale(rhs.locale) 1.805 + , collator(NULL) 1.806 + , decimalFormatSymbols(NULL) 1.807 + , lenient(FALSE) 1.808 + , lenientParseRules(NULL) 1.809 + , localizations(NULL) 1.810 +{ 1.811 + this->operator=(rhs); 1.812 +} 1.813 + 1.814 +// -------- 1.815 + 1.816 +RuleBasedNumberFormat& 1.817 +RuleBasedNumberFormat::operator=(const RuleBasedNumberFormat& rhs) 1.818 +{ 1.819 + UErrorCode status = U_ZERO_ERROR; 1.820 + dispose(); 1.821 + locale = rhs.locale; 1.822 + lenient = rhs.lenient; 1.823 + 1.824 + UnicodeString rules = rhs.getRules(); 1.825 + UParseError perror; 1.826 + init(rules, rhs.localizations ? rhs.localizations->ref() : NULL, perror, status); 1.827 + 1.828 + return *this; 1.829 +} 1.830 + 1.831 +RuleBasedNumberFormat::~RuleBasedNumberFormat() 1.832 +{ 1.833 + dispose(); 1.834 +} 1.835 + 1.836 +Format* 1.837 +RuleBasedNumberFormat::clone(void) const 1.838 +{ 1.839 + RuleBasedNumberFormat * result = NULL; 1.840 + UnicodeString rules = getRules(); 1.841 + UErrorCode status = U_ZERO_ERROR; 1.842 + UParseError perror; 1.843 + result = new RuleBasedNumberFormat(rules, localizations, locale, perror, status); 1.844 + /* test for NULL */ 1.845 + if (result == 0) { 1.846 + status = U_MEMORY_ALLOCATION_ERROR; 1.847 + return 0; 1.848 + } 1.849 + if (U_FAILURE(status)) { 1.850 + delete result; 1.851 + result = 0; 1.852 + } else { 1.853 + result->lenient = lenient; 1.854 + } 1.855 + return result; 1.856 +} 1.857 + 1.858 +UBool 1.859 +RuleBasedNumberFormat::operator==(const Format& other) const 1.860 +{ 1.861 + if (this == &other) { 1.862 + return TRUE; 1.863 + } 1.864 + 1.865 + if (typeid(*this) == typeid(other)) { 1.866 + const RuleBasedNumberFormat& rhs = (const RuleBasedNumberFormat&)other; 1.867 + if (locale == rhs.locale && 1.868 + lenient == rhs.lenient && 1.869 + (localizations == NULL 1.870 + ? rhs.localizations == NULL 1.871 + : (rhs.localizations == NULL 1.872 + ? FALSE 1.873 + : *localizations == rhs.localizations))) { 1.874 + 1.875 + NFRuleSet** p = ruleSets; 1.876 + NFRuleSet** q = rhs.ruleSets; 1.877 + if (p == NULL) { 1.878 + return q == NULL; 1.879 + } else if (q == NULL) { 1.880 + return FALSE; 1.881 + } 1.882 + while (*p && *q && (**p == **q)) { 1.883 + ++p; 1.884 + ++q; 1.885 + } 1.886 + return *q == NULL && *p == NULL; 1.887 + } 1.888 + } 1.889 + 1.890 + return FALSE; 1.891 +} 1.892 + 1.893 +UnicodeString 1.894 +RuleBasedNumberFormat::getRules() const 1.895 +{ 1.896 + UnicodeString result; 1.897 + if (ruleSets != NULL) { 1.898 + for (NFRuleSet** p = ruleSets; *p; ++p) { 1.899 + (*p)->appendRules(result); 1.900 + } 1.901 + } 1.902 + return result; 1.903 +} 1.904 + 1.905 +UnicodeString 1.906 +RuleBasedNumberFormat::getRuleSetName(int32_t index) const 1.907 +{ 1.908 + if (localizations) { 1.909 + UnicodeString string(TRUE, localizations->getRuleSetName(index), (int32_t)-1); 1.910 + return string; 1.911 + } else if (ruleSets) { 1.912 + UnicodeString result; 1.913 + for (NFRuleSet** p = ruleSets; *p; ++p) { 1.914 + NFRuleSet* rs = *p; 1.915 + if (rs->isPublic()) { 1.916 + if (--index == -1) { 1.917 + rs->getName(result); 1.918 + return result; 1.919 + } 1.920 + } 1.921 + } 1.922 + } 1.923 + UnicodeString empty; 1.924 + return empty; 1.925 +} 1.926 + 1.927 +int32_t 1.928 +RuleBasedNumberFormat::getNumberOfRuleSetNames() const 1.929 +{ 1.930 + int32_t result = 0; 1.931 + if (localizations) { 1.932 + result = localizations->getNumberOfRuleSets(); 1.933 + } else if (ruleSets) { 1.934 + for (NFRuleSet** p = ruleSets; *p; ++p) { 1.935 + if ((**p).isPublic()) { 1.936 + ++result; 1.937 + } 1.938 + } 1.939 + } 1.940 + return result; 1.941 +} 1.942 + 1.943 +int32_t 1.944 +RuleBasedNumberFormat::getNumberOfRuleSetDisplayNameLocales(void) const { 1.945 + if (localizations) { 1.946 + return localizations->getNumberOfDisplayLocales(); 1.947 + } 1.948 + return 0; 1.949 +} 1.950 + 1.951 +Locale 1.952 +RuleBasedNumberFormat::getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const { 1.953 + if (U_FAILURE(status)) { 1.954 + return Locale(""); 1.955 + } 1.956 + if (localizations && index >= 0 && index < localizations->getNumberOfDisplayLocales()) { 1.957 + UnicodeString name(TRUE, localizations->getLocaleName(index), -1); 1.958 + char buffer[64]; 1.959 + int32_t cap = name.length() + 1; 1.960 + char* bp = buffer; 1.961 + if (cap > 64) { 1.962 + bp = (char *)uprv_malloc(cap); 1.963 + if (bp == NULL) { 1.964 + status = U_MEMORY_ALLOCATION_ERROR; 1.965 + return Locale(""); 1.966 + } 1.967 + } 1.968 + name.extract(0, name.length(), bp, cap, UnicodeString::kInvariant); 1.969 + Locale retLocale(bp); 1.970 + if (bp != buffer) { 1.971 + uprv_free(bp); 1.972 + } 1.973 + return retLocale; 1.974 + } 1.975 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.976 + Locale retLocale; 1.977 + return retLocale; 1.978 +} 1.979 + 1.980 +UnicodeString 1.981 +RuleBasedNumberFormat::getRuleSetDisplayName(int32_t index, const Locale& localeParam) { 1.982 + if (localizations && index >= 0 && index < localizations->getNumberOfRuleSets()) { 1.983 + UnicodeString localeName(localeParam.getBaseName(), -1, UnicodeString::kInvariant); 1.984 + int32_t len = localeName.length(); 1.985 + UChar* localeStr = localeName.getBuffer(len + 1); 1.986 + while (len >= 0) { 1.987 + localeStr[len] = 0; 1.988 + int32_t ix = localizations->indexForLocale(localeStr); 1.989 + if (ix >= 0) { 1.990 + UnicodeString name(TRUE, localizations->getDisplayName(ix, index), -1); 1.991 + return name; 1.992 + } 1.993 + 1.994 + // trim trailing portion, skipping over ommitted sections 1.995 + do { --len;} while (len > 0 && localeStr[len] != 0x005f); // underscore 1.996 + while (len > 0 && localeStr[len-1] == 0x005F) --len; 1.997 + } 1.998 + UnicodeString name(TRUE, localizations->getRuleSetName(index), -1); 1.999 + return name; 1.1000 + } 1.1001 + UnicodeString bogus; 1.1002 + bogus.setToBogus(); 1.1003 + return bogus; 1.1004 +} 1.1005 + 1.1006 +UnicodeString 1.1007 +RuleBasedNumberFormat::getRuleSetDisplayName(const UnicodeString& ruleSetName, const Locale& localeParam) { 1.1008 + if (localizations) { 1.1009 + UnicodeString rsn(ruleSetName); 1.1010 + int32_t ix = localizations->indexForRuleSet(rsn.getTerminatedBuffer()); 1.1011 + return getRuleSetDisplayName(ix, localeParam); 1.1012 + } 1.1013 + UnicodeString bogus; 1.1014 + bogus.setToBogus(); 1.1015 + return bogus; 1.1016 +} 1.1017 + 1.1018 +NFRuleSet* 1.1019 +RuleBasedNumberFormat::findRuleSet(const UnicodeString& name, UErrorCode& status) const 1.1020 +{ 1.1021 + if (U_SUCCESS(status) && ruleSets) { 1.1022 + for (NFRuleSet** p = ruleSets; *p; ++p) { 1.1023 + NFRuleSet* rs = *p; 1.1024 + if (rs->isNamed(name)) { 1.1025 + return rs; 1.1026 + } 1.1027 + } 1.1028 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.1029 + } 1.1030 + return NULL; 1.1031 +} 1.1032 + 1.1033 +UnicodeString& 1.1034 +RuleBasedNumberFormat::format(int32_t number, 1.1035 + UnicodeString& toAppendTo, 1.1036 + FieldPosition& /* pos */) const 1.1037 +{ 1.1038 + if (defaultRuleSet) defaultRuleSet->format((int64_t)number, toAppendTo, toAppendTo.length()); 1.1039 + return toAppendTo; 1.1040 +} 1.1041 + 1.1042 + 1.1043 +UnicodeString& 1.1044 +RuleBasedNumberFormat::format(int64_t number, 1.1045 + UnicodeString& toAppendTo, 1.1046 + FieldPosition& /* pos */) const 1.1047 +{ 1.1048 + if (defaultRuleSet) defaultRuleSet->format(number, toAppendTo, toAppendTo.length()); 1.1049 + return toAppendTo; 1.1050 +} 1.1051 + 1.1052 + 1.1053 +UnicodeString& 1.1054 +RuleBasedNumberFormat::format(double number, 1.1055 + UnicodeString& toAppendTo, 1.1056 + FieldPosition& /* pos */) const 1.1057 +{ 1.1058 + // Special case for NaN; adapted from what DecimalFormat::_format( double number,...) does. 1.1059 + if (uprv_isNaN(number)) { 1.1060 + DecimalFormatSymbols* decFmtSyms = getDecimalFormatSymbols(); // RuleBasedNumberFormat internal 1.1061 + if (decFmtSyms) { 1.1062 + toAppendTo += decFmtSyms->getConstSymbol(DecimalFormatSymbols::kNaNSymbol); 1.1063 + } 1.1064 + } else if (defaultRuleSet) { 1.1065 + defaultRuleSet->format(number, toAppendTo, toAppendTo.length()); 1.1066 + } 1.1067 + return toAppendTo; 1.1068 +} 1.1069 + 1.1070 + 1.1071 +UnicodeString& 1.1072 +RuleBasedNumberFormat::format(int32_t number, 1.1073 + const UnicodeString& ruleSetName, 1.1074 + UnicodeString& toAppendTo, 1.1075 + FieldPosition& /* pos */, 1.1076 + UErrorCode& status) const 1.1077 +{ 1.1078 + // return format((int64_t)number, ruleSetName, toAppendTo, pos, status); 1.1079 + if (U_SUCCESS(status)) { 1.1080 + if (ruleSetName.indexOf(gPercentPercent, 2, 0) == 0) { 1.1081 + // throw new IllegalArgumentException("Can't use internal rule set"); 1.1082 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.1083 + } else { 1.1084 + NFRuleSet *rs = findRuleSet(ruleSetName, status); 1.1085 + if (rs) { 1.1086 + rs->format((int64_t)number, toAppendTo, toAppendTo.length()); 1.1087 + } 1.1088 + } 1.1089 + } 1.1090 + return toAppendTo; 1.1091 +} 1.1092 + 1.1093 + 1.1094 +UnicodeString& 1.1095 +RuleBasedNumberFormat::format(int64_t number, 1.1096 + const UnicodeString& ruleSetName, 1.1097 + UnicodeString& toAppendTo, 1.1098 + FieldPosition& /* pos */, 1.1099 + UErrorCode& status) const 1.1100 +{ 1.1101 + if (U_SUCCESS(status)) { 1.1102 + if (ruleSetName.indexOf(gPercentPercent, 2, 0) == 0) { 1.1103 + // throw new IllegalArgumentException("Can't use internal rule set"); 1.1104 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.1105 + } else { 1.1106 + NFRuleSet *rs = findRuleSet(ruleSetName, status); 1.1107 + if (rs) { 1.1108 + rs->format(number, toAppendTo, toAppendTo.length()); 1.1109 + } 1.1110 + } 1.1111 + } 1.1112 + return toAppendTo; 1.1113 +} 1.1114 + 1.1115 + 1.1116 +UnicodeString& 1.1117 +RuleBasedNumberFormat::format(double number, 1.1118 + const UnicodeString& ruleSetName, 1.1119 + UnicodeString& toAppendTo, 1.1120 + FieldPosition& /* pos */, 1.1121 + UErrorCode& status) const 1.1122 +{ 1.1123 + if (U_SUCCESS(status)) { 1.1124 + if (ruleSetName.indexOf(gPercentPercent, 2, 0) == 0) { 1.1125 + // throw new IllegalArgumentException("Can't use internal rule set"); 1.1126 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.1127 + } else { 1.1128 + NFRuleSet *rs = findRuleSet(ruleSetName, status); 1.1129 + if (rs) { 1.1130 + rs->format(number, toAppendTo, toAppendTo.length()); 1.1131 + } 1.1132 + } 1.1133 + } 1.1134 + return toAppendTo; 1.1135 +} 1.1136 + 1.1137 +void 1.1138 +RuleBasedNumberFormat::parse(const UnicodeString& text, 1.1139 + Formattable& result, 1.1140 + ParsePosition& parsePosition) const 1.1141 +{ 1.1142 + if (!ruleSets) { 1.1143 + parsePosition.setErrorIndex(0); 1.1144 + return; 1.1145 + } 1.1146 + 1.1147 + UnicodeString workingText(text, parsePosition.getIndex()); 1.1148 + ParsePosition workingPos(0); 1.1149 + 1.1150 + ParsePosition high_pp(0); 1.1151 + Formattable high_result; 1.1152 + 1.1153 + for (NFRuleSet** p = ruleSets; *p; ++p) { 1.1154 + NFRuleSet *rp = *p; 1.1155 + if (rp->isPublic() && rp->isParseable()) { 1.1156 + ParsePosition working_pp(0); 1.1157 + Formattable working_result; 1.1158 + 1.1159 + rp->parse(workingText, working_pp, kMaxDouble, working_result); 1.1160 + if (working_pp.getIndex() > high_pp.getIndex()) { 1.1161 + high_pp = working_pp; 1.1162 + high_result = working_result; 1.1163 + 1.1164 + if (high_pp.getIndex() == workingText.length()) { 1.1165 + break; 1.1166 + } 1.1167 + } 1.1168 + } 1.1169 + } 1.1170 + 1.1171 + int32_t startIndex = parsePosition.getIndex(); 1.1172 + parsePosition.setIndex(startIndex + high_pp.getIndex()); 1.1173 + if (high_pp.getIndex() > 0) { 1.1174 + parsePosition.setErrorIndex(-1); 1.1175 + } else { 1.1176 + int32_t errorIndex = (high_pp.getErrorIndex()>0)? high_pp.getErrorIndex(): 0; 1.1177 + parsePosition.setErrorIndex(startIndex + errorIndex); 1.1178 + } 1.1179 + result = high_result; 1.1180 + if (result.getType() == Formattable::kDouble) { 1.1181 + int32_t r = (int32_t)result.getDouble(); 1.1182 + if ((double)r == result.getDouble()) { 1.1183 + result.setLong(r); 1.1184 + } 1.1185 + } 1.1186 +} 1.1187 + 1.1188 +#if !UCONFIG_NO_COLLATION 1.1189 + 1.1190 +void 1.1191 +RuleBasedNumberFormat::setLenient(UBool enabled) 1.1192 +{ 1.1193 + lenient = enabled; 1.1194 + if (!enabled && collator) { 1.1195 + delete collator; 1.1196 + collator = NULL; 1.1197 + } 1.1198 +} 1.1199 + 1.1200 +#endif 1.1201 + 1.1202 +void 1.1203 +RuleBasedNumberFormat::setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status) { 1.1204 + if (U_SUCCESS(status)) { 1.1205 + if (ruleSetName.isEmpty()) { 1.1206 + if (localizations) { 1.1207 + UnicodeString name(TRUE, localizations->getRuleSetName(0), -1); 1.1208 + defaultRuleSet = findRuleSet(name, status); 1.1209 + } else { 1.1210 + initDefaultRuleSet(); 1.1211 + } 1.1212 + } else if (ruleSetName.startsWith(UNICODE_STRING_SIMPLE("%%"))) { 1.1213 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.1214 + } else { 1.1215 + NFRuleSet* result = findRuleSet(ruleSetName, status); 1.1216 + if (result != NULL) { 1.1217 + defaultRuleSet = result; 1.1218 + } 1.1219 + } 1.1220 + } 1.1221 +} 1.1222 + 1.1223 +UnicodeString 1.1224 +RuleBasedNumberFormat::getDefaultRuleSetName() const { 1.1225 + UnicodeString result; 1.1226 + if (defaultRuleSet && defaultRuleSet->isPublic()) { 1.1227 + defaultRuleSet->getName(result); 1.1228 + } else { 1.1229 + result.setToBogus(); 1.1230 + } 1.1231 + return result; 1.1232 +} 1.1233 + 1.1234 +void 1.1235 +RuleBasedNumberFormat::initDefaultRuleSet() 1.1236 +{ 1.1237 + defaultRuleSet = NULL; 1.1238 + if (!ruleSets) { 1.1239 + return; 1.1240 + } 1.1241 + 1.1242 + const UnicodeString spellout = UNICODE_STRING_SIMPLE("%spellout-numbering"); 1.1243 + const UnicodeString ordinal = UNICODE_STRING_SIMPLE("%digits-ordinal"); 1.1244 + const UnicodeString duration = UNICODE_STRING_SIMPLE("%duration"); 1.1245 + 1.1246 + NFRuleSet**p = &ruleSets[0]; 1.1247 + while (*p) { 1.1248 + if ((*p)->isNamed(spellout) || (*p)->isNamed(ordinal) || (*p)->isNamed(duration)) { 1.1249 + defaultRuleSet = *p; 1.1250 + return; 1.1251 + } else { 1.1252 + ++p; 1.1253 + } 1.1254 + } 1.1255 + 1.1256 + defaultRuleSet = *--p; 1.1257 + if (!defaultRuleSet->isPublic()) { 1.1258 + while (p != ruleSets) { 1.1259 + if ((*--p)->isPublic()) { 1.1260 + defaultRuleSet = *p; 1.1261 + break; 1.1262 + } 1.1263 + } 1.1264 + } 1.1265 +} 1.1266 + 1.1267 + 1.1268 +void 1.1269 +RuleBasedNumberFormat::init(const UnicodeString& rules, LocalizationInfo* localizationInfos, 1.1270 + UParseError& pErr, UErrorCode& status) 1.1271 +{ 1.1272 + // TODO: implement UParseError 1.1273 + uprv_memset(&pErr, 0, sizeof(UParseError)); 1.1274 + // Note: this can leave ruleSets == NULL, so remaining code should check 1.1275 + if (U_FAILURE(status)) { 1.1276 + return; 1.1277 + } 1.1278 + 1.1279 + this->localizations = localizationInfos == NULL ? NULL : localizationInfos->ref(); 1.1280 + 1.1281 + UnicodeString description(rules); 1.1282 + if (!description.length()) { 1.1283 + status = U_MEMORY_ALLOCATION_ERROR; 1.1284 + return; 1.1285 + } 1.1286 + 1.1287 + // start by stripping the trailing whitespace from all the rules 1.1288 + // (this is all the whitespace follwing each semicolon in the 1.1289 + // description). This allows us to look for rule-set boundaries 1.1290 + // by searching for ";%" without having to worry about whitespace 1.1291 + // between the ; and the % 1.1292 + stripWhitespace(description); 1.1293 + 1.1294 + // check to see if there's a set of lenient-parse rules. If there 1.1295 + // is, pull them out into our temporary holding place for them, 1.1296 + // and delete them from the description before the real desciption- 1.1297 + // parsing code sees them 1.1298 + int32_t lp = description.indexOf(gLenientParse, -1, 0); 1.1299 + if (lp != -1) { 1.1300 + // we've got to make sure we're not in the middle of a rule 1.1301 + // (where "%%lenient-parse" would actually get treated as 1.1302 + // rule text) 1.1303 + if (lp == 0 || description.charAt(lp - 1) == gSemiColon) { 1.1304 + // locate the beginning and end of the actual collation 1.1305 + // rules (there may be whitespace between the name and 1.1306 + // the first token in the description) 1.1307 + int lpEnd = description.indexOf(gSemiPercent, 2, lp); 1.1308 + 1.1309 + if (lpEnd == -1) { 1.1310 + lpEnd = description.length() - 1; 1.1311 + } 1.1312 + int lpStart = lp + u_strlen(gLenientParse); 1.1313 + while (PatternProps::isWhiteSpace(description.charAt(lpStart))) { 1.1314 + ++lpStart; 1.1315 + } 1.1316 + 1.1317 + // copy out the lenient-parse rules and delete them 1.1318 + // from the description 1.1319 + lenientParseRules = new UnicodeString(); 1.1320 + /* test for NULL */ 1.1321 + if (lenientParseRules == 0) { 1.1322 + status = U_MEMORY_ALLOCATION_ERROR; 1.1323 + return; 1.1324 + } 1.1325 + lenientParseRules->setTo(description, lpStart, lpEnd - lpStart); 1.1326 + 1.1327 + description.remove(lp, lpEnd + 1 - lp); 1.1328 + } 1.1329 + } 1.1330 + 1.1331 + // pre-flight parsing the description and count the number of 1.1332 + // rule sets (";%" marks the end of one rule set and the beginning 1.1333 + // of the next) 1.1334 + numRuleSets = 0; 1.1335 + for (int32_t p = description.indexOf(gSemiPercent, 2, 0); p != -1; p = description.indexOf(gSemiPercent, 2, p)) { 1.1336 + ++numRuleSets; 1.1337 + ++p; 1.1338 + } 1.1339 + ++numRuleSets; 1.1340 + 1.1341 + // our rule list is an array of the appropriate size 1.1342 + ruleSets = (NFRuleSet **)uprv_malloc((numRuleSets + 1) * sizeof(NFRuleSet *)); 1.1343 + /* test for NULL */ 1.1344 + if (ruleSets == 0) { 1.1345 + status = U_MEMORY_ALLOCATION_ERROR; 1.1346 + return; 1.1347 + } 1.1348 + 1.1349 + for (int i = 0; i <= numRuleSets; ++i) { 1.1350 + ruleSets[i] = NULL; 1.1351 + } 1.1352 + 1.1353 + // divide up the descriptions into individual rule-set descriptions 1.1354 + // and store them in a temporary array. At each step, we also 1.1355 + // new up a rule set, but all this does is initialize its name 1.1356 + // and remove it from its description. We can't actually parse 1.1357 + // the rest of the descriptions and finish initializing everything 1.1358 + // because we have to know the names and locations of all the rule 1.1359 + // sets before we can actually set everything up 1.1360 + if(!numRuleSets) { 1.1361 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.1362 + return; 1.1363 + } 1.1364 + 1.1365 + ruleSetDescriptions = new UnicodeString[numRuleSets]; 1.1366 + if (ruleSetDescriptions == 0) { 1.1367 + status = U_MEMORY_ALLOCATION_ERROR; 1.1368 + return; 1.1369 + } 1.1370 + 1.1371 + { 1.1372 + int curRuleSet = 0; 1.1373 + int32_t start = 0; 1.1374 + for (int32_t p = description.indexOf(gSemiPercent, 2, 0); p != -1; p = description.indexOf(gSemiPercent, 2, start)) { 1.1375 + ruleSetDescriptions[curRuleSet].setTo(description, start, p + 1 - start); 1.1376 + ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet, status); 1.1377 + if (ruleSets[curRuleSet] == 0) { 1.1378 + status = U_MEMORY_ALLOCATION_ERROR; 1.1379 + return; 1.1380 + } 1.1381 + ++curRuleSet; 1.1382 + start = p + 1; 1.1383 + } 1.1384 + ruleSetDescriptions[curRuleSet].setTo(description, start, description.length() - start); 1.1385 + ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet, status); 1.1386 + if (ruleSets[curRuleSet] == 0) { 1.1387 + status = U_MEMORY_ALLOCATION_ERROR; 1.1388 + return; 1.1389 + } 1.1390 + } 1.1391 + 1.1392 + // now we can take note of the formatter's default rule set, which 1.1393 + // is the last public rule set in the description (it's the last 1.1394 + // rather than the first so that a user can create a new formatter 1.1395 + // from an existing formatter and change its default behavior just 1.1396 + // by appending more rule sets to the end) 1.1397 + 1.1398 + // {dlf} Initialization of a fraction rule set requires the default rule 1.1399 + // set to be known. For purposes of initialization, this is always the 1.1400 + // last public rule set, no matter what the localization data says. 1.1401 + initDefaultRuleSet(); 1.1402 + 1.1403 + // finally, we can go back through the temporary descriptions 1.1404 + // list and finish seting up the substructure (and we throw 1.1405 + // away the temporary descriptions as we go) 1.1406 + { 1.1407 + for (int i = 0; i < numRuleSets; i++) { 1.1408 + ruleSets[i]->parseRules(ruleSetDescriptions[i], this, status); 1.1409 + } 1.1410 + } 1.1411 + 1.1412 + // Now that the rules are initialized, the 'real' default rule 1.1413 + // set can be adjusted by the localization data. 1.1414 + 1.1415 + // The C code keeps the localization array as is, rather than building 1.1416 + // a separate array of the public rule set names, so we have less work 1.1417 + // to do here-- but we still need to check the names. 1.1418 + 1.1419 + if (localizationInfos) { 1.1420 + // confirm the names, if any aren't in the rules, that's an error 1.1421 + // it is ok if the rules contain public rule sets that are not in this list 1.1422 + for (int32_t i = 0; i < localizationInfos->getNumberOfRuleSets(); ++i) { 1.1423 + UnicodeString name(TRUE, localizationInfos->getRuleSetName(i), -1); 1.1424 + NFRuleSet* rs = findRuleSet(name, status); 1.1425 + if (rs == NULL) { 1.1426 + break; // error 1.1427 + } 1.1428 + if (i == 0) { 1.1429 + defaultRuleSet = rs; 1.1430 + } 1.1431 + } 1.1432 + } else { 1.1433 + defaultRuleSet = getDefaultRuleSet(); 1.1434 + } 1.1435 +} 1.1436 + 1.1437 +void 1.1438 +RuleBasedNumberFormat::stripWhitespace(UnicodeString& description) 1.1439 +{ 1.1440 + // iterate through the characters... 1.1441 + UnicodeString result; 1.1442 + 1.1443 + int start = 0; 1.1444 + while (start != -1 && start < description.length()) { 1.1445 + // seek to the first non-whitespace character... 1.1446 + while (start < description.length() 1.1447 + && PatternProps::isWhiteSpace(description.charAt(start))) { 1.1448 + ++start; 1.1449 + } 1.1450 + 1.1451 + // locate the next semicolon in the text and copy the text from 1.1452 + // our current position up to that semicolon into the result 1.1453 + int32_t p = description.indexOf(gSemiColon, start); 1.1454 + if (p == -1) { 1.1455 + // or if we don't find a semicolon, just copy the rest of 1.1456 + // the string into the result 1.1457 + result.append(description, start, description.length() - start); 1.1458 + start = -1; 1.1459 + } 1.1460 + else if (p < description.length()) { 1.1461 + result.append(description, start, p + 1 - start); 1.1462 + start = p + 1; 1.1463 + } 1.1464 + 1.1465 + // when we get here, we've seeked off the end of the sring, and 1.1466 + // we terminate the loop (we continue until *start* is -1 rather 1.1467 + // than until *p* is -1, because otherwise we'd miss the last 1.1468 + // rule in the description) 1.1469 + else { 1.1470 + start = -1; 1.1471 + } 1.1472 + } 1.1473 + 1.1474 + description.setTo(result); 1.1475 +} 1.1476 + 1.1477 + 1.1478 +void 1.1479 +RuleBasedNumberFormat::dispose() 1.1480 +{ 1.1481 + if (ruleSets) { 1.1482 + for (NFRuleSet** p = ruleSets; *p; ++p) { 1.1483 + delete *p; 1.1484 + } 1.1485 + uprv_free(ruleSets); 1.1486 + ruleSets = NULL; 1.1487 + } 1.1488 + 1.1489 + if (ruleSetDescriptions) { 1.1490 + delete [] ruleSetDescriptions; 1.1491 + } 1.1492 + 1.1493 +#if !UCONFIG_NO_COLLATION 1.1494 + delete collator; 1.1495 +#endif 1.1496 + collator = NULL; 1.1497 + 1.1498 + delete decimalFormatSymbols; 1.1499 + decimalFormatSymbols = NULL; 1.1500 + 1.1501 + delete lenientParseRules; 1.1502 + lenientParseRules = NULL; 1.1503 + 1.1504 + if (localizations) localizations = localizations->unref(); 1.1505 +} 1.1506 + 1.1507 + 1.1508 +//----------------------------------------------------------------------- 1.1509 +// package-internal API 1.1510 +//----------------------------------------------------------------------- 1.1511 + 1.1512 +/** 1.1513 + * Returns the collator to use for lenient parsing. The collator is lazily created: 1.1514 + * this function creates it the first time it's called. 1.1515 + * @return The collator to use for lenient parsing, or null if lenient parsing 1.1516 + * is turned off. 1.1517 +*/ 1.1518 +Collator* 1.1519 +RuleBasedNumberFormat::getCollator() const 1.1520 +{ 1.1521 +#if !UCONFIG_NO_COLLATION 1.1522 + if (!ruleSets) { 1.1523 + return NULL; 1.1524 + } 1.1525 + 1.1526 + // lazy-evaulate the collator 1.1527 + if (collator == NULL && lenient) { 1.1528 + // create a default collator based on the formatter's locale, 1.1529 + // then pull out that collator's rules, append any additional 1.1530 + // rules specified in the description, and create a _new_ 1.1531 + // collator based on the combinaiton of those rules 1.1532 + 1.1533 + UErrorCode status = U_ZERO_ERROR; 1.1534 + 1.1535 + Collator* temp = Collator::createInstance(locale, status); 1.1536 + RuleBasedCollator* newCollator; 1.1537 + if (U_SUCCESS(status) && (newCollator = dynamic_cast<RuleBasedCollator*>(temp)) != NULL) { 1.1538 + if (lenientParseRules) { 1.1539 + UnicodeString rules(newCollator->getRules()); 1.1540 + rules.append(*lenientParseRules); 1.1541 + 1.1542 + newCollator = new RuleBasedCollator(rules, status); 1.1543 + // Exit if newCollator could not be created. 1.1544 + if (newCollator == NULL) { 1.1545 + return NULL; 1.1546 + } 1.1547 + } else { 1.1548 + temp = NULL; 1.1549 + } 1.1550 + if (U_SUCCESS(status)) { 1.1551 + newCollator->setAttribute(UCOL_DECOMPOSITION_MODE, UCOL_ON, status); 1.1552 + // cast away const 1.1553 + ((RuleBasedNumberFormat*)this)->collator = newCollator; 1.1554 + } else { 1.1555 + delete newCollator; 1.1556 + } 1.1557 + } 1.1558 + delete temp; 1.1559 + } 1.1560 +#endif 1.1561 + 1.1562 + // if lenient-parse mode is off, this will be null 1.1563 + // (see setLenientParseMode()) 1.1564 + return collator; 1.1565 +} 1.1566 + 1.1567 + 1.1568 +/** 1.1569 + * Returns the DecimalFormatSymbols object that should be used by all DecimalFormat 1.1570 + * instances owned by this formatter. This object is lazily created: this function 1.1571 + * creates it the first time it's called. 1.1572 + * @return The DecimalFormatSymbols object that should be used by all DecimalFormat 1.1573 + * instances owned by this formatter. 1.1574 +*/ 1.1575 +DecimalFormatSymbols* 1.1576 +RuleBasedNumberFormat::getDecimalFormatSymbols() const 1.1577 +{ 1.1578 + // lazy-evaluate the DecimalFormatSymbols object. This object 1.1579 + // is shared by all DecimalFormat instances belonging to this 1.1580 + // formatter 1.1581 + if (decimalFormatSymbols == NULL) { 1.1582 + UErrorCode status = U_ZERO_ERROR; 1.1583 + DecimalFormatSymbols* temp = new DecimalFormatSymbols(locale, status); 1.1584 + if (U_SUCCESS(status)) { 1.1585 + ((RuleBasedNumberFormat*)this)->decimalFormatSymbols = temp; 1.1586 + } else { 1.1587 + delete temp; 1.1588 + } 1.1589 + } 1.1590 + return decimalFormatSymbols; 1.1591 +} 1.1592 + 1.1593 +// De-owning the current localized symbols and adopt the new symbols. 1.1594 +void 1.1595 +RuleBasedNumberFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt) 1.1596 +{ 1.1597 + if (symbolsToAdopt == NULL) { 1.1598 + return; // do not allow caller to set decimalFormatSymbols to NULL 1.1599 + } 1.1600 + 1.1601 + if (decimalFormatSymbols != NULL) { 1.1602 + delete decimalFormatSymbols; 1.1603 + } 1.1604 + 1.1605 + decimalFormatSymbols = symbolsToAdopt; 1.1606 + 1.1607 + { 1.1608 + // Apply the new decimalFormatSymbols by reparsing the rulesets 1.1609 + UErrorCode status = U_ZERO_ERROR; 1.1610 + 1.1611 + for (int32_t i = 0; i < numRuleSets; i++) { 1.1612 + ruleSets[i]->parseRules(ruleSetDescriptions[i], this, status); 1.1613 + } 1.1614 + } 1.1615 +} 1.1616 + 1.1617 +// Setting the symbols is equlivalent to adopting a newly created localized symbols. 1.1618 +void 1.1619 +RuleBasedNumberFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols) 1.1620 +{ 1.1621 + adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols)); 1.1622 +} 1.1623 + 1.1624 +U_NAMESPACE_END 1.1625 + 1.1626 +/* U_HAVE_RBNF */ 1.1627 +#endif