2001-10-10 21:43:11 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2011-04-26 06:39:29 +00:00
|
|
|
* Copyright (C) 1997-2011, International Business Machines Corporation
|
2005-03-08 22:49:59 +00:00
|
|
|
* and others. All Rights Reserved.
|
2001-10-10 21:43:11 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
#include <typeinfo> // for 'typeid' to work
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
#include "unicode/rbnf.h"
|
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
#if U_HAVE_RBNF
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
#include "unicode/normlzr.h"
|
|
|
|
#include "unicode/tblcoll.h"
|
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "unicode/ucol.h"
|
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "unicode/unum.h"
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/utf16.h"
|
2004-07-14 18:35:16 +00:00
|
|
|
#include "unicode/udata.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "nfrs.h"
|
|
|
|
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
2011-04-26 06:39:29 +00:00
|
|
|
#include "patternprops.h"
|
2008-12-12 01:04:38 +00:00
|
|
|
#include "uresimp.h"
|
2001-10-10 21:43:11 +00:00
|
|
|
|
2004-09-24 22:58:00 +00:00
|
|
|
// debugging
|
|
|
|
// #define DEBUG
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include "stdio.h"
|
|
|
|
#endif
|
|
|
|
|
2004-07-14 18:35:16 +00:00
|
|
|
#define U_ICUDATA_RBNF U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "rbnf"
|
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
static const UChar gPercentPercent[] =
|
|
|
|
{
|
|
|
|
0x25, 0x25, 0
|
|
|
|
}; /* "%%" */
|
|
|
|
|
|
|
|
// All urbnf objects are created through openRules, so we init all of the
|
|
|
|
// Unicode string constants required by rbnf, nfrs, or nfr here.
|
|
|
|
static const UChar gLenientParse[] =
|
|
|
|
{
|
|
|
|
0x25, 0x25, 0x6C, 0x65, 0x6E, 0x69, 0x65, 0x6E, 0x74, 0x2D, 0x70, 0x61, 0x72, 0x73, 0x65, 0x3A, 0
|
|
|
|
}; /* "%%lenient-parse:" */
|
|
|
|
static const UChar gSemiColon = 0x003B;
|
|
|
|
static const UChar gSemiPercent[] =
|
|
|
|
{
|
|
|
|
0x3B, 0x25, 0
|
|
|
|
}; /* ";%" */
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
#define kSomeNumberOfBitsDiv2 22
|
|
|
|
#define kHalfMaxDouble (double)(1 << kSomeNumberOfBitsDiv2)
|
|
|
|
#define kMaxDouble (kHalfMaxDouble * kHalfMaxDouble)
|
|
|
|
|
2009-04-30 07:24:02 +00:00
|
|
|
// Temporary workaround - when noParse is true, do noting in parse.
|
|
|
|
// TODO: We need a real fix - see #6895/#6896
|
2009-05-01 20:30:31 +00:00
|
|
|
static const char *NO_SPELLOUT_PARSE_LANGUAGES[] = { "ga", NULL };
|
2009-04-30 07:24:02 +00:00
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedNumberFormat)
|
2001-10-10 21:43:11 +00:00
|
|
|
|
2005-01-05 06:58:55 +00:00
|
|
|
/*
|
|
|
|
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 {
|
2004-09-24 22:58:00 +00:00
|
|
|
protected:
|
2011-07-26 05:32:25 +00:00
|
|
|
virtual ~LocalizationInfo();
|
2004-09-24 22:58:00 +00:00
|
|
|
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;
|
|
|
|
|
2005-01-05 06:58:55 +00:00
|
|
|
// virtual UClassID getDynamicClassID() const = 0;
|
|
|
|
// static UClassID getStaticClassID(void);
|
2004-09-24 22:58:00 +00:00
|
|
|
};
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
LocalizationInfo::~LocalizationInfo() {}
|
|
|
|
|
2005-01-05 06:58:55 +00:00
|
|
|
//UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(LocalizationInfo)
|
2004-09-24 22:58:00 +00:00
|
|
|
|
|
|
|
// if both strings are NULL, this returns TRUE
|
2004-10-19 03:57:56 +00:00
|
|
|
static UBool
|
2004-09-24 22:58:00 +00:00
|
|
|
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) {}
|
|
|
|
|
2004-11-14 05:11:59 +00:00
|
|
|
~VArray() {
|
2004-09-24 22:58:00 +00:00
|
|
|
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;
|
|
|
|
}
|
2004-10-11 21:59:44 +00:00
|
|
|
void* start = &buf[size];
|
|
|
|
size_t count = (cap - size) * sizeof(void*);
|
|
|
|
uprv_memset(start, 0, count); // fill with nulls, just because
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2005-01-05 06:58:55 +00:00
|
|
|
// virtual UClassID getDynamicClassID() const;
|
|
|
|
// static UClassID getStaticClassID(void);
|
2004-09-24 22:58:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void init(UErrorCode& status) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-10-19 03:57:56 +00:00
|
|
|
enum {
|
|
|
|
OPEN_ANGLE = 0x003c, /* '<' */
|
|
|
|
CLOSE_ANGLE = 0x003e, /* '>' */
|
|
|
|
COMMA = 0x002c,
|
|
|
|
TICK = 0x0027,
|
|
|
|
QUOTE = 0x0022,
|
|
|
|
SPACE = 0x0020
|
|
|
|
};
|
|
|
|
|
2004-09-24 22:58:00 +00:00
|
|
|
/**
|
|
|
|
* 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:
|
|
|
|
|
|
|
|
void inc(void) { ++p; ch = 0xffff; }
|
|
|
|
UBool checkInc(UChar c) { if (p < e && (ch == c || *p == c)) { inc(); return TRUE; } return FALSE; }
|
|
|
|
UBool check(UChar c) { return p < e && (ch == c || *p == c); }
|
2011-04-26 06:39:29 +00:00
|
|
|
void skipWhitespace(void) { while (p < e && PatternProps::isWhiteSpace(ch != 0xffff ? ch : *p)) inc();}
|
2004-09-24 22:58:00 +00:00
|
|
|
UBool inList(UChar c, const UChar* list) const {
|
2011-04-26 06:39:29 +00:00
|
|
|
if (*list == SPACE && PatternProps::isWhiteSpace(c)) return TRUE;
|
2004-09-24 22:58:00 +00:00
|
|
|
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 DEBUG
|
|
|
|
#define ERROR(msg) parseError(msg); return NULL;
|
|
|
|
#else
|
|
|
|
#define ERROR(msg) parseError(NULL); return NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-10-19 03:57:56 +00:00
|
|
|
static const UChar DQUOTE_STOPLIST[] = {
|
|
|
|
QUOTE, 0
|
2004-09-24 22:58:00 +00:00
|
|
|
};
|
|
|
|
|
2004-10-19 03:57:56 +00:00
|
|
|
static const UChar SQUOTE_STOPLIST[] = {
|
|
|
|
TICK, 0
|
2004-09-24 22:58:00 +00:00
|
|
|
};
|
|
|
|
|
2004-10-19 03:57:56 +00:00
|
|
|
static const UChar NOQUOTE_STOPLIST[] = {
|
|
|
|
SPACE, COMMA, CLOSE_ANGLE, OPEN_ANGLE, TICK, QUOTE, 0
|
2004-09-24 22:58:00 +00:00
|
|
|
};
|
|
|
|
|
2004-10-19 03:57:56 +00:00
|
|
|
static void
|
2004-09-24 22:58:00 +00:00
|
|
|
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
|
2004-11-05 05:12:29 +00:00
|
|
|
LocDataParser::parseError(const char* /*str*/) {
|
2004-09-24 22:58:00 +00:00
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UChar* start = p - U_PARSE_CONTEXT_LEN - 1;
|
2004-12-08 23:14:49 +00:00
|
|
|
if (start < data) {
|
2004-11-05 05:12:29 +00:00
|
|
|
start = data;
|
2004-12-08 23:14:49 +00:00
|
|
|
}
|
|
|
|
for (UChar* x = p; --x >= start;) {
|
2004-11-05 05:12:29 +00:00
|
|
|
if (!*x) {
|
|
|
|
start = x+1;
|
|
|
|
break;
|
|
|
|
}
|
2004-12-08 23:14:49 +00:00
|
|
|
}
|
2004-09-24 22:58:00 +00:00
|
|
|
const UChar* limit = p + U_PARSE_CONTEXT_LEN - 1;
|
2004-12-08 23:14:49 +00:00
|
|
|
if (limit > e) {
|
2004-11-05 05:12:29 +00:00
|
|
|
limit = e;
|
2004-12-08 23:14:49 +00:00
|
|
|
}
|
|
|
|
u_strncpy(pe.preContext, start, (int32_t)(p-start));
|
2004-09-24 22:58:00 +00:00
|
|
|
pe.preContext[p-start] = 0;
|
2004-12-08 23:14:49 +00:00
|
|
|
u_strncpy(pe.postContext, p, (int32_t)(limit-p));
|
2004-09-24 22:58:00 +00:00
|
|
|
pe.postContext[limit-p] = 0;
|
2004-12-08 23:14:49 +00:00
|
|
|
pe.offset = (int32_t)(p - data);
|
2004-09-24 22:58:00 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "%s at or near character %d: ", str, p-data);
|
|
|
|
|
|
|
|
UnicodeString msg;
|
|
|
|
msg.append(start, p - start);
|
|
|
|
msg.append((UChar)0x002f); /* SOLIDUS/SLASH */
|
|
|
|
msg.append(p, limit-p);
|
|
|
|
msg.append("'");
|
|
|
|
|
|
|
|
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;
|
2004-10-11 21:59:44 +00:00
|
|
|
p = NULL;
|
|
|
|
e = NULL;
|
2004-09-24 22:58:00 +00:00
|
|
|
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
ec = U_PARSE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-05 06:58:55 +00:00
|
|
|
//UOBJECT_DEFINE_RTTI_IMPLEMENTATION(StringLocalizationInfo)
|
2004-09-24 22:58:00 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
: ruleSets(NULL)
|
|
|
|
, defaultRuleSet(NULL)
|
|
|
|
, locale(alocale)
|
|
|
|
, collator(NULL)
|
|
|
|
, decimalFormatSymbols(NULL)
|
2010-10-26 18:11:13 +00:00
|
|
|
, lenient(FALSE)
|
2004-09-24 22:58:00 +00:00
|
|
|
, lenientParseRules(NULL)
|
|
|
|
, localizations(NULL)
|
2009-04-30 18:29:33 +00:00
|
|
|
, noParse(FALSE) //TODO: to be removed after #6895
|
2004-09-24 22:58:00 +00:00
|
|
|
{
|
|
|
|
LocalizationInfo* locinfo = StringLocalizationInfo::create(locs, perror, status);
|
|
|
|
init(description, locinfo, perror, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description,
|
|
|
|
const UnicodeString& locs,
|
|
|
|
UParseError& perror, UErrorCode& status)
|
|
|
|
: ruleSets(NULL)
|
|
|
|
, defaultRuleSet(NULL)
|
|
|
|
, locale(Locale::getDefault())
|
|
|
|
, collator(NULL)
|
|
|
|
, decimalFormatSymbols(NULL)
|
2010-10-26 18:11:13 +00:00
|
|
|
, lenient(FALSE)
|
2004-09-24 22:58:00 +00:00
|
|
|
, lenientParseRules(NULL)
|
|
|
|
, localizations(NULL)
|
2009-04-30 18:29:33 +00:00
|
|
|
, noParse(FALSE) //TODO: to be removed after #6895
|
2004-09-24 22:58:00 +00:00
|
|
|
{
|
|
|
|
LocalizationInfo* locinfo = StringLocalizationInfo::create(locs, perror, status);
|
|
|
|
init(description, locinfo, perror, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description,
|
|
|
|
LocalizationInfo* info,
|
|
|
|
const Locale& alocale, UParseError& perror, UErrorCode& status)
|
2001-10-10 21:43:11 +00:00
|
|
|
: ruleSets(NULL)
|
|
|
|
, defaultRuleSet(NULL)
|
|
|
|
, locale(alocale)
|
|
|
|
, collator(NULL)
|
|
|
|
, decimalFormatSymbols(NULL)
|
2010-10-26 18:11:13 +00:00
|
|
|
, lenient(FALSE)
|
2001-10-10 21:43:11 +00:00
|
|
|
, lenientParseRules(NULL)
|
2004-09-24 22:58:00 +00:00
|
|
|
, localizations(NULL)
|
2009-04-30 18:29:33 +00:00
|
|
|
, noParse(FALSE) //TODO: to be removed after #6895
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2004-09-24 22:58:00 +00:00
|
|
|
init(description, info, perror, status);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2004-10-06 17:38:10 +00:00
|
|
|
RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description,
|
2004-11-11 23:34:58 +00:00
|
|
|
UParseError& perror,
|
|
|
|
UErrorCode& status)
|
2004-10-06 17:38:10 +00:00
|
|
|
: ruleSets(NULL)
|
|
|
|
, defaultRuleSet(NULL)
|
|
|
|
, locale(Locale::getDefault())
|
|
|
|
, collator(NULL)
|
|
|
|
, decimalFormatSymbols(NULL)
|
2010-10-26 18:11:13 +00:00
|
|
|
, lenient(FALSE)
|
2004-10-06 17:38:10 +00:00
|
|
|
, lenientParseRules(NULL)
|
|
|
|
, localizations(NULL)
|
2009-04-30 18:29:33 +00:00
|
|
|
, noParse(FALSE) //TODO: to be removed after #6895
|
2004-10-06 17:38:10 +00:00
|
|
|
{
|
|
|
|
init(description, NULL, perror, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedNumberFormat::RuleBasedNumberFormat(const UnicodeString& description,
|
2004-11-11 23:34:58 +00:00
|
|
|
const Locale& aLocale,
|
|
|
|
UParseError& perror,
|
|
|
|
UErrorCode& status)
|
2004-10-06 17:38:10 +00:00
|
|
|
: ruleSets(NULL)
|
|
|
|
, defaultRuleSet(NULL)
|
|
|
|
, locale(aLocale)
|
|
|
|
, collator(NULL)
|
|
|
|
, decimalFormatSymbols(NULL)
|
2010-10-26 18:11:13 +00:00
|
|
|
, lenient(FALSE)
|
2004-10-06 17:38:10 +00:00
|
|
|
, lenientParseRules(NULL)
|
|
|
|
, localizations(NULL)
|
2009-04-30 18:29:33 +00:00
|
|
|
, noParse(FALSE) //TODO: to be removed after #6895
|
2004-10-06 17:38:10 +00:00
|
|
|
{
|
|
|
|
init(description, NULL, perror, status);
|
|
|
|
}
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& alocale, UErrorCode& status)
|
|
|
|
: ruleSets(NULL)
|
|
|
|
, defaultRuleSet(NULL)
|
|
|
|
, locale(alocale)
|
|
|
|
, collator(NULL)
|
|
|
|
, decimalFormatSymbols(NULL)
|
2010-10-26 18:11:13 +00:00
|
|
|
, lenient(FALSE)
|
2001-10-10 21:43:11 +00:00
|
|
|
, lenientParseRules(NULL)
|
2004-09-24 22:58:00 +00:00
|
|
|
, localizations(NULL)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2008-12-12 01:04:38 +00:00
|
|
|
const char* rules_tag = "RBNFRules";
|
2001-10-19 16:24:26 +00:00
|
|
|
const char* fmt_tag = "";
|
|
|
|
switch (tag) {
|
|
|
|
case URBNF_SPELLOUT: fmt_tag = "SpelloutRules"; break;
|
|
|
|
case URBNF_ORDINAL: fmt_tag = "OrdinalRules"; break;
|
|
|
|
case URBNF_DURATION: fmt_tag = "DurationRules"; break;
|
2009-01-22 04:46:35 +00:00
|
|
|
case URBNF_NUMBERING_SYSTEM: fmt_tag = "NumberingSystemRules"; break;
|
2001-10-19 16:24:26 +00:00
|
|
|
default: status = U_ILLEGAL_ARGUMENT_ERROR; return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2004-09-24 22:58:00 +00:00
|
|
|
// TODO: read localization info from resource
|
|
|
|
LocalizationInfo* locinfo = NULL;
|
|
|
|
|
2004-07-14 18:35:16 +00:00
|
|
|
UResourceBundle* nfrb = ures_open(U_ICUDATA_RBNF, locale.getName(), &status);
|
2001-10-19 16:24:26 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2004-01-20 23:06:38 +00:00
|
|
|
setLocaleIDs(ures_getLocaleByType(nfrb, ULOC_VALID_LOCALE, &status),
|
|
|
|
ures_getLocaleByType(nfrb, ULOC_ACTUAL_LOCALE, &status));
|
2008-12-12 01:04:38 +00:00
|
|
|
|
|
|
|
UResourceBundle* rbnfRules = ures_getByKeyWithFallback(nfrb, rules_tag, NULL, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
ures_close(nfrb);
|
|
|
|
}
|
|
|
|
UResourceBundle* ruleSets = ures_getByKeyWithFallback(rbnfRules, fmt_tag, NULL, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
ures_close(rbnfRules);
|
|
|
|
ures_close(nfrb);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-07 18:46:19 +00:00
|
|
|
|
2008-12-12 01:04:38 +00:00
|
|
|
UnicodeString desc;
|
|
|
|
while (ures_hasNext(ruleSets)) {
|
2011-07-07 18:46:19 +00:00
|
|
|
desc.append(ures_getNextUnicodeString(ruleSets,NULL,&status));
|
2008-12-12 01:04:38 +00:00
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
UParseError perror;
|
2008-12-12 01:04:38 +00:00
|
|
|
|
2004-09-24 22:58:00 +00:00
|
|
|
init (desc, locinfo, perror, status);
|
2008-12-12 01:04:38 +00:00
|
|
|
|
2009-04-30 07:24:02 +00:00
|
|
|
//TODO: we need a real fix - see #6895 / #6896
|
|
|
|
noParse = FALSE;
|
|
|
|
if (tag == URBNF_SPELLOUT) {
|
|
|
|
const char *lang = alocale.getLanguage();
|
|
|
|
for (int32_t i = 0; NO_SPELLOUT_PARSE_LANGUAGES[i] != NULL; i++) {
|
|
|
|
if (uprv_strcmp(lang, NO_SPELLOUT_PARSE_LANGUAGES[i]) == 0) {
|
|
|
|
noParse = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO: end
|
|
|
|
|
2008-12-12 01:04:38 +00:00
|
|
|
ures_close(ruleSets);
|
|
|
|
ures_close(rbnfRules);
|
2001-10-19 16:24:26 +00:00
|
|
|
}
|
|
|
|
ures_close(nfrb);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedNumberFormat::RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs)
|
2001-10-19 16:24:26 +00:00
|
|
|
: NumberFormat(rhs)
|
|
|
|
, ruleSets(NULL)
|
2001-10-10 21:43:11 +00:00
|
|
|
, defaultRuleSet(NULL)
|
|
|
|
, locale(rhs.locale)
|
|
|
|
, collator(NULL)
|
|
|
|
, decimalFormatSymbols(NULL)
|
2010-10-26 18:11:13 +00:00
|
|
|
, lenient(FALSE)
|
2001-10-10 21:43:11 +00:00
|
|
|
, lenientParseRules(NULL)
|
2004-09-24 22:58:00 +00:00
|
|
|
, localizations(NULL)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
this->operator=(rhs);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2004-10-06 17:38:10 +00:00
|
|
|
// --------
|
2004-09-24 22:58:00 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
RuleBasedNumberFormat&
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::operator=(const RuleBasedNumberFormat& rhs)
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
dispose();
|
|
|
|
locale = rhs.locale;
|
2010-10-26 18:11:13 +00:00
|
|
|
lenient = rhs.lenient;
|
2004-09-24 22:58:00 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
UnicodeString rules = rhs.getRules();
|
|
|
|
UParseError perror;
|
2004-09-24 22:58:00 +00:00
|
|
|
init(rules, rhs.localizations ? rhs.localizations->ref() : NULL, perror, status);
|
|
|
|
|
2009-04-30 07:24:02 +00:00
|
|
|
//TODO: remove below when we fix the parse bug - See #6895 / #6896
|
|
|
|
noParse = rhs.noParse;
|
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
return *this;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RuleBasedNumberFormat::~RuleBasedNumberFormat()
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
dispose();
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
Format*
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::clone(void) const
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
RuleBasedNumberFormat * result = NULL;
|
|
|
|
UnicodeString rules = getRules();
|
2001-11-28 00:58:33 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2002-02-28 01:42:40 +00:00
|
|
|
UParseError perror;
|
2004-09-24 22:58:00 +00:00
|
|
|
result = new RuleBasedNumberFormat(rules, localizations, locale, perror, status);
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (result == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete result;
|
2002-06-29 09:31:05 +00:00
|
|
|
result = 0;
|
2002-02-28 01:42:40 +00:00
|
|
|
} else {
|
2010-10-26 18:11:13 +00:00
|
|
|
result->lenient = lenient;
|
2009-04-30 17:40:52 +00:00
|
|
|
|
|
|
|
//TODO: remove below when we fix the parse bug - See #6895 / #6896
|
|
|
|
result->noParse = noParse;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
return result;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UBool
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::operator==(const Format& other) const
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
if (this == &other) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
if (typeid(*this) == typeid(other)) {
|
2001-10-19 16:24:26 +00:00
|
|
|
const RuleBasedNumberFormat& rhs = (const RuleBasedNumberFormat&)other;
|
|
|
|
if (locale == rhs.locale &&
|
2010-10-26 18:11:13 +00:00
|
|
|
lenient == rhs.lenient &&
|
2004-09-24 22:58:00 +00:00
|
|
|
(localizations == NULL
|
|
|
|
? rhs.localizations == NULL
|
|
|
|
: (rhs.localizations == NULL
|
|
|
|
? FALSE
|
|
|
|
: *localizations == rhs.localizations))) {
|
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
NFRuleSet** p = ruleSets;
|
|
|
|
NFRuleSet** q = rhs.ruleSets;
|
2003-05-15 20:06:06 +00:00
|
|
|
if (p == NULL) {
|
2004-09-24 22:58:00 +00:00
|
|
|
return q == NULL;
|
|
|
|
} else if (q == NULL) {
|
2003-05-16 00:36:09 +00:00
|
|
|
return FALSE;
|
2003-05-15 20:06:06 +00:00
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
while (*p && *q && (**p == **q)) {
|
|
|
|
++p;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
return *q == NULL && *p == NULL;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
return FALSE;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::getRules() const
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
UnicodeString result;
|
2003-05-15 20:06:06 +00:00
|
|
|
if (ruleSets != NULL) {
|
|
|
|
for (NFRuleSet** p = ruleSets; *p; ++p) {
|
|
|
|
(*p)->appendRules(result);
|
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
}
|
|
|
|
return result;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::getRuleSetName(int32_t index) const
|
|
|
|
{
|
2004-09-24 22:58:00 +00:00
|
|
|
if (localizations) {
|
|
|
|
UnicodeString string(TRUE, localizations->getRuleSetName(index), (int32_t)-1);
|
|
|
|
return string;
|
|
|
|
} else if (ruleSets) {
|
|
|
|
UnicodeString result;
|
2003-05-15 20:06:06 +00:00
|
|
|
for (NFRuleSet** p = ruleSets; *p; ++p) {
|
|
|
|
NFRuleSet* rs = *p;
|
|
|
|
if (rs->isPublic()) {
|
|
|
|
if (--index == -1) {
|
|
|
|
rs->getName(result);
|
|
|
|
return result;
|
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2004-09-24 22:58:00 +00:00
|
|
|
UnicodeString empty;
|
|
|
|
return empty;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2002-02-28 01:42:40 +00:00
|
|
|
RuleBasedNumberFormat::getNumberOfRuleSetNames() const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
int32_t result = 0;
|
2004-09-24 22:58:00 +00:00
|
|
|
if (localizations) {
|
|
|
|
result = localizations->getNumberOfRuleSets();
|
|
|
|
} else if (ruleSets) {
|
2003-05-15 20:06:06 +00:00
|
|
|
for (NFRuleSet** p = ruleSets; *p; ++p) {
|
|
|
|
if ((**p).isPublic()) {
|
|
|
|
++result;
|
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2004-09-24 22:58:00 +00:00
|
|
|
int32_t
|
|
|
|
RuleBasedNumberFormat::getNumberOfRuleSetDisplayNameLocales(void) const {
|
|
|
|
if (localizations) {
|
|
|
|
return localizations->getNumberOfDisplayLocales();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Locale
|
|
|
|
RuleBasedNumberFormat::getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const {
|
2004-11-14 05:11:59 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2004-11-16 22:42:08 +00:00
|
|
|
return Locale("");
|
2004-11-14 05:11:59 +00:00
|
|
|
}
|
2004-09-24 22:58:00 +00:00
|
|
|
if (localizations && index >= 0 && index < localizations->getNumberOfDisplayLocales()) {
|
|
|
|
UnicodeString name(TRUE, localizations->getLocaleName(index), -1);
|
|
|
|
char buffer[64];
|
|
|
|
int32_t cap = name.length() + 1;
|
|
|
|
char* bp = buffer;
|
|
|
|
if (cap > 64) {
|
2004-11-14 05:11:59 +00:00
|
|
|
bp = (char *)uprv_malloc(cap);
|
|
|
|
if (bp == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2004-11-16 22:42:08 +00:00
|
|
|
return Locale("");
|
2004-11-14 05:11:59 +00:00
|
|
|
}
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
2004-10-27 23:40:36 +00:00
|
|
|
name.extract(0, name.length(), bp, cap, UnicodeString::kInvariant);
|
2004-09-30 23:25:20 +00:00
|
|
|
Locale retLocale(bp);
|
2004-09-24 22:58:00 +00:00
|
|
|
if (bp != buffer) {
|
2004-11-14 05:11:59 +00:00
|
|
|
uprv_free(bp);
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
2004-09-30 23:25:20 +00:00
|
|
|
return retLocale;
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2004-09-30 23:25:20 +00:00
|
|
|
Locale retLocale;
|
|
|
|
return retLocale;
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
2004-09-30 23:25:20 +00:00
|
|
|
RuleBasedNumberFormat::getRuleSetDisplayName(int32_t index, const Locale& localeParam) {
|
2004-09-24 22:58:00 +00:00
|
|
|
if (localizations && index >= 0 && index < localizations->getNumberOfRuleSets()) {
|
2004-09-30 23:25:20 +00:00
|
|
|
UnicodeString localeName(localeParam.getBaseName(), -1, UnicodeString::kInvariant);
|
2004-09-24 22:58:00 +00:00
|
|
|
int32_t len = localeName.length();
|
2004-09-30 23:25:20 +00:00
|
|
|
UChar* localeStr = localeName.getBuffer(len + 1);
|
2004-09-24 22:58:00 +00:00
|
|
|
while (len >= 0) {
|
2004-09-30 23:25:20 +00:00
|
|
|
localeStr[len] = 0;
|
|
|
|
int32_t ix = localizations->indexForLocale(localeStr);
|
2004-09-24 22:58:00 +00:00
|
|
|
if (ix >= 0) {
|
|
|
|
UnicodeString name(TRUE, localizations->getDisplayName(ix, index), -1);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim trailing portion, skipping over ommitted sections
|
2004-09-30 23:25:20 +00:00
|
|
|
do { --len;} while (len > 0 && localeStr[len] != 0x005f); // underscore
|
|
|
|
while (len > 0 && localeStr[len-1] == 0x005F) --len;
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
|
|
|
UnicodeString name(TRUE, localizations->getRuleSetName(index), -1);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
UnicodeString bogus;
|
|
|
|
bogus.setToBogus();
|
|
|
|
return bogus;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString
|
2004-09-30 23:25:20 +00:00
|
|
|
RuleBasedNumberFormat::getRuleSetDisplayName(const UnicodeString& ruleSetName, const Locale& localeParam) {
|
2004-09-24 22:58:00 +00:00
|
|
|
if (localizations) {
|
|
|
|
UnicodeString rsn(ruleSetName);
|
|
|
|
int32_t ix = localizations->indexForRuleSet(rsn.getTerminatedBuffer());
|
2004-09-30 23:25:20 +00:00
|
|
|
return getRuleSetDisplayName(ix, localeParam);
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
|
|
|
UnicodeString bogus;
|
|
|
|
bogus.setToBogus();
|
|
|
|
return bogus;
|
|
|
|
}
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
NFRuleSet*
|
|
|
|
RuleBasedNumberFormat::findRuleSet(const UnicodeString& name, UErrorCode& status) const
|
|
|
|
{
|
2003-05-15 20:06:06 +00:00
|
|
|
if (U_SUCCESS(status) && ruleSets) {
|
2001-10-19 16:24:26 +00:00
|
|
|
for (NFRuleSet** p = ruleSets; *p; ++p) {
|
|
|
|
NFRuleSet* rs = *p;
|
|
|
|
if (rs->isNamed(name)) {
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
return NULL;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString&
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::format(int32_t number,
|
|
|
|
UnicodeString& toAppendTo,
|
2003-12-02 01:55:53 +00:00
|
|
|
FieldPosition& /* pos */) const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2003-05-15 20:06:06 +00:00
|
|
|
if (defaultRuleSet) defaultRuleSet->format((int64_t)number, toAppendTo, toAppendTo.length());
|
2001-10-19 16:24:26 +00:00
|
|
|
return toAppendTo;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString&
|
2002-03-14 00:28:22 +00:00
|
|
|
RuleBasedNumberFormat::format(int64_t number,
|
2001-10-10 21:43:11 +00:00
|
|
|
UnicodeString& toAppendTo,
|
2003-12-02 01:55:53 +00:00
|
|
|
FieldPosition& /* pos */) const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2003-05-15 20:06:06 +00:00
|
|
|
if (defaultRuleSet) defaultRuleSet->format(number, toAppendTo, toAppendTo.length());
|
2001-10-19 16:24:26 +00:00
|
|
|
return toAppendTo;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-03-14 00:28:22 +00:00
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString&
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::format(double number,
|
|
|
|
UnicodeString& toAppendTo,
|
2003-12-02 01:55:53 +00:00
|
|
|
FieldPosition& /* pos */) const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2011-06-30 05:43:56 +00:00
|
|
|
// Special case for NaN; adapted from what DecimalFormat::_format( double number,...) does.
|
|
|
|
if (uprv_isNaN(number)) {
|
|
|
|
DecimalFormatSymbols* decFmtSyms = getDecimalFormatSymbols(); // RuleBasedNumberFormat internal
|
|
|
|
if (decFmtSyms) {
|
|
|
|
toAppendTo += decFmtSyms->getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
|
|
|
|
}
|
|
|
|
} else if (defaultRuleSet) {
|
|
|
|
defaultRuleSet->format(number, toAppendTo, toAppendTo.length());
|
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
return toAppendTo;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString&
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::format(int32_t number,
|
|
|
|
const UnicodeString& ruleSetName,
|
|
|
|
UnicodeString& toAppendTo,
|
2003-12-02 01:55:53 +00:00
|
|
|
FieldPosition& /* pos */,
|
2001-10-10 21:43:11 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2002-03-14 00:28:22 +00:00
|
|
|
// return format((int64_t)number, ruleSetName, toAppendTo, pos, status);
|
2001-10-19 16:24:26 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2011-07-07 18:46:19 +00:00
|
|
|
if (ruleSetName.indexOf(gPercentPercent, 2, 0) == 0) {
|
2001-10-19 16:24:26 +00:00
|
|
|
// throw new IllegalArgumentException("Can't use internal rule set");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
|
|
|
NFRuleSet *rs = findRuleSet(ruleSetName, status);
|
|
|
|
if (rs) {
|
2002-03-14 00:28:22 +00:00
|
|
|
rs->format((int64_t)number, toAppendTo, toAppendTo.length());
|
2001-10-19 16:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toAppendTo;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString&
|
2002-03-14 00:28:22 +00:00
|
|
|
RuleBasedNumberFormat::format(int64_t number,
|
2001-10-10 21:43:11 +00:00
|
|
|
const UnicodeString& ruleSetName,
|
|
|
|
UnicodeString& toAppendTo,
|
2003-12-02 01:55:53 +00:00
|
|
|
FieldPosition& /* pos */,
|
2001-10-10 21:43:11 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2011-07-07 18:46:19 +00:00
|
|
|
if (ruleSetName.indexOf(gPercentPercent, 2, 0) == 0) {
|
2001-10-19 16:24:26 +00:00
|
|
|
// throw new IllegalArgumentException("Can't use internal rule set");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
|
|
|
NFRuleSet *rs = findRuleSet(ruleSetName, status);
|
|
|
|
if (rs) {
|
|
|
|
rs->format(number, toAppendTo, toAppendTo.length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toAppendTo;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-03-14 00:28:22 +00:00
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
// make linker happy
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString&
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::format(const Formattable& obj,
|
|
|
|
UnicodeString& toAppendTo,
|
|
|
|
FieldPosition& pos,
|
2002-02-28 01:42:40 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
return NumberFormat::format(obj, toAppendTo, pos, status);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
UnicodeString&
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::format(double number,
|
|
|
|
const UnicodeString& ruleSetName,
|
|
|
|
UnicodeString& toAppendTo,
|
2003-12-02 01:55:53 +00:00
|
|
|
FieldPosition& /* pos */,
|
2001-10-10 21:43:11 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2011-07-07 18:46:19 +00:00
|
|
|
if (ruleSetName.indexOf(gPercentPercent, 2, 0) == 0) {
|
2001-10-19 16:24:26 +00:00
|
|
|
// throw new IllegalArgumentException("Can't use internal rule set");
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
|
|
|
NFRuleSet *rs = findRuleSet(ruleSetName, status);
|
|
|
|
if (rs) {
|
|
|
|
rs->format(number, toAppendTo, toAppendTo.length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toAppendTo;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
void
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::parse(const UnicodeString& text,
|
2001-10-19 16:24:26 +00:00
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& parsePosition) const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2009-04-30 07:24:02 +00:00
|
|
|
//TODO: We need a real fix. See #6895 / #6896
|
|
|
|
if (noParse) {
|
|
|
|
// skip parsing
|
|
|
|
parsePosition.setErrorIndex(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-15 20:06:06 +00:00
|
|
|
if (!ruleSets) {
|
|
|
|
parsePosition.setErrorIndex(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-04 05:58:41 +00:00
|
|
|
UnicodeString workingText(text, parsePosition.getIndex());
|
|
|
|
ParsePosition workingPos(0);
|
|
|
|
|
|
|
|
ParsePosition high_pp(0);
|
2001-10-19 16:24:26 +00:00
|
|
|
Formattable high_result;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
for (NFRuleSet** p = ruleSets; *p; ++p) {
|
|
|
|
NFRuleSet *rp = *p;
|
2009-05-01 20:30:31 +00:00
|
|
|
if (rp->isPublic() && rp->isParseable()) {
|
2004-08-04 05:58:41 +00:00
|
|
|
ParsePosition working_pp(0);
|
2001-10-19 16:24:26 +00:00
|
|
|
Formattable working_result;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2004-08-04 05:58:41 +00:00
|
|
|
rp->parse(workingText, working_pp, kMaxDouble, working_result);
|
2001-10-19 16:24:26 +00:00
|
|
|
if (working_pp.getIndex() > high_pp.getIndex()) {
|
|
|
|
high_pp = working_pp;
|
|
|
|
high_result = working_result;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2004-08-04 05:58:41 +00:00
|
|
|
if (high_pp.getIndex() == workingText.length()) {
|
2001-10-19 16:24:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2009-02-19 08:41:08 +00:00
|
|
|
int32_t startIndex = parsePosition.getIndex();
|
|
|
|
parsePosition.setIndex(startIndex + high_pp.getIndex());
|
2004-08-04 05:58:41 +00:00
|
|
|
if (high_pp.getIndex() > 0) {
|
|
|
|
parsePosition.setErrorIndex(-1);
|
2009-02-19 08:41:08 +00:00
|
|
|
} else {
|
|
|
|
int32_t errorIndex = (high_pp.getErrorIndex()>0)? high_pp.getErrorIndex(): 0;
|
|
|
|
parsePosition.setErrorIndex(startIndex + errorIndex);
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
result = high_result;
|
|
|
|
if (result.getType() == Formattable::kDouble) {
|
|
|
|
int32_t r = (int32_t)result.getDouble();
|
|
|
|
if ((double)r == result.getDouble()) {
|
|
|
|
result.setLong(r);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#if !UCONFIG_NO_COLLATION
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
void
|
|
|
|
RuleBasedNumberFormat::setLenient(UBool enabled)
|
|
|
|
{
|
2010-10-26 18:11:13 +00:00
|
|
|
lenient = enabled;
|
2001-10-19 16:24:26 +00:00
|
|
|
if (!enabled && collator) {
|
|
|
|
delete collator;
|
|
|
|
collator = NULL;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif
|
|
|
|
|
2002-07-12 20:37:14 +00:00
|
|
|
void
|
|
|
|
RuleBasedNumberFormat::setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status) {
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if (ruleSetName.isEmpty()) {
|
2004-09-24 22:58:00 +00:00
|
|
|
if (localizations) {
|
|
|
|
UnicodeString name(TRUE, localizations->getRuleSetName(0), -1);
|
|
|
|
defaultRuleSet = findRuleSet(name, status);
|
|
|
|
} else {
|
2002-07-12 20:37:14 +00:00
|
|
|
initDefaultRuleSet();
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
2004-09-15 23:44:48 +00:00
|
|
|
} else if (ruleSetName.startsWith(UNICODE_STRING_SIMPLE("%%"))) {
|
2002-07-12 20:37:14 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
2003-05-15 20:06:06 +00:00
|
|
|
NFRuleSet* result = findRuleSet(ruleSetName, status);
|
|
|
|
if (result != NULL) {
|
|
|
|
defaultRuleSet = result;
|
|
|
|
}
|
2002-07-12 20:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
UnicodeString
|
2004-03-30 00:43:40 +00:00
|
|
|
RuleBasedNumberFormat::getDefaultRuleSetName() const {
|
2004-03-12 20:15:13 +00:00
|
|
|
UnicodeString result;
|
|
|
|
if (defaultRuleSet && defaultRuleSet->isPublic()) {
|
|
|
|
defaultRuleSet->getName(result);
|
|
|
|
} else {
|
|
|
|
result.setToBogus();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-07-12 20:37:14 +00:00
|
|
|
void
|
|
|
|
RuleBasedNumberFormat::initDefaultRuleSet()
|
|
|
|
{
|
2004-09-24 22:58:00 +00:00
|
|
|
defaultRuleSet = NULL;
|
2003-05-15 20:06:06 +00:00
|
|
|
if (!ruleSets) {
|
2004-09-24 22:58:00 +00:00
|
|
|
return;
|
2003-05-15 20:06:06 +00:00
|
|
|
}
|
2004-09-24 22:58:00 +00:00
|
|
|
|
2009-03-30 15:00:49 +00:00
|
|
|
const UnicodeString spellout = UNICODE_STRING_SIMPLE("%spellout-numbering");
|
|
|
|
const UnicodeString ordinal = UNICODE_STRING_SIMPLE("%digits-ordinal");
|
2008-12-12 01:04:38 +00:00
|
|
|
const UnicodeString duration = UNICODE_STRING_SIMPLE("%duration");
|
|
|
|
|
2004-09-24 22:58:00 +00:00
|
|
|
NFRuleSet**p = &ruleSets[0];
|
2003-05-15 20:06:06 +00:00
|
|
|
while (*p) {
|
2008-12-12 01:04:38 +00:00
|
|
|
if ((*p)->isNamed(spellout) || (*p)->isNamed(ordinal) || (*p)->isNamed(duration)) {
|
|
|
|
defaultRuleSet = *p;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
++p;
|
|
|
|
}
|
2003-05-15 20:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultRuleSet = *--p;
|
|
|
|
if (!defaultRuleSet->isPublic()) {
|
|
|
|
while (p != ruleSets) {
|
|
|
|
if ((*--p)->isPublic()) {
|
|
|
|
defaultRuleSet = *p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-07-12 20:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
void
|
2004-11-04 06:41:39 +00:00
|
|
|
RuleBasedNumberFormat::init(const UnicodeString& rules, LocalizationInfo* localizationInfos,
|
2006-04-17 17:01:52 +00:00
|
|
|
UParseError& pErr, UErrorCode& status)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
// TODO: implement UParseError
|
2006-04-17 17:01:52 +00:00
|
|
|
uprv_memset(&pErr, 0, sizeof(UParseError));
|
2003-05-15 20:06:06 +00:00
|
|
|
// Note: this can leave ruleSets == NULL, so remaining code should check
|
2001-10-19 16:24:26 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2004-11-04 06:41:39 +00:00
|
|
|
this->localizations = localizationInfos == NULL ? NULL : localizationInfos->ref();
|
2004-09-24 22:58:00 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
UnicodeString description(rules);
|
|
|
|
if (!description.length()) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// start by stripping the trailing whitespace from all the rules
|
|
|
|
// (this is all the whitespace follwing each semicolon in the
|
|
|
|
// description). This allows us to look for rule-set boundaries
|
|
|
|
// by searching for ";%" without having to worry about whitespace
|
|
|
|
// between the ; and the %
|
|
|
|
stripWhitespace(description);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// check to see if there's a set of lenient-parse rules. If there
|
|
|
|
// is, pull them out into our temporary holding place for them,
|
|
|
|
// and delete them from the description before the real desciption-
|
|
|
|
// parsing code sees them
|
2011-07-07 18:46:19 +00:00
|
|
|
int32_t lp = description.indexOf(gLenientParse, -1, 0);
|
2001-10-19 16:24:26 +00:00
|
|
|
if (lp != -1) {
|
|
|
|
// we've got to make sure we're not in the middle of a rule
|
|
|
|
// (where "%%lenient-parse" would actually get treated as
|
|
|
|
// rule text)
|
|
|
|
if (lp == 0 || description.charAt(lp - 1) == gSemiColon) {
|
|
|
|
// locate the beginning and end of the actual collation
|
|
|
|
// rules (there may be whitespace between the name and
|
|
|
|
// the first token in the description)
|
2011-07-07 18:46:19 +00:00
|
|
|
int lpEnd = description.indexOf(gSemiPercent, 2, lp);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
if (lpEnd == -1) {
|
|
|
|
lpEnd = description.length() - 1;
|
|
|
|
}
|
|
|
|
int lpStart = lp + u_strlen(gLenientParse);
|
2011-04-26 06:39:29 +00:00
|
|
|
while (PatternProps::isWhiteSpace(description.charAt(lpStart))) {
|
2001-10-19 16:24:26 +00:00
|
|
|
++lpStart;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// copy out the lenient-parse rules and delete them
|
|
|
|
// from the description
|
|
|
|
lenientParseRules = new UnicodeString();
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (lenientParseRules == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
lenientParseRules->setTo(description, lpStart, lpEnd - lpStart);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
description.remove(lp, lpEnd + 1 - lp);
|
|
|
|
}
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// pre-flight parsing the description and count the number of
|
|
|
|
// rule sets (";%" marks the end of one rule set and the beginning
|
|
|
|
// of the next)
|
|
|
|
int numRuleSets = 0;
|
2011-07-07 18:46:19 +00:00
|
|
|
for (int32_t p = description.indexOf(gSemiPercent, 2, 0); p != -1; p = description.indexOf(gSemiPercent, 2, p)) {
|
2001-10-19 16:24:26 +00:00
|
|
|
++numRuleSets;
|
|
|
|
++p;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
++numRuleSets;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// our rule list is an array of the appropriate size
|
2002-07-16 17:50:42 +00:00
|
|
|
ruleSets = (NFRuleSet **)uprv_malloc((numRuleSets + 1) * sizeof(NFRuleSet *));
|
2002-07-02 15:10:30 +00:00
|
|
|
/* test for NULL */
|
2002-06-29 09:31:05 +00:00
|
|
|
if (ruleSets == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
for (int i = 0; i <= numRuleSets; ++i) {
|
|
|
|
ruleSets[i] = NULL;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// divide up the descriptions into individual rule-set descriptions
|
|
|
|
// and store them in a temporary array. At each step, we also
|
|
|
|
// new up a rule set, but all this does is initialize its name
|
|
|
|
// and remove it from its description. We can't actually parse
|
|
|
|
// the rest of the descriptions and finish initializing everything
|
|
|
|
// because we have to know the names and locations of all the rule
|
|
|
|
// sets before we can actually set everything up
|
2003-06-09 21:21:36 +00:00
|
|
|
if(!numRuleSets) {
|
2008-02-13 09:35:50 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
2003-06-09 21:21:36 +00:00
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
UnicodeString* ruleSetDescriptions = new UnicodeString[numRuleSets];
|
2002-06-29 09:31:05 +00:00
|
|
|
if (ruleSetDescriptions == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
{
|
|
|
|
int curRuleSet = 0;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start = 0;
|
2011-07-07 18:46:19 +00:00
|
|
|
for (int32_t p = description.indexOf(gSemiPercent, 2, 0); p != -1; p = description.indexOf(gSemiPercent, 2, start)) {
|
2001-10-19 16:24:26 +00:00
|
|
|
ruleSetDescriptions[curRuleSet].setTo(description, start, p + 1 - start);
|
|
|
|
ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet, status);
|
2002-06-29 09:31:05 +00:00
|
|
|
if (ruleSets[curRuleSet] == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2008-02-13 09:35:50 +00:00
|
|
|
goto cleanup;
|
2002-06-29 09:31:05 +00:00
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
++curRuleSet;
|
|
|
|
start = p + 1;
|
|
|
|
}
|
|
|
|
ruleSetDescriptions[curRuleSet].setTo(description, start, description.length() - start);
|
|
|
|
ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet, status);
|
2002-06-29 09:31:05 +00:00
|
|
|
if (ruleSets[curRuleSet] == 0) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2008-02-13 09:35:50 +00:00
|
|
|
goto cleanup;
|
2002-06-29 09:31:05 +00:00
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// now we can take note of the formatter's default rule set, which
|
|
|
|
// is the last public rule set in the description (it's the last
|
|
|
|
// rather than the first so that a user can create a new formatter
|
|
|
|
// from an existing formatter and change its default behavior just
|
|
|
|
// by appending more rule sets to the end)
|
2004-09-24 22:58:00 +00:00
|
|
|
|
|
|
|
// {dlf} Initialization of a fraction rule set requires the default rule
|
|
|
|
// set to be known. For purposes of initialization, this is always the
|
|
|
|
// last public rule set, no matter what the localization data says.
|
|
|
|
initDefaultRuleSet();
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// finally, we can go back through the temporary descriptions
|
|
|
|
// list and finish seting up the substructure (and we throw
|
|
|
|
// away the temporary descriptions as we go)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < numRuleSets; i++) {
|
|
|
|
ruleSets[i]->parseRules(ruleSetDescriptions[i], this, status);
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2004-09-24 22:58:00 +00:00
|
|
|
// Now that the rules are initialized, the 'real' default rule
|
|
|
|
// set can be adjusted by the localization data.
|
|
|
|
|
|
|
|
// The C code keeps the localization array as is, rather than building
|
|
|
|
// a separate array of the public rule set names, so we have less work
|
|
|
|
// to do here-- but we still need to check the names.
|
|
|
|
|
2004-11-04 06:41:39 +00:00
|
|
|
if (localizationInfos) {
|
2004-09-24 22:58:00 +00:00
|
|
|
// confirm the names, if any aren't in the rules, that's an error
|
|
|
|
// it is ok if the rules contain public rule sets that are not in this list
|
2004-11-04 06:41:39 +00:00
|
|
|
for (int32_t i = 0; i < localizationInfos->getNumberOfRuleSets(); ++i) {
|
|
|
|
UnicodeString name(TRUE, localizationInfos->getRuleSetName(i), -1);
|
2004-09-24 22:58:00 +00:00
|
|
|
NFRuleSet* rs = findRuleSet(name, status);
|
|
|
|
if (rs == NULL) {
|
2004-11-04 06:41:39 +00:00
|
|
|
break; // error
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
|
|
|
if (i == 0) {
|
2004-11-04 06:41:39 +00:00
|
|
|
defaultRuleSet = rs;
|
2004-09-24 22:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defaultRuleSet = getDefaultRuleSet();
|
|
|
|
}
|
2008-02-13 09:35:50 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
delete[] ruleSetDescriptions;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-02-28 01:42:40 +00:00
|
|
|
RuleBasedNumberFormat::stripWhitespace(UnicodeString& description)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
// iterate through the characters...
|
|
|
|
UnicodeString result;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
int start = 0;
|
|
|
|
while (start != -1 && start < description.length()) {
|
|
|
|
// seek to the first non-whitespace character...
|
|
|
|
while (start < description.length()
|
2011-04-26 06:39:29 +00:00
|
|
|
&& PatternProps::isWhiteSpace(description.charAt(start))) {
|
2001-10-19 16:24:26 +00:00
|
|
|
++start;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// locate the next semicolon in the text and copy the text from
|
|
|
|
// our current position up to that semicolon into the result
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t p = description.indexOf(gSemiColon, start);
|
2001-10-19 16:24:26 +00:00
|
|
|
if (p == -1) {
|
|
|
|
// or if we don't find a semicolon, just copy the rest of
|
|
|
|
// the string into the result
|
|
|
|
result.append(description, start, description.length() - start);
|
|
|
|
start = -1;
|
|
|
|
}
|
|
|
|
else if (p < description.length()) {
|
|
|
|
result.append(description, start, p + 1 - start);
|
|
|
|
start = p + 1;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
// when we get here, we've seeked off the end of the sring, and
|
|
|
|
// we terminate the loop (we continue until *start* is -1 rather
|
|
|
|
// than until *p* is -1, because otherwise we'd miss the last
|
|
|
|
// rule in the description)
|
|
|
|
else {
|
|
|
|
start = -1;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
description.setTo(result);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
void
|
2001-10-10 21:43:11 +00:00
|
|
|
RuleBasedNumberFormat::dispose()
|
|
|
|
{
|
2001-10-19 16:24:26 +00:00
|
|
|
if (ruleSets) {
|
|
|
|
for (NFRuleSet** p = ruleSets; *p; ++p) {
|
|
|
|
delete *p;
|
|
|
|
}
|
2002-07-16 17:50:42 +00:00
|
|
|
uprv_free(ruleSets);
|
2001-10-19 16:24:26 +00:00
|
|
|
ruleSets = NULL;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#if !UCONFIG_NO_COLLATION
|
2002-02-28 01:42:40 +00:00
|
|
|
delete collator;
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
collator = NULL;
|
|
|
|
|
|
|
|
delete decimalFormatSymbols;
|
|
|
|
decimalFormatSymbols = NULL;
|
|
|
|
|
|
|
|
delete lenientParseRules;
|
|
|
|
lenientParseRules = NULL;
|
2004-09-24 22:58:00 +00:00
|
|
|
|
|
|
|
if (localizations) localizations = localizations->unref();
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
// package-internal API
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the collator to use for lenient parsing. The collator is lazily created:
|
|
|
|
* this function creates it the first time it's called.
|
|
|
|
* @return The collator to use for lenient parsing, or null if lenient parsing
|
|
|
|
* is turned off.
|
|
|
|
*/
|
|
|
|
Collator*
|
|
|
|
RuleBasedNumberFormat::getCollator() const
|
|
|
|
{
|
2002-09-20 01:54:48 +00:00
|
|
|
#if !UCONFIG_NO_COLLATION
|
2003-05-15 20:06:06 +00:00
|
|
|
if (!ruleSets) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
// lazy-evaulate the collator
|
2010-10-26 18:11:13 +00:00
|
|
|
if (collator == NULL && lenient) {
|
2001-10-19 16:24:26 +00:00
|
|
|
// create a default collator based on the formatter's locale,
|
|
|
|
// then pull out that collator's rules, append any additional
|
|
|
|
// rules specified in the description, and create a _new_
|
|
|
|
// collator based on the combinaiton of those rules
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
Collator* temp = Collator::createInstance(locale, status);
|
2010-05-19 17:29:33 +00:00
|
|
|
RuleBasedCollator* newCollator;
|
|
|
|
if (U_SUCCESS(status) && (newCollator = dynamic_cast<RuleBasedCollator*>(temp)) != NULL) {
|
2001-10-19 16:24:26 +00:00
|
|
|
if (lenientParseRules) {
|
|
|
|
UnicodeString rules(newCollator->getRules());
|
|
|
|
rules.append(*lenientParseRules);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-19 16:24:26 +00:00
|
|
|
newCollator = new RuleBasedCollator(rules, status);
|
2008-01-14 20:27:51 +00:00
|
|
|
// Exit if newCollator could not be created.
|
|
|
|
if (newCollator == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-19 16:24:26 +00:00
|
|
|
} else {
|
|
|
|
temp = NULL;
|
|
|
|
}
|
|
|
|
if (U_SUCCESS(status)) {
|
2002-08-21 19:12:24 +00:00
|
|
|
newCollator->setAttribute(UCOL_DECOMPOSITION_MODE, UCOL_ON, status);
|
2001-10-19 16:24:26 +00:00
|
|
|
// cast away const
|
|
|
|
((RuleBasedNumberFormat*)this)->collator = newCollator;
|
|
|
|
} else {
|
|
|
|
delete newCollator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete temp;
|
|
|
|
}
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
// if lenient-parse mode is off, this will be null
|
|
|
|
// (see setLenientParseMode())
|
|
|
|
return collator;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the DecimalFormatSymbols object that should be used by all DecimalFormat
|
|
|
|
* instances owned by this formatter. This object is lazily created: this function
|
|
|
|
* creates it the first time it's called.
|
|
|
|
* @return The DecimalFormatSymbols object that should be used by all DecimalFormat
|
|
|
|
* instances owned by this formatter.
|
|
|
|
*/
|
|
|
|
DecimalFormatSymbols*
|
|
|
|
RuleBasedNumberFormat::getDecimalFormatSymbols() const
|
|
|
|
{
|
|
|
|
// lazy-evaluate the DecimalFormatSymbols object. This object
|
|
|
|
// is shared by all DecimalFormat instances belonging to this
|
|
|
|
// formatter
|
|
|
|
if (decimalFormatSymbols == NULL) {
|
2001-10-19 16:24:26 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
DecimalFormatSymbols* temp = new DecimalFormatSymbols(locale, status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
((RuleBasedNumberFormat*)this)->decimalFormatSymbols = temp;
|
|
|
|
} else {
|
|
|
|
delete temp;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
return decimalFormatSymbols;
|
|
|
|
}
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
/* U_HAVE_RBNF */
|
|
|
|
#endif
|