+U_NAMESPACE_BEGIN
+
+using number::impl::DecimalQuantity;
+
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedNumberFormat)
+
+/*
+This is a utility class. It does not use ICU's RTTI.
+If ICU's RTTI is needed again, you can uncomment the RTTI code and derive from UObject.
+Please make sure that intltest passes on Windows in Release mode,
+since the string pooling per compilation unit will mess up how RTTI works.
+The RTTI code was also removed due to lack of code coverage.
+*/
+class LocalizationInfo : public UMemory {
+protected:
+ virtual ~LocalizationInfo();
+ uint32_t refcount;
+
+public:
+ LocalizationInfo() : refcount(0) {}
+
+ LocalizationInfo* ref(void) {
+ ++refcount;
+ return this;
+ }
+
+ LocalizationInfo* unref(void) {
+ if (refcount && --refcount == 0) {
+ delete this;
+ }
+ return NULL;
+ }
+
+ virtual UBool operator==(const LocalizationInfo* rhs) const;
+ inline UBool operator!=(const LocalizationInfo* rhs) const { return !operator==(rhs); }
+
+ virtual int32_t getNumberOfRuleSets(void) const = 0;
+ virtual const UChar* getRuleSetName(int32_t index) const = 0;
+ virtual int32_t getNumberOfDisplayLocales(void) const = 0;
+ virtual const UChar* getLocaleName(int32_t index) const = 0;
+ virtual const UChar* getDisplayName(int32_t localeIndex, int32_t ruleIndex) const = 0;
+
+ virtual int32_t indexForLocale(const UChar* locale) const;
+ virtual int32_t indexForRuleSet(const UChar* ruleset) const;
+
+// virtual UClassID getDynamicClassID() const = 0;
+// static UClassID getStaticClassID(void);
+};
+
+LocalizationInfo::~LocalizationInfo() {}
+
+//UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(LocalizationInfo)
+
+// if both strings are NULL, this returns TRUE
+static UBool
+streq(const UChar* lhs, const UChar* rhs) {
+ if (rhs == lhs) {
+ return TRUE;
+ }
+ if (lhs && rhs) {
+ return u_strcmp(lhs, rhs) == 0;
+ }
+ return FALSE;
+}
+
+UBool
+LocalizationInfo::operator==(const LocalizationInfo* rhs) const {
+ if (rhs) {
+ if (this == rhs) {
+ return TRUE;
+ }
+
+ int32_t rsc = getNumberOfRuleSets();
+ if (rsc == rhs->getNumberOfRuleSets()) {
+ for (int i = 0; i < rsc; ++i) {
+ if (!streq(getRuleSetName(i), rhs->getRuleSetName(i))) {
+ return FALSE;
+ }
+ }
+ int32_t dlc = getNumberOfDisplayLocales();
+ if (dlc == rhs->getNumberOfDisplayLocales()) {
+ for (int i = 0; i < dlc; ++i) {
+ const UChar* locale = getLocaleName(i);
+ int32_t ix = rhs->indexForLocale(locale);
+ // if no locale, ix is -1, getLocaleName returns null, so streq returns false
+ if (!streq(locale, rhs->getLocaleName(ix))) {
+ return FALSE;
+ }
+ for (int j = 0; j < rsc; ++j) {
+ if (!streq(getDisplayName(i, j), rhs->getDisplayName(ix, j))) {
+ return FALSE;
+ }
+ }
+ }
+ return TRUE;
+ }
+ }
+ }
+ return FALSE;
+}
+
+int32_t
+LocalizationInfo::indexForLocale(const UChar* locale) const {
+ for (int i = 0; i < getNumberOfDisplayLocales(); ++i) {
+ if (streq(locale, getLocaleName(i))) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+int32_t
+LocalizationInfo::indexForRuleSet(const UChar* ruleset) const {
+ if (ruleset) {
+ for (int i = 0; i < getNumberOfRuleSets(); ++i) {
+ if (streq(ruleset, getRuleSetName(i))) {
+ return i;
+ }
+ }
+ }
+ return -1;
+}
+
+
+typedef void (*Fn_Deleter)(void*);
+
+class VArray {
+ void** buf;
+ int32_t cap;
+ int32_t size;
+ Fn_Deleter deleter;
+public:
+ VArray() : buf(NULL), cap(0), size(0), deleter(NULL) {}
+
+ VArray(Fn_Deleter del) : buf(NULL), cap(0), size(0), deleter(del) {}
+
+ ~VArray() {
+ if (deleter) {
+ for (int i = 0; i < size; ++i) {
+ (*deleter)(buf[i]);
+ }
+ }
+ uprv_free(buf);
+ }
+
+ int32_t length() {
+ return size;
+ }
+
+ void add(void* elem, UErrorCode& status) {
+ if (U_SUCCESS(status)) {
+ if (size == cap) {
+ if (cap == 0) {
+ cap = 1;
+ } else if (cap < 256) {
+ cap *= 2;
+ } else {
+ cap += 256;
+ }
+ if (buf == NULL) {
+ buf = (void**)uprv_malloc(cap * sizeof(void*));
+ } else {
+ buf = (void**)uprv_realloc(buf, cap * sizeof(void*));
+ }
+ if (buf == NULL) {
+ // if we couldn't realloc, we leak the memory we've already allocated, but we're in deep trouble anyway
+ status = U_MEMORY_ALLOCATION_ERROR;
+ return;
+ }
+ void* start = &buf[size];
+ size_t count = (cap - size) * sizeof(void*);
+ uprv_memset(start, 0, count); // fill with nulls, just because
+ }
+ buf[size++] = elem;
+ }
+ }
+
+ void** release(void) {
+ void** result = buf;
+ buf = NULL;
+ cap = 0;
+ size = 0;
+ return result;
+ }
+};
+
+class LocDataParser;
+
+class StringLocalizationInfo : public LocalizationInfo {
+ UChar* info;
+ UChar*** data;
+ int32_t numRuleSets;
+ int32_t numLocales;
+
+friend class LocDataParser;
+
+ StringLocalizationInfo(UChar* i, UChar*** d, int32_t numRS, int32_t numLocs)
+ : info(i), data(d), numRuleSets(numRS), numLocales(numLocs)
+ {
+ }
+
+public:
+ static StringLocalizationInfo* create(const UnicodeString& info, UParseError& perror, UErrorCode& status);
+
+ virtual ~StringLocalizationInfo();
+ virtual int32_t getNumberOfRuleSets(void) const { return numRuleSets; }
+ virtual const UChar* getRuleSetName(int32_t index) const;
+ virtual int32_t getNumberOfDisplayLocales(void) const { return numLocales; }
+ virtual const UChar* getLocaleName(int32_t index) const;
+ virtual const UChar* getDisplayName(int32_t localeIndex, int32_t ruleIndex) const;
+
+// virtual UClassID getDynamicClassID() const;
+// static UClassID getStaticClassID(void);
+
+private:
+ void init(UErrorCode& status) const;
+};
+
+
+enum {
+ OPEN_ANGLE = 0x003c, /* '<' */
+ CLOSE_ANGLE = 0x003e, /* '>' */
+ COMMA = 0x002c,
+ TICK = 0x0027,
+ QUOTE = 0x0022,
+ SPACE = 0x0020
+};
+
+/**
+ * Utility for parsing a localization string and returning a StringLocalizationInfo*.
+ */
+class LocDataParser {
+ UChar* data;
+ const UChar* e;
+ UChar* p;
+ UChar ch;
+ UParseError& pe;
+ UErrorCode& ec;
+
+public:
+ LocDataParser(UParseError& parseError, UErrorCode& status)
+ : data(NULL), e(NULL), p(NULL), ch(0xffff), pe(parseError), ec(status) {}
+ ~LocDataParser() {}
+
+ /*
+ * On a successful parse, return a StringLocalizationInfo*, otherwise delete locData, set perror and status,
+ * and return NULL. The StringLocalizationInfo will adopt locData if it is created.
+ */
+ StringLocalizationInfo* parse(UChar* data, int32_t len);
+
+private:
+
+ inline void inc(void) {
+ ++p;
+ ch = 0xffff;
+ }
+ inline UBool checkInc(UChar c) {
+ if (p < e && (ch == c || *p == c)) {
+ inc();
+ return TRUE;
+ }
+ return FALSE;
+ }
+ inline UBool check(UChar c) {
+ return p < e && (ch == c || *p == c);
+ }
+ inline void skipWhitespace(void) {
+ while (p < e && PatternProps::isWhiteSpace(ch != 0xffff ? ch : *p)) {
+ inc();
+ }
+ }
+ inline UBool inList(UChar c, const UChar* list) const {
+ if (*list == SPACE && PatternProps::isWhiteSpace(c)) {
+ return TRUE;
+ }
+ while (*list && *list != c) {
+ ++list;
+ }
+ return *list == c;
+ }
+ void parseError(const char* msg);
+
+ StringLocalizationInfo* doParse(void);
+
+ UChar** nextArray(int32_t& requiredLength);
+ UChar* nextString(void);
+};
+
+#ifdef RBNF_DEBUG
+#define ERROR(msg) parseError(msg); return NULL;
+#define EXPLANATION_ARG explanationArg
+#else
+#define ERROR(msg) parseError(NULL); return NULL;
+#define EXPLANATION_ARG
+#endif
+
+
+static const UChar DQUOTE_STOPLIST[] = {
+ QUOTE, 0
+};
+
+static const UChar SQUOTE_STOPLIST[] = {
+ TICK, 0
+};
+
+static const UChar NOQUOTE_STOPLIST[] = {
+ SPACE, COMMA, CLOSE_ANGLE, OPEN_ANGLE, TICK, QUOTE, 0
+};
+
+static void
+DeleteFn(void* p) {
+ uprv_free(p);
+}
+
+StringLocalizationInfo*
+LocDataParser::parse(UChar* _data, int32_t len) {
+ if (U_FAILURE(ec)) {
+ if (_data) uprv_free(_data);
+ return NULL;
+ }
+
+ pe.line = 0;
+ pe.offset = -1;
+ pe.postContext[0] = 0;
+ pe.preContext[0] = 0;
+
+ if (_data == NULL) {
+ ec = U_ILLEGAL_ARGUMENT_ERROR;
+ return NULL;
+ }
+
+ if (len <= 0) {
+ ec = U_ILLEGAL_ARGUMENT_ERROR;
+ uprv_free(_data);
+ return NULL;
+ }
+
+ data = _data;
+ e = data + len;
+ p = _data;
+ ch = 0xffff;
+
+ return doParse();
+}
+
+
+StringLocalizationInfo*
+LocDataParser::doParse(void) {
+ skipWhitespace();
+ if (!checkInc(OPEN_ANGLE)) {
+ ERROR("Missing open angle");
+ } else {
+ VArray array(DeleteFn);
+ UBool mightHaveNext = TRUE;
+ int32_t requiredLength = -1;
+ while (mightHaveNext) {
+ mightHaveNext = FALSE;
+ UChar** elem = nextArray(requiredLength);
+ skipWhitespace();
+ UBool haveComma = check(COMMA);
+ if (elem) {
+ array.add(elem, ec);
+ if (haveComma) {
+ inc();
+ mightHaveNext = TRUE;
+ }
+ } else if (haveComma) {
+ ERROR("Unexpected character");
+ }
+ }
+
+ skipWhitespace();
+ if (!checkInc(CLOSE_ANGLE)) {
+ if (check(OPEN_ANGLE)) {
+ ERROR("Missing comma in outer array");
+ } else {
+ ERROR("Missing close angle bracket in outer array");
+ }
+ }
+
+ skipWhitespace();
+ if (p != e) {
+ ERROR("Extra text after close of localization data");
+ }
+
+ array.add(NULL, ec);
+ if (U_SUCCESS(ec)) {
+ int32_t numLocs = array.length() - 2; // subtract first, NULL
+ UChar*** result = (UChar***)array.release();
+
+ return new StringLocalizationInfo(data, result, requiredLength-2, numLocs); // subtract first, NULL
+ }
+ }
+
+ ERROR("Unknown error");
+}
+
+UChar**
+LocDataParser::nextArray(int32_t& requiredLength) {
+ if (U_FAILURE(ec)) {
+ return NULL;
+ }
+
+ skipWhitespace();
+ if (!checkInc(OPEN_ANGLE)) {
+ ERROR("Missing open angle");
+ }
+
+ VArray array;
+ UBool mightHaveNext = TRUE;
+ while (mightHaveNext) {
+ mightHaveNext = FALSE;
+ UChar* elem = nextString();
+ skipWhitespace();
+ UBool haveComma = check(COMMA);
+ if (elem) {
+ array.add(elem, ec);
+ if (haveComma) {
+ inc();
+ mightHaveNext = TRUE;
+ }
+ } else if (haveComma) {
+ ERROR("Unexpected comma");
+ }
+ }
+ skipWhitespace();
+ if (!checkInc(CLOSE_ANGLE)) {
+ if (check(OPEN_ANGLE)) {
+ ERROR("Missing close angle bracket in inner array");
+ } else {
+ ERROR("Missing comma in inner array");
+ }
+ }
+
+ array.add(NULL, ec);
+ if (U_SUCCESS(ec)) {
+ if (requiredLength == -1) {
+ requiredLength = array.length() + 1;
+ } else if (array.length() != requiredLength) {
+ ec = U_ILLEGAL_ARGUMENT_ERROR;
+ ERROR("Array not of required length");
+ }
+
+ return (UChar**)array.release();
+ }
+ ERROR("Unknown Error");
+}
+
+UChar*
+LocDataParser::nextString() {
+ UChar* result = NULL;
+
+ skipWhitespace();
+ if (p < e) {
+ const UChar* terminators;
+ UChar c = *p;
+ UBool haveQuote = c == QUOTE || c == TICK;
+ if (haveQuote) {
+ inc();
+ terminators = c == QUOTE ? DQUOTE_STOPLIST : SQUOTE_STOPLIST;
+ } else {
+ terminators = NOQUOTE_STOPLIST;
+ }
+ UChar* start = p;
+ while (p < e && !inList(*p, terminators)) ++p;
+ if (p == e) {
+ ERROR("Unexpected end of data");
+ }
+
+ UChar x = *p;
+ if (p > start) {
+ ch = x;
+ *p = 0x0; // terminate by writing to data
+ result = start; // just point into data
+ }
+ if (haveQuote) {
+ if (x != c) {
+ ERROR("Missing matching quote");
+ } else if (p == start) {
+ ERROR("Empty string");
+ }
+ inc();
+ } else if (x == OPEN_ANGLE || x == TICK || x == QUOTE) {
+ ERROR("Unexpected character in string");
+ }
+ }
+
+ // ok for there to be no next string
+ return result;
+}
+
+void LocDataParser::parseError(const char* EXPLANATION_ARG)
+{
+ if (!data) {
+ return;
+ }
+
+ const UChar* start = p - U_PARSE_CONTEXT_LEN - 1;
+ if (start < data) {
+ start = data;
+ }
+ for (UChar* x = p; --x >= start;) {
+ if (!*x) {
+ start = x+1;
+ break;
+ }
+ }
+ const UChar* limit = p + U_PARSE_CONTEXT_LEN - 1;
+ if (limit > e) {
+ limit = e;
+ }
+ u_strncpy(pe.preContext, start, (int32_t)(p-start));
+ pe.preContext[p-start] = 0;
+ u_strncpy(pe.postContext, p, (int32_t)(limit-p));
+ pe.postContext[limit-p] = 0;
+ pe.offset = (int32_t)(p - data);
+
+#ifdef RBNF_DEBUG
+ fprintf(stderr, "%s at or near character %ld: ", EXPLANATION_ARG, p-data);
+
+ UnicodeString msg;
+ msg.append(start, p - start);
+ msg.append((UChar)0x002f); /* SOLIDUS/SLASH */
+ msg.append(p, limit-p);
+ msg.append(UNICODE_STRING_SIMPLE("'"));
+
+ char buf[128];
+ int32_t len = msg.extract(0, msg.length(), buf, 128);
+ if (len >= 128) {
+ buf[127] = 0;
+ } else {
+ buf[len] = 0;
+ }
+ fprintf(stderr, "%s\n", buf);
+ fflush(stderr);
+#endif
+
+ uprv_free(data);
+ data = NULL;
+ p = NULL;
+ e = NULL;
+
+ if (U_SUCCESS(ec)) {
+ ec = U_PARSE_ERROR;
+ }
+}
+
+//UOBJECT_DEFINE_RTTI_IMPLEMENTATION(StringLocalizationInfo)
+
+StringLocalizationInfo*
+StringLocalizationInfo::create(const UnicodeString& info, UParseError& perror, UErrorCode& status) {
+ if (U_FAILURE(status)) {
+ return NULL;
+ }
+
+ int32_t len = info.length();
+ if (len == 0) {
+ return NULL; // no error;
+ }
+
+ UChar* p = (UChar*)uprv_malloc(len * sizeof(UChar));
+ if (!p) {
+ status = U_MEMORY_ALLOCATION_ERROR;
+ return NULL;
+ }
+ info.extract(p, len, status);
+ if (!U_FAILURE(status)) {
+ status = U_ZERO_ERROR; // clear warning about non-termination
+ }
+
+ LocDataParser parser(perror, status);
+ return parser.parse(p, len);
+}
+
+StringLocalizationInfo::~StringLocalizationInfo() {
+ for (UChar*** p = (UChar***)data; *p; ++p) {
+ // remaining data is simply pointer into our unicode string data.
+ if (*p) uprv_free(*p);
+ }
+ if (data) uprv_free(data);
+ if (info) uprv_free(info);
+}
+
+
+const UChar*
+StringLocalizationInfo::getRuleSetName(int32_t index) const {
+ if (index >= 0 && index < getNumberOfRuleSets()) {
+ return data[0][index];
+ }
+ return NULL;
+}
+
+const UChar*
+StringLocalizationInfo::getLocaleName(int32_t index) const {
+ if (index >= 0 && index < getNumberOfDisplayLocales()) {
+ return data[index+1][0];
+ }
+ return NULL;
+}
+
+const UChar*
+StringLocalizationInfo::getDisplayName(int32_t localeIndex, int32_t ruleIndex) const {
+ if (localeIndex >= 0 && localeIndex < getNumberOfDisplayLocales() &&
+ ruleIndex >= 0 && ruleIndex < getNumberOfRuleSets()) {
+ return data[localeIndex+1][ruleIndex+1];
+ }
+ return NULL;
+}
+
+// ----------
+
+RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description,
+ const UnicodeString& locs,
+ const Locale& alocale, UParseError& perror, UErrorCode& status)
+ : fRuleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
+ , defaultRuleSet(NULL)
+ , locale(alocale)
+ , collator(NULL)
+ , decimalFormatSymbols(NULL)
+ , defaultInfinityRule(NULL)
+ , defaultNaNRule(NULL)
+ , fRoundingMode(DecimalFormat::ERoundingMode::kRoundUnnecessary)
+ , lenient(FALSE)
+ , lenientParseRules(NULL)
+ , localizations(NULL)
+ , capitalizationInfoSet(FALSE)
+ , capitalizationForUIListMenu(FALSE)
+ , capitalizationForStandAlone(FALSE)
+ , capitalizationBrkIter(NULL)
+{
+ LocalizationInfo* locinfo = StringLocalizationInfo::create(locs, perror, status);
+ init(description, locinfo, perror, status);
+}
+
+RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description,
+ const UnicodeString& locs,
+ UParseError& perror, UErrorCode& status)
+ : fRuleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
+ , defaultRuleSet(NULL)
+ , locale(Locale::getDefault())
+ , collator(NULL)
+ , decimalFormatSymbols(NULL)
+ , defaultInfinityRule(NULL)
+ , defaultNaNRule(NULL)
+ , fRoundingMode(DecimalFormat::ERoundingMode::kRoundUnnecessary)
+ , lenient(FALSE)
+ , lenientParseRules(NULL)
+ , localizations(NULL)
+ , capitalizationInfoSet(FALSE)
+ , capitalizationForUIListMenu(FALSE)
+ , capitalizationForStandAlone(FALSE)
+ , capitalizationBrkIter(NULL)
+{
+ LocalizationInfo* locinfo = StringLocalizationInfo::create(locs, perror, status);
+ init(description, locinfo, perror, status);
+}