2001-10-10 21:43:11 +00:00
|
|
|
/*
|
2001-10-11 16:56:24 +00:00
|
|
|
******************************************************************************
|
2011-04-26 06:39:29 +00:00
|
|
|
* Copyright (C) 1997-2011, International Business Machines
|
2001-10-11 16:56:24 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
******************************************************************************
|
|
|
|
* file name: nfrs.cpp
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* Modification history
|
|
|
|
* Date Name Comments
|
|
|
|
* 10/11/2001 Doug Ported from ICU4J
|
2001-10-10 21:43:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nfrs.h"
|
2002-03-14 00:28:22 +00:00
|
|
|
|
|
|
|
#if U_HAVE_RBNF
|
|
|
|
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-10-10 21:43:11 +00:00
|
|
|
#include "nfrule.h"
|
|
|
|
#include "nfrlist.h"
|
2011-04-26 06:39:29 +00:00
|
|
|
#include "patternprops.h"
|
2001-11-30 00:06:13 +00:00
|
|
|
|
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-10 21:43:11 +00:00
|
|
|
#include "cmemory.h"
|
2001-11-30 00:06:13 +00:00
|
|
|
#endif
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// euclid's algorithm works with doubles
|
|
|
|
// note, doubles only get us up to one quadrillion or so, which
|
|
|
|
// isn't as much range as we get with longs. We probably still
|
|
|
|
// want either 64-bit math, or BigInteger.
|
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
static int64_t
|
|
|
|
util_lcm(int64_t x, int64_t y)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
x.abs();
|
|
|
|
y.abs();
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
if (x == 0 || y == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (x < y) {
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t t = x; x = y; y = t;
|
2001-10-11 16:56:24 +00:00
|
|
|
}
|
|
|
|
x -= y * (x/y);
|
|
|
|
} while (x != 0);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
return y;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
/**
|
|
|
|
* Calculates the least common multiple of x and y.
|
|
|
|
*/
|
2002-03-14 00:28:22 +00:00
|
|
|
static int64_t
|
|
|
|
util_lcm(int64_t x, int64_t y)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
// binary gcd algorithm from Knuth, "The Art of Computer Programming,"
|
|
|
|
// vol. 2, 1st ed., pp. 298-299
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t x1 = x;
|
|
|
|
int64_t y1 = y;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
int p2 = 0;
|
|
|
|
while ((x1 & 1) == 0 && (y1 & 1) == 0) {
|
|
|
|
++p2;
|
|
|
|
x1 >>= 1;
|
|
|
|
y1 >>= 1;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t t;
|
2001-10-11 16:56:24 +00:00
|
|
|
if ((x1 & 1) == 1) {
|
|
|
|
t = -y1;
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
2001-10-11 16:56:24 +00:00
|
|
|
t = x1;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
while (t != 0) {
|
|
|
|
while ((t & 1) == 0) {
|
2002-03-18 20:06:16 +00:00
|
|
|
t = t >> 1;
|
2001-10-11 16:56:24 +00:00
|
|
|
}
|
|
|
|
if (t > 0) {
|
|
|
|
x1 = t;
|
|
|
|
} else {
|
|
|
|
y1 = -t;
|
|
|
|
}
|
|
|
|
t = x1 - y1;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t gcd = x1 << p2;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// x * y == gcd(x, y) * lcm(x, y)
|
|
|
|
return x / gcd * y;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const UChar gPercent = 0x0025;
|
|
|
|
static const UChar gColon = 0x003a;
|
|
|
|
static const UChar gSemicolon = 0x003b;
|
2001-11-28 00:58:33 +00:00
|
|
|
static const UChar gLineFeed = 0x000a;
|
2001-10-10 21:43:11 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
static const UChar gFourSpaces[] =
|
|
|
|
{
|
|
|
|
0x20, 0x20, 0x20, 0x20, 0
|
|
|
|
}; /* " " */
|
2002-02-28 01:42:40 +00:00
|
|
|
static const UChar gPercentPercent[] =
|
2001-10-11 16:56:24 +00:00
|
|
|
{
|
|
|
|
0x25, 0x25, 0
|
|
|
|
}; /* "%%" */
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
NFRuleSet::NFRuleSet(UnicodeString* descriptions, int32_t index, UErrorCode& status)
|
|
|
|
: name()
|
|
|
|
, rules(0)
|
|
|
|
, negativeNumberRule(NULL)
|
|
|
|
, fIsFractionRuleSet(FALSE)
|
|
|
|
, fIsPublic(FALSE)
|
2004-04-09 19:58:58 +00:00
|
|
|
, fRecursionCount(0)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
fractionRules[i] = NULL;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
UnicodeString& description = descriptions[index]; // !!! make sure index is valid
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2004-04-14 18:55:19 +00:00
|
|
|
if (description.length() == 0) {
|
|
|
|
// throw new IllegalArgumentException("Empty rule set description");
|
|
|
|
status = U_PARSE_ERROR;
|
2004-12-31 18:54:55 +00:00
|
|
|
return;
|
2004-04-14 18:55:19 +00:00
|
|
|
}
|
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if the description begins with a rule set name (the rule set
|
|
|
|
// name can be omitted in formatter descriptions that consist
|
|
|
|
// of only one rule set), copy it out into our "name" member
|
|
|
|
// and delete it from the description
|
|
|
|
if (description.charAt(0) == gPercent) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t pos = description.indexOf(gColon);
|
2001-10-11 16:56:24 +00:00
|
|
|
if (pos == -1) {
|
|
|
|
// throw new IllegalArgumentException("Rule set name doesn't end in colon");
|
|
|
|
status = U_PARSE_ERROR;
|
|
|
|
} else {
|
|
|
|
name.setTo(description, 0, pos);
|
2011-04-26 06:39:29 +00:00
|
|
|
while (pos < description.length() && PatternProps::isWhiteSpace(description.charAt(++pos))) {
|
2001-10-11 16:56:24 +00:00
|
|
|
}
|
|
|
|
description.remove(0, pos);
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
2004-09-15 23:44:48 +00:00
|
|
|
name.setTo(UNICODE_STRING_SIMPLE("%default"));
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
if (description.length() == 0) {
|
|
|
|
// throw new IllegalArgumentException("Empty rule set description");
|
|
|
|
status = U_PARSE_ERROR;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
fIsPublic = name.indexOf(gPercentPercent) != 0;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// all of the other members of NFRuleSet are initialized
|
|
|
|
// by parseRules()
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
void
|
|
|
|
NFRuleSet::parseRules(UnicodeString& description, const RuleBasedNumberFormat* owner, UErrorCode& status)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
// start by creating a Vector whose elements are Strings containing
|
|
|
|
// the descriptions of the rules (one rule per element). The rules
|
|
|
|
// are separated by semicolons (there's no escape facility: ALL
|
|
|
|
// semicolons are rule delimiters)
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2001-10-10 21:43:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// dlf - the original code kept a separate description array for no reason,
|
|
|
|
// so I got rid of it. The loop was too complex so I simplified it.
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
UnicodeString currentDescription;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t oldP = 0;
|
2001-10-11 16:56:24 +00:00
|
|
|
while (oldP < description.length()) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t p = description.indexOf(gSemicolon, oldP);
|
2001-10-11 16:56:24 +00:00
|
|
|
if (p == -1) {
|
|
|
|
p = description.length();
|
|
|
|
}
|
|
|
|
currentDescription.setTo(description, oldP, p - oldP);
|
|
|
|
NFRule::makeRules(currentDescription, this, rules.last(), owner, rules, status);
|
|
|
|
oldP = p + 1;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// for rules that didn't specify a base value, their base values
|
|
|
|
// were initialized to 0. Make another pass through the list and
|
|
|
|
// set all those rules' base values. We also remove any special
|
|
|
|
// rules from the list and put them into their own member variables
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t defaultBaseValue = 0;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// (this isn't a for loop because we might be deleting items from
|
|
|
|
// the vector-- we want to make sure we only increment i when
|
|
|
|
// we _didn't_ delete aything from the vector)
|
|
|
|
uint32_t i = 0;
|
|
|
|
while (i < rules.size()) {
|
|
|
|
NFRule* rule = rules[i];
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
switch (rule->getType()) {
|
|
|
|
// if the rule's base value is 0, fill in a default
|
|
|
|
// base value (this will be 1 plus the preceding
|
|
|
|
// rule's base value for regular rule sets, and the
|
|
|
|
// same as the preceding rule's base value in fraction
|
|
|
|
// rule sets)
|
|
|
|
case NFRule::kNoBase:
|
2004-04-09 19:58:58 +00:00
|
|
|
rule->setBaseValue(defaultBaseValue, status);
|
2001-10-11 16:56:24 +00:00
|
|
|
if (!isFractionRuleSet()) {
|
|
|
|
++defaultBaseValue;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
break;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if it's the negative-number rule, copy it into its own
|
|
|
|
// data member and delete it from the list
|
|
|
|
case NFRule::kNegativeNumberRule:
|
|
|
|
negativeNumberRule = rules.remove(i);
|
|
|
|
break;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if it's the improper fraction rule, copy it into the
|
|
|
|
// correct element of fractionRules
|
|
|
|
case NFRule::kImproperFractionRule:
|
|
|
|
fractionRules[0] = rules.remove(i);
|
|
|
|
break;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if it's the proper fraction rule, copy it into the
|
|
|
|
// correct element of fractionRules
|
|
|
|
case NFRule::kProperFractionRule:
|
|
|
|
fractionRules[1] = rules.remove(i);
|
|
|
|
break;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if it's the master rule, copy it into the
|
|
|
|
// correct element of fractionRules
|
|
|
|
case NFRule::kMasterRule:
|
|
|
|
fractionRules[2] = rules.remove(i);
|
|
|
|
break;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if it's a regular rule that already knows its base value,
|
|
|
|
// check to make sure the rules are in order, and update
|
|
|
|
// the default base value for the next rule
|
|
|
|
default:
|
|
|
|
if (rule->getBaseValue() < defaultBaseValue) {
|
|
|
|
// throw new IllegalArgumentException("Rules are not in order");
|
|
|
|
status = U_PARSE_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
defaultBaseValue = rule->getBaseValue();
|
|
|
|
if (!isFractionRuleSet()) {
|
|
|
|
++defaultBaseValue;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NFRuleSet::~NFRuleSet()
|
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
delete negativeNumberRule;
|
|
|
|
delete fractionRules[0];
|
|
|
|
delete fractionRules[1];
|
|
|
|
delete fractionRules[2];
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2004-01-15 22:12:26 +00:00
|
|
|
static UBool
|
2002-02-28 01:42:40 +00:00
|
|
|
util_equalRules(const NFRule* rule1, const NFRule* rule2)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
if (rule1) {
|
|
|
|
if (rule2) {
|
|
|
|
return *rule1 == *rule2;
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
2001-10-11 16:56:24 +00:00
|
|
|
} else if (!rule2) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
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
|
|
|
NFRuleSet::operator==(const NFRuleSet& rhs) const
|
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
if (rules.size() == rhs.rules.size() &&
|
|
|
|
fIsFractionRuleSet == rhs.fIsFractionRuleSet &&
|
|
|
|
name == rhs.name &&
|
|
|
|
util_equalRules(negativeNumberRule, rhs.negativeNumberRule) &&
|
|
|
|
util_equalRules(fractionRules[0], rhs.fractionRules[0]) &&
|
|
|
|
util_equalRules(fractionRules[1], rhs.fractionRules[1]) &&
|
|
|
|
util_equalRules(fractionRules[2], rhs.fractionRules[2])) {
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
for (uint32_t i = 0; i < rules.size(); ++i) {
|
|
|
|
if (*rules[i] != *rhs.rules[i]) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2001-10-11 16:56:24 +00:00
|
|
|
return FALSE;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2004-04-09 19:58:58 +00:00
|
|
|
#define RECURSION_LIMIT 50
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
void
|
2002-03-14 00:28:22 +00:00
|
|
|
NFRuleSet::format(int64_t number, UnicodeString& toAppendTo, int32_t pos) const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
NFRule *rule = findNormalRule(number);
|
2004-04-09 19:58:58 +00:00
|
|
|
if (rule) { // else error, but can't report it
|
|
|
|
NFRuleSet* ncThis = (NFRuleSet*)this;
|
|
|
|
if (ncThis->fRecursionCount++ >= RECURSION_LIMIT) {
|
|
|
|
// stop recursion
|
|
|
|
ncThis->fRecursionCount = 0;
|
|
|
|
} else {
|
|
|
|
rule->doFormat(number, toAppendTo, pos);
|
|
|
|
ncThis->fRecursionCount--;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
NFRuleSet::format(double number, UnicodeString& toAppendTo, int32_t pos) const
|
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
NFRule *rule = findDoubleRule(number);
|
2004-04-09 19:58:58 +00:00
|
|
|
if (rule) { // else error, but can't report it
|
|
|
|
NFRuleSet* ncThis = (NFRuleSet*)this;
|
|
|
|
if (ncThis->fRecursionCount++ >= RECURSION_LIMIT) {
|
|
|
|
// stop recursion
|
|
|
|
ncThis->fRecursionCount = 0;
|
|
|
|
} else {
|
|
|
|
rule->doFormat(number, toAppendTo, pos);
|
|
|
|
ncThis->fRecursionCount--;
|
|
|
|
}
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
NFRule*
|
2001-10-10 21:43:11 +00:00
|
|
|
NFRuleSet::findDoubleRule(double number) const
|
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
// if this is a fraction rule set, use findFractionRuleSetRule()
|
|
|
|
if (isFractionRuleSet()) {
|
|
|
|
return findFractionRuleSetRule(number);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if the number is negative, return the negative number rule
|
|
|
|
// (if there isn't a negative-number rule, we pretend it's a
|
|
|
|
// positive number)
|
|
|
|
if (number < 0) {
|
|
|
|
if (negativeNumberRule) {
|
|
|
|
return negativeNumberRule;
|
|
|
|
} else {
|
|
|
|
number = -number;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if the number isn't an integer, we use one of the fraction rules...
|
|
|
|
if (number != uprv_floor(number)) {
|
|
|
|
// if the number is between 0 and 1, return the proper
|
|
|
|
// fraction rule
|
|
|
|
if (number < 1 && fractionRules[1]) {
|
|
|
|
return fractionRules[1];
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
2001-10-11 16:56:24 +00:00
|
|
|
// otherwise, return the improper fraction rule
|
|
|
|
else if (fractionRules[0]) {
|
|
|
|
return fractionRules[0];
|
|
|
|
}
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if there's a master rule, use it to format the number
|
|
|
|
if (fractionRules[2]) {
|
|
|
|
return fractionRules[2];
|
2002-02-28 01:42:40 +00:00
|
|
|
}
|
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// and if we haven't yet returned a rule, use findNormalRule()
|
|
|
|
// to find the applicable rule
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t r = util64_fromDouble(number + 0.5);
|
2001-10-11 16:56:24 +00:00
|
|
|
return findNormalRule(r);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NFRule *
|
2002-03-14 00:28:22 +00:00
|
|
|
NFRuleSet::findNormalRule(int64_t number) const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
// if this is a fraction rule set, use findFractionRuleSetRule()
|
|
|
|
// to find the rule (we should only go into this clause if the
|
|
|
|
// value is 0)
|
|
|
|
if (fIsFractionRuleSet) {
|
2002-03-14 00:28:22 +00:00
|
|
|
return findFractionRuleSetRule((double)number);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if the number is negative, return the negative-number rule
|
|
|
|
// (if there isn't one, pretend the number is positive)
|
|
|
|
if (number < 0) {
|
|
|
|
if (negativeNumberRule) {
|
|
|
|
return negativeNumberRule;
|
|
|
|
} else {
|
|
|
|
number = -number;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// we have to repeat the preceding two checks, even though we
|
|
|
|
// do them in findRule(), because the version of format() that
|
|
|
|
// takes a long bypasses findRule() and goes straight to this
|
|
|
|
// function. This function does skip the fraction rules since
|
|
|
|
// we know the value is an integer (it also skips the master
|
|
|
|
// rule, since it's considered a fraction rule. Skipping the
|
|
|
|
// master rule in this function is also how we avoid infinite
|
|
|
|
// recursion)
|
2002-02-28 01:42:40 +00:00
|
|
|
|
|
|
|
// {dlf} unfortunately this fails if there are no rules except
|
|
|
|
// special rules. If there are no rules, use the master rule.
|
2001-11-28 23:31:53 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// binary-search the rule list for the applicable rule
|
|
|
|
// (a rule is used for all values from its base value to
|
|
|
|
// the next rule's base value)
|
|
|
|
int32_t hi = rules.size();
|
2002-02-28 01:42:40 +00:00
|
|
|
if (hi > 0) {
|
|
|
|
int32_t lo = 0;
|
|
|
|
|
|
|
|
while (lo < hi) {
|
|
|
|
int32_t mid = (lo + hi) / 2;
|
|
|
|
if (rules[mid]->getBaseValue() == number) {
|
|
|
|
return rules[mid];
|
|
|
|
}
|
|
|
|
else if (rules[mid]->getBaseValue() > number) {
|
|
|
|
hi = mid;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lo = mid + 1;
|
|
|
|
}
|
|
|
|
}
|
2004-04-09 19:58:58 +00:00
|
|
|
if (hi == 0) { // bad rule set, minimum base > 0
|
|
|
|
return NULL; // want to throw exception here
|
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
NFRule *result = rules[hi - 1];
|
|
|
|
|
|
|
|
// use shouldRollBack() to see whether we need to invoke the
|
|
|
|
// rollback rule (see shouldRollBack()'s documentation for
|
|
|
|
// an explanation of the rollback rule). If we do, roll back
|
|
|
|
// one rule and return that one instead of the one we'd normally
|
|
|
|
// return
|
2002-03-14 00:28:22 +00:00
|
|
|
if (result->shouldRollBack((double)number)) {
|
2004-04-09 19:58:58 +00:00
|
|
|
if (hi == 1) { // bad rule set, no prior rule to rollback to from this base
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
result = rules[hi - 2];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// else use the master rule
|
|
|
|
return fractionRules[2];
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this rule is a fraction rule set, this function is used by
|
|
|
|
* findRule() to select the most appropriate rule for formatting
|
|
|
|
* the number. Basically, the base value of each rule in the rule
|
|
|
|
* set is treated as the denominator of a fraction. Whichever
|
|
|
|
* denominator can produce the fraction closest in value to the
|
|
|
|
* number passed in is the result. If there's a tie, the earlier
|
|
|
|
* one in the list wins. (If there are two rules in a row with the
|
|
|
|
* same base value, the first one is used when the numerator of the
|
|
|
|
* fraction would be 1, and the second rule is used the rest of the
|
|
|
|
* time.
|
|
|
|
* @param number The number being formatted (which will always be
|
|
|
|
* a number between 0 and 1)
|
|
|
|
* @return The rule to use to format this number
|
|
|
|
*/
|
|
|
|
NFRule*
|
2002-02-28 01:42:40 +00:00
|
|
|
NFRuleSet::findFractionRuleSetRule(double number) const
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
// the obvious way to do this (multiply the value being formatted
|
|
|
|
// by each rule's base value until you get an integral result)
|
|
|
|
// doesn't work because of rounding error. This method is more
|
|
|
|
// accurate
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// find the least common multiple of the rules' base values
|
|
|
|
// and multiply this by the number being formatted. This is
|
|
|
|
// all the precision we need, and we can do all of the rest
|
|
|
|
// of the math using integer arithmetic
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t leastCommonMultiple = rules[0]->getBaseValue();
|
|
|
|
int64_t numerator;
|
2001-10-11 16:56:24 +00:00
|
|
|
{
|
|
|
|
for (uint32_t i = 1; i < rules.size(); ++i) {
|
|
|
|
leastCommonMultiple = util_lcm(leastCommonMultiple, rules[i]->getBaseValue());
|
|
|
|
}
|
2002-03-14 00:28:22 +00:00
|
|
|
numerator = util64_fromDouble(number * (double)leastCommonMultiple + 0.5);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2001-10-11 16:56:24 +00:00
|
|
|
// for each rule, do the following...
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t tempDifference;
|
|
|
|
int64_t difference = util64_fromDouble(uprv_maxMantissa());
|
2001-10-11 16:56:24 +00:00
|
|
|
int32_t winner = 0;
|
|
|
|
for (uint32_t i = 0; i < rules.size(); ++i) {
|
2002-02-28 01:42:40 +00:00
|
|
|
// "numerator" is the numerator of the fraction if the
|
2001-10-11 16:56:24 +00:00
|
|
|
// denominator is the LCD. The numerator if the rule's
|
|
|
|
// base value is the denominator is "numerator" times the
|
|
|
|
// base value divided bythe LCD. Here we check to see if
|
|
|
|
// that's an integer, and if not, how close it is to being
|
|
|
|
// an integer.
|
|
|
|
tempDifference = numerator * rules[i]->getBaseValue() % leastCommonMultiple;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// normalize the result of the above calculation: we want
|
|
|
|
// the numerator's distance from the CLOSEST multiple
|
|
|
|
// of the LCD
|
|
|
|
if (leastCommonMultiple - tempDifference < tempDifference) {
|
|
|
|
tempDifference = leastCommonMultiple - tempDifference;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if this is as close as we've come, keep track of how close
|
|
|
|
// that is, and the line number of the rule that did it. If
|
|
|
|
// we've scored a direct hit, we don't have to look at any more
|
|
|
|
// rules
|
|
|
|
if (tempDifference < difference) {
|
|
|
|
difference = tempDifference;
|
|
|
|
winner = i;
|
|
|
|
if (difference == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// if we have two successive rules that both have the winning base
|
|
|
|
// value, then the first one (the one we found above) is used if
|
|
|
|
// the numerator of the fraction is 1 and the second one is used if
|
|
|
|
// the numerator of the fraction is anything else (this lets us
|
|
|
|
// do things like "one third"/"two thirds" without haveing to define
|
|
|
|
// a whole bunch of extra rule sets)
|
|
|
|
if ((unsigned)(winner + 1) < rules.size() &&
|
|
|
|
rules[winner + 1]->getBaseValue() == rules[winner]->getBaseValue()) {
|
2002-03-14 00:28:22 +00:00
|
|
|
double n = ((double)rules[winner]->getBaseValue()) * number;
|
2001-10-11 16:56:24 +00:00
|
|
|
if (n < 0.5 || n >= 2) {
|
|
|
|
++winner;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// finally, return the winning rule
|
|
|
|
return rules[winner];
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a string. Matches the string to be parsed against each
|
|
|
|
* of its rules (with a base value less than upperBound) and returns
|
|
|
|
* the value produced by the rule that matched the most charcters
|
|
|
|
* in the source string.
|
|
|
|
* @param text The string to parse
|
|
|
|
* @param parsePosition The initial position is ignored and assumed
|
|
|
|
* to be 0. On exit, this object has been updated to point to the
|
|
|
|
* first character position this rule set didn't consume.
|
|
|
|
* @param upperBound Limits the rules that can be allowed to match.
|
|
|
|
* Only rules whose base values are strictly less than upperBound
|
|
|
|
* are considered.
|
|
|
|
* @return The numerical result of parsing this string. This will
|
|
|
|
* be the matching rule's base value, composed appropriately with
|
|
|
|
* the results of matching any of its substitutions. The object
|
|
|
|
* will be an instance of Long if it's an integral value; otherwise,
|
|
|
|
* it will be an instance of Double. This function always returns
|
|
|
|
* a valid object: If nothing matched the input string at all,
|
|
|
|
* this function returns new Long(0), and the parse position is
|
|
|
|
* left unchanged.
|
|
|
|
*/
|
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-24 00:47:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
static void dumpUS(FILE* f, const UnicodeString& us) {
|
|
|
|
int len = us.length();
|
2003-06-09 21:22:08 +00:00
|
|
|
char* buf = (char *)uprv_malloc((len+1)*sizeof(char)); //new char[len+1];
|
2008-01-11 22:38:26 +00:00
|
|
|
if (buf != NULL) {
|
|
|
|
us.extract(0, len, buf);
|
|
|
|
buf[len] = 0;
|
|
|
|
fprintf(f, "%s", buf);
|
|
|
|
uprv_free(buf); //delete[] buf;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
UBool
|
|
|
|
NFRuleSet::parse(const UnicodeString& text, ParsePosition& pos, double upperBound, Formattable& result) const
|
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
// try matching each rule in the rule set against the text being
|
|
|
|
// parsed. Whichever one matches the most characters is the one
|
|
|
|
// that determines the value we return.
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
result.setLong(0);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// dump out if there's no text to parse
|
|
|
|
if (text.length() == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
ParsePosition highWaterMark;
|
|
|
|
ParsePosition workingPos = pos;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-11 16:56:24 +00:00
|
|
|
fprintf(stderr, "<nfrs> %x '", this);
|
|
|
|
dumpUS(stderr, name);
|
|
|
|
fprintf(stderr, "' text '");
|
|
|
|
dumpUS(stderr, text);
|
|
|
|
fprintf(stderr, "'\n");
|
|
|
|
fprintf(stderr, " parse negative: %d\n", this, negativeNumberRule != 0);
|
2001-10-10 21:43:11 +00:00
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// start by trying the negative number rule (if there is one)
|
|
|
|
if (negativeNumberRule) {
|
|
|
|
Formattable tempResult;
|
2001-10-10 21:43:11 +00:00
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-11 16:56:24 +00:00
|
|
|
fprintf(stderr, " <nfrs before negative> %x ub: %g\n", negativeNumberRule, upperBound);
|
2001-10-10 21:43:11 +00:00
|
|
|
#endif
|
2001-10-11 16:56:24 +00:00
|
|
|
UBool success = negativeNumberRule->doParse(text, workingPos, 0, upperBound, tempResult);
|
2001-10-10 21:43:11 +00:00
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-11 16:56:24 +00:00
|
|
|
fprintf(stderr, " <nfrs after negative> success: %d wpi: %d\n", success, workingPos.getIndex());
|
2001-10-10 21:43:11 +00:00
|
|
|
#endif
|
2001-10-11 16:56:24 +00:00
|
|
|
if (success && workingPos.getIndex() > highWaterMark.getIndex()) {
|
|
|
|
result = tempResult;
|
|
|
|
highWaterMark = workingPos;
|
|
|
|
}
|
|
|
|
workingPos = pos;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-11 16:56:24 +00:00
|
|
|
fprintf(stderr, "<nfrs> continue fractional with text '");
|
|
|
|
dumpUS(stderr, text);
|
|
|
|
fprintf(stderr, "' hwm: %d\n", highWaterMark.getIndex());
|
2001-10-10 21:43:11 +00:00
|
|
|
#endif
|
2001-10-11 16:56:24 +00:00
|
|
|
// then try each of the fraction rules
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (fractionRules[i]) {
|
|
|
|
Formattable tempResult;
|
|
|
|
UBool success = fractionRules[i]->doParse(text, workingPos, 0, upperBound, tempResult);
|
|
|
|
if (success && (workingPos.getIndex() > highWaterMark.getIndex())) {
|
|
|
|
result = tempResult;
|
|
|
|
highWaterMark = workingPos;
|
|
|
|
}
|
|
|
|
workingPos = pos;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-11 16:56:24 +00:00
|
|
|
fprintf(stderr, "<nfrs> continue other with text '");
|
|
|
|
dumpUS(stderr, text);
|
|
|
|
fprintf(stderr, "' hwm: %d\n", highWaterMark.getIndex());
|
2001-10-10 21:43:11 +00:00
|
|
|
#endif
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// finally, go through the regular rules one at a time. We start
|
|
|
|
// at the end of the list because we want to try matching the most
|
|
|
|
// sigificant rule first (this helps ensure that we parse
|
|
|
|
// "five thousand three hundred six" as
|
|
|
|
// "(five thousand) (three hundred) (six)" rather than
|
|
|
|
// "((five thousand three) hundred) (six)"). Skip rules whose
|
|
|
|
// base values are higher than the upper bound (again, this helps
|
|
|
|
// limit ambiguity by making sure the rules that match a rule's
|
|
|
|
// are less significant than the rule containing the substitutions)/
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
2002-03-14 00:28:22 +00:00
|
|
|
int64_t ub = util64_fromDouble(upperBound);
|
2001-10-11 16:56:24 +00:00
|
|
|
#ifdef RBNF_DEBUG
|
|
|
|
{
|
|
|
|
char ubstr[64];
|
2002-03-14 00:28:22 +00:00
|
|
|
util64_toa(ub, ubstr, 64);
|
2002-04-02 02:55:31 +00:00
|
|
|
char ubstrhex[64];
|
|
|
|
util64_toa(ub, ubstrhex, 64, 16);
|
2002-03-14 00:28:22 +00:00
|
|
|
fprintf(stderr, "ub: %g, i64: %s (%s)\n", upperBound, ubstr, ubstrhex);
|
2001-10-11 16:56:24 +00:00
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
#endif
|
2001-10-11 16:56:24 +00:00
|
|
|
for (int32_t i = rules.size(); --i >= 0 && highWaterMark.getIndex() < text.length();) {
|
|
|
|
if ((!fIsFractionRuleSet) && (rules[i]->getBaseValue() >= ub)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Formattable tempResult;
|
|
|
|
UBool success = rules[i]->doParse(text, workingPos, fIsFractionRuleSet, upperBound, tempResult);
|
|
|
|
if (success && workingPos.getIndex() > highWaterMark.getIndex()) {
|
|
|
|
result = tempResult;
|
|
|
|
highWaterMark = workingPos;
|
|
|
|
}
|
|
|
|
workingPos = pos;
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
#ifdef RBNF_DEBUG
|
2001-10-11 16:56:24 +00:00
|
|
|
fprintf(stderr, "<nfrs> exit\n");
|
2001-10-10 21:43:11 +00:00
|
|
|
#endif
|
2001-10-11 16:56:24 +00:00
|
|
|
// finally, update the parse postion we were passed to point to the
|
|
|
|
// first character we didn't use, and return the result that
|
|
|
|
// corresponds to that string of characters
|
|
|
|
pos = highWaterMark;
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
return 1;
|
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
|
|
|
NFRuleSet::appendRules(UnicodeString& result) const
|
|
|
|
{
|
2001-10-11 16:56:24 +00:00
|
|
|
// the rule set name goes first...
|
|
|
|
result.append(name);
|
|
|
|
result.append(gColon);
|
|
|
|
result.append(gLineFeed);
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// followed by the regular rules...
|
|
|
|
for (uint32_t i = 0; i < rules.size(); i++) {
|
|
|
|
result.append(gFourSpaces);
|
2005-06-14 17:14:30 +00:00
|
|
|
rules[i]->_appendRuleText(result);
|
2001-10-11 16:56:24 +00:00
|
|
|
result.append(gLineFeed);
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
// followed by the special rules (if they exist)
|
|
|
|
if (negativeNumberRule) {
|
|
|
|
result.append(gFourSpaces);
|
2005-06-14 17:14:30 +00:00
|
|
|
negativeNumberRule->_appendRuleText(result);
|
2001-10-11 16:56:24 +00:00
|
|
|
result.append(gLineFeed);
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
|
2001-10-11 16:56:24 +00:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < 3; ++i) {
|
|
|
|
if (fractionRules[i]) {
|
|
|
|
result.append(gFourSpaces);
|
2005-06-14 17:14:30 +00:00
|
|
|
fractionRules[i]->_appendRuleText(result);
|
2001-10-11 16:56:24 +00:00
|
|
|
result.append(gLineFeed);
|
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-11 16:56:24 +00:00
|
|
|
}
|
2001-10-10 21:43:11 +00:00
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
// utility functions
|
|
|
|
|
|
|
|
int64_t util64_fromDouble(double d) {
|
2002-04-02 02:55:31 +00:00
|
|
|
int64_t result = 0;
|
2002-03-14 00:28:22 +00:00
|
|
|
if (!uprv_isNaN(d)) {
|
|
|
|
double mant = uprv_maxMantissa();
|
|
|
|
if (d < -mant) {
|
|
|
|
d = -mant;
|
|
|
|
} else if (d > mant) {
|
|
|
|
d = mant;
|
|
|
|
}
|
|
|
|
UBool neg = d < 0;
|
|
|
|
if (neg) {
|
|
|
|
d = -d;
|
|
|
|
}
|
|
|
|
result = (int64_t)uprv_floor(d);
|
|
|
|
if (neg) {
|
|
|
|
result = -result;
|
|
|
|
}
|
|
|
|
}
|
2002-04-02 02:55:31 +00:00
|
|
|
return result;
|
2002-03-14 00:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t util64_pow(int32_t r, uint32_t e) {
|
|
|
|
if (r == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (e == 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
2002-04-02 02:55:31 +00:00
|
|
|
int64_t n = r;
|
2002-03-14 00:28:22 +00:00
|
|
|
while (--e > 0) {
|
|
|
|
n *= r;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t asciiDigits[] = {
|
|
|
|
0x30u, 0x31u, 0x32u, 0x33u, 0x34u, 0x35u, 0x36u, 0x37u,
|
|
|
|
0x38u, 0x39u, 0x61u, 0x62u, 0x63u, 0x64u, 0x65u, 0x66u,
|
|
|
|
0x67u, 0x68u, 0x69u, 0x6au, 0x6bu, 0x6cu, 0x6du, 0x6eu,
|
|
|
|
0x6fu, 0x70u, 0x71u, 0x72u, 0x73u, 0x74u, 0x75u, 0x76u,
|
|
|
|
0x77u, 0x78u, 0x79u, 0x7au,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const UChar kUMinus = (UChar)0x002d;
|
|
|
|
|
2005-06-16 17:22:32 +00:00
|
|
|
#ifdef RBNF_DEBUG
|
2002-03-14 00:28:22 +00:00
|
|
|
static const char kMinus = '-';
|
|
|
|
|
|
|
|
static const uint8_t digitInfo[] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0x80u, 0x81u, 0x82u, 0x83u, 0x84u, 0x85u, 0x86u, 0x87u,
|
|
|
|
0x88u, 0x89u, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0x8au, 0x8bu, 0x8cu, 0x8du, 0x8eu, 0x8fu, 0x90u,
|
|
|
|
0x91u, 0x92u, 0x93u, 0x94u, 0x95u, 0x96u, 0x97u, 0x98u,
|
|
|
|
0x99u, 0x9au, 0x9bu, 0x9cu, 0x9du, 0x9eu, 0x9fu, 0xa0u,
|
|
|
|
0xa1u, 0xa2u, 0xa3u, 0, 0, 0, 0, 0,
|
|
|
|
0, 0x8au, 0x8bu, 0x8cu, 0x8du, 0x8eu, 0x8fu, 0x90u,
|
|
|
|
0x91u, 0x92u, 0x93u, 0x94u, 0x95u, 0x96u, 0x97u, 0x98u,
|
|
|
|
0x99u, 0x9au, 0x9bu, 0x9cu, 0x9du, 0x9eu, 0x9fu, 0xa0u,
|
|
|
|
0xa1u, 0xa2u, 0xa3u, 0, 0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
int64_t util64_atoi(const char* str, uint32_t radix)
|
|
|
|
{
|
|
|
|
if (radix > 36) {
|
|
|
|
radix = 36;
|
|
|
|
} else if (radix < 2) {
|
|
|
|
radix = 2;
|
|
|
|
}
|
|
|
|
int64_t lradix = radix;
|
|
|
|
|
|
|
|
int neg = 0;
|
|
|
|
if (*str == kMinus) {
|
|
|
|
++str;
|
|
|
|
neg = 1;
|
|
|
|
}
|
|
|
|
int64_t result = 0;
|
|
|
|
uint8_t b;
|
|
|
|
while ((b = digitInfo[*str++]) && ((b &= 0x7f) < radix)) {
|
|
|
|
result *= lradix;
|
|
|
|
result += (int32_t)b;
|
|
|
|
}
|
|
|
|
if (neg) {
|
|
|
|
result = -result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t util64_utoi(const UChar* str, uint32_t radix)
|
|
|
|
{
|
|
|
|
if (radix > 36) {
|
|
|
|
radix = 36;
|
|
|
|
} else if (radix < 2) {
|
|
|
|
radix = 2;
|
|
|
|
}
|
|
|
|
int64_t lradix = radix;
|
|
|
|
|
|
|
|
int neg = 0;
|
|
|
|
if (*str == kUMinus) {
|
|
|
|
++str;
|
|
|
|
neg = 1;
|
|
|
|
}
|
|
|
|
int64_t result = 0;
|
|
|
|
UChar c;
|
|
|
|
uint8_t b;
|
|
|
|
while (((c = *str++) < 0x0080) && (b = digitInfo[c]) && ((b &= 0x7f) < radix)) {
|
|
|
|
result *= lradix;
|
|
|
|
result += (int32_t)b;
|
|
|
|
}
|
|
|
|
if (neg) {
|
|
|
|
result = -result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t util64_toa(int64_t w, char* buf, uint32_t len, uint32_t radix, UBool raw)
|
|
|
|
{
|
|
|
|
if (radix > 36) {
|
|
|
|
radix = 36;
|
|
|
|
} else if (radix < 2) {
|
|
|
|
radix = 2;
|
|
|
|
}
|
|
|
|
int64_t base = radix;
|
|
|
|
|
|
|
|
char* p = buf;
|
|
|
|
if (len && (w < 0) && (radix == 10) && !raw) {
|
|
|
|
w = -w;
|
|
|
|
*p++ = kMinus;
|
|
|
|
--len;
|
|
|
|
} else if (len && (w == 0)) {
|
2002-04-02 02:55:31 +00:00
|
|
|
*p++ = (char)raw ? 0 : asciiDigits[0];
|
|
|
|
--len;
|
2002-03-14 00:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (len && w != 0) {
|
|
|
|
int64_t n = w / base;
|
|
|
|
int64_t m = n * base;
|
|
|
|
int32_t d = (int32_t)(w-m);
|
|
|
|
*p++ = raw ? (char)d : asciiDigits[d];
|
|
|
|
w = n;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
if (len) {
|
|
|
|
*p = 0; // null terminate if room for caller convenience
|
|
|
|
}
|
|
|
|
|
|
|
|
len = p - buf;
|
|
|
|
if (*buf == kMinus) {
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
while (--p > buf) {
|
|
|
|
char c = *p;
|
|
|
|
*p = *buf;
|
|
|
|
*buf = c;
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint32_t util64_tou(int64_t w, UChar* buf, uint32_t len, uint32_t radix, UBool raw)
|
|
|
|
{
|
|
|
|
if (radix > 36) {
|
|
|
|
radix = 36;
|
|
|
|
} else if (radix < 2) {
|
|
|
|
radix = 2;
|
|
|
|
}
|
|
|
|
int64_t base = radix;
|
|
|
|
|
|
|
|
UChar* p = buf;
|
|
|
|
if (len && (w < 0) && (radix == 10) && !raw) {
|
|
|
|
w = -w;
|
|
|
|
*p++ = kUMinus;
|
|
|
|
--len;
|
|
|
|
} else if (len && (w == 0)) {
|
2002-04-02 02:55:31 +00:00
|
|
|
*p++ = (UChar)raw ? 0 : asciiDigits[0];
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len && (w != 0)) {
|
|
|
|
int64_t n = w / base;
|
|
|
|
int64_t m = n * base;
|
|
|
|
int32_t d = (int32_t)(w-m);
|
|
|
|
*p++ = (UChar)(raw ? d : asciiDigits[d]);
|
|
|
|
w = n;
|
|
|
|
--len;
|
|
|
|
}
|
2002-03-14 00:28:22 +00:00
|
|
|
if (len) {
|
|
|
|
*p = 0; // null terminate if room for caller convenience
|
|
|
|
}
|
|
|
|
|
2002-04-30 21:03:00 +00:00
|
|
|
len = (uint32_t)(p - buf);
|
2002-03-14 00:28:22 +00:00
|
|
|
if (*buf == kUMinus) {
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
while (--p > buf) {
|
|
|
|
UChar c = *p;
|
|
|
|
*p = *buf;
|
|
|
|
*buf = c;
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
U_NAMESPACE_END
|
2002-03-14 00:28:22 +00:00
|
|
|
|
|
|
|
/* U_HAVE_RBNF */
|
|
|
|
#endif
|
|
|
|
|