+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
******************************************************************************
-* Copyright (C) 1997-2004, International Business Machines
+* Copyright (C) 1997-2015, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* file name: nfsubs.cpp
-* encoding: US-ASCII
+* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* 10/11/2001 Doug Ported from ICU4J
*/
+#include <stdio.h>
+#include "utypeinfo.h" // for 'typeid' to work
+
#include "nfsubs.h"
#include "digitlst.h"
-
-#include <stdio.h>
+#include "fmtableimp.h"
#if U_HAVE_RBNF
0x3E, 0x3E, 0
}; /* ">>" */
+U_NAMESPACE_BEGIN
+
+class SameValueSubstitution : public NFSubstitution {
+public:
+ SameValueSubstitution(int32_t pos,
+ const NFRuleSet* ruleset,
+ const UnicodeString& description,
+ UErrorCode& status);
+ virtual ~SameValueSubstitution();
+
+ virtual int64_t transformNumber(int64_t number) const { return number; }
+ virtual double transformNumber(double number) const { return number; }
+ virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return newRuleValue; }
+ virtual double calcUpperBound(double oldUpperBound) const { return oldUpperBound; }
+ virtual UChar tokenChar() const { return (UChar)0x003d; } // '='
+
+public:
+ static UClassID getStaticClassID(void);
+ virtual UClassID getDynamicClassID(void) const;
+};
+
+SameValueSubstitution::~SameValueSubstitution() {}
+
+class MultiplierSubstitution : public NFSubstitution {
+ int64_t divisor;
+
+public:
+ MultiplierSubstitution(int32_t _pos,
+ const NFRule *rule,
+ const NFRuleSet* _ruleSet,
+ const UnicodeString& description,
+ UErrorCode& status)
+ : NFSubstitution(_pos, _ruleSet, description, status), divisor(rule->getDivisor())
+ {
+ if (divisor == 0) {
+ status = U_PARSE_ERROR;
+ }
+ }
+ virtual ~MultiplierSubstitution();
+
+ virtual void setDivisor(int32_t radix, int16_t exponent, UErrorCode& status) {
+ divisor = util64_pow(radix, exponent);
+
+ if(divisor == 0) {
+ status = U_PARSE_ERROR;
+ }
+ }
+
+ virtual UBool operator==(const NFSubstitution& rhs) const;
+
+ virtual int64_t transformNumber(int64_t number) const {
+ return number / divisor;
+ }
+
+ virtual double transformNumber(double number) const {
+ if (getRuleSet()) {
+ return uprv_floor(number / divisor);
+ } else {
+ return number / divisor;
+ }
+ }
+
+ virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const {
+ return newRuleValue * divisor;
+ }
+
+ virtual double calcUpperBound(double /*oldUpperBound*/) const { return static_cast<double>(divisor); }
+
+ virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
+
+public:
+ static UClassID getStaticClassID(void);
+ virtual UClassID getDynamicClassID(void) const;
+};
+
+MultiplierSubstitution::~MultiplierSubstitution() {}
+
+class ModulusSubstitution : public NFSubstitution {
+ int64_t divisor;
+ const NFRule* ruleToUse;
+public:
+ ModulusSubstitution(int32_t pos,
+ const NFRule* rule,
+ const NFRule* rulePredecessor,
+ const NFRuleSet* ruleSet,
+ const UnicodeString& description,
+ UErrorCode& status);
+ virtual ~ModulusSubstitution();
+
+ virtual void setDivisor(int32_t radix, int16_t exponent, UErrorCode& status) {
+ divisor = util64_pow(radix, exponent);
+
+ if (divisor == 0) {
+ status = U_PARSE_ERROR;
+ }
+ }
+
+ virtual UBool operator==(const NFSubstitution& rhs) const;
+
+ virtual void doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
+ virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
+
+ virtual int64_t transformNumber(int64_t number) const { return number % divisor; }
+ virtual double transformNumber(double number) const { return uprv_fmod(number, static_cast<double>(divisor)); }
+
+ virtual UBool doParse(const UnicodeString& text,
+ ParsePosition& parsePosition,
+ double baseValue,
+ double upperBound,
+ UBool lenientParse,
+ uint32_t nonNumericalExecutedRuleMask,
+ Formattable& result) const;
+
+ virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const {
+ return oldRuleValue - uprv_fmod(oldRuleValue, static_cast<double>(divisor)) + newRuleValue;
+ }
+
+ virtual double calcUpperBound(double /*oldUpperBound*/) const { return static_cast<double>(divisor); }
+
+ virtual UBool isModulusSubstitution() const { return TRUE; }
+
+ virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
+
+ virtual void toString(UnicodeString& result) const;
+
+public:
+ static UClassID getStaticClassID(void);
+ virtual UClassID getDynamicClassID(void) const;
+};
+
+ModulusSubstitution::~ModulusSubstitution() {}
+
+class IntegralPartSubstitution : public NFSubstitution {
+public:
+ IntegralPartSubstitution(int32_t _pos,
+ const NFRuleSet* _ruleSet,
+ const UnicodeString& description,
+ UErrorCode& status)
+ : NFSubstitution(_pos, _ruleSet, description, status) {}
+ virtual ~IntegralPartSubstitution();
+
+ virtual int64_t transformNumber(int64_t number) const { return number; }
+ virtual double transformNumber(double number) const { return uprv_floor(number); }
+ virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
+ virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
+ virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
+
+public:
+ static UClassID getStaticClassID(void);
+ virtual UClassID getDynamicClassID(void) const;
+};
+
+IntegralPartSubstitution::~IntegralPartSubstitution() {}
+
+class FractionalPartSubstitution : public NFSubstitution {
+ UBool byDigits;
+ UBool useSpaces;
+ enum { kMaxDecimalDigits = 8 };
+public:
+ FractionalPartSubstitution(int32_t pos,
+ const NFRuleSet* ruleSet,
+ const UnicodeString& description,
+ UErrorCode& status);
+ virtual ~FractionalPartSubstitution();
+
+ virtual UBool operator==(const NFSubstitution& rhs) const;
+
+ virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
+ virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
+ virtual int64_t transformNumber(int64_t /*number*/) const { return 0; }
+ virtual double transformNumber(double number) const { return number - uprv_floor(number); }
+
+ virtual UBool doParse(const UnicodeString& text,
+ ParsePosition& parsePosition,
+ double baseValue,
+ double upperBound,
+ UBool lenientParse,
+ uint32_t nonNumericalExecutedRuleMask,
+ Formattable& result) const;
+
+ virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
+ virtual double calcUpperBound(double /*oldUpperBound*/) const { return 0.0; }
+ virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
+
+public:
+ static UClassID getStaticClassID(void);
+ virtual UClassID getDynamicClassID(void) const;
+};
+
+FractionalPartSubstitution::~FractionalPartSubstitution() {}
+
+class AbsoluteValueSubstitution : public NFSubstitution {
+public:
+ AbsoluteValueSubstitution(int32_t _pos,
+ const NFRuleSet* _ruleSet,
+ const UnicodeString& description,
+ UErrorCode& status)
+ : NFSubstitution(_pos, _ruleSet, description, status) {}
+ virtual ~AbsoluteValueSubstitution();
+
+ virtual int64_t transformNumber(int64_t number) const { return number >= 0 ? number : -number; }
+ virtual double transformNumber(double number) const { return uprv_fabs(number); }
+ virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return -newRuleValue; }
+ virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
+ virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
+
+public:
+ static UClassID getStaticClassID(void);
+ virtual UClassID getDynamicClassID(void) const;
+};
+
+AbsoluteValueSubstitution::~AbsoluteValueSubstitution() {}
+
+class NumeratorSubstitution : public NFSubstitution {
+ double denominator;
+ int64_t ldenominator;
+ UBool withZeros;
+public:
+ static inline UnicodeString fixdesc(const UnicodeString& desc) {
+ if (desc.endsWith(LTLT, 2)) {
+ UnicodeString result(desc, 0, desc.length()-1);
+ return result;
+ }
+ return desc;
+ }
+ NumeratorSubstitution(int32_t _pos,
+ double _denominator,
+ NFRuleSet* _ruleSet,
+ const UnicodeString& description,
+ UErrorCode& status)
+ : NFSubstitution(_pos, _ruleSet, fixdesc(description), status), denominator(_denominator)
+ {
+ ldenominator = util64_fromDouble(denominator);
+ withZeros = description.endsWith(LTLT, 2);
+ }
+ virtual ~NumeratorSubstitution();
+
+ virtual UBool operator==(const NFSubstitution& rhs) const;
+
+ virtual int64_t transformNumber(int64_t number) const { return number * ldenominator; }
+ virtual double transformNumber(double number) const { return uprv_round(number * denominator); }
+
+ virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
+ virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
+ virtual UBool doParse(const UnicodeString& text,
+ ParsePosition& parsePosition,
+ double baseValue,
+ double upperBound,
+ UBool /*lenientParse*/,
+ uint32_t nonNumericalExecutedRuleMask,
+ Formattable& result) const;
+
+ virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue / oldRuleValue; }
+ virtual double calcUpperBound(double /*oldUpperBound*/) const { return denominator; }
+ virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
+private:
+ static const UChar LTLT[2];
+
+public:
+ static UClassID getStaticClassID(void);
+ virtual UClassID getDynamicClassID(void) const;
+};
+
+NumeratorSubstitution::~NumeratorSubstitution() {}
+
NFSubstitution*
NFSubstitution::makeSubstitution(int32_t pos,
const NFRule* rule,
{
// if the description is empty, return a NullSubstitution
if (description.length() == 0) {
- return new NullSubstitution(pos, ruleSet, formatter, description, status);
+ return NULL;
}
switch (description.charAt(0)) {
else if (rule->getBaseValue() == NFRule::kImproperFractionRule
|| rule->getBaseValue() == NFRule::kProperFractionRule
|| rule->getBaseValue() == NFRule::kMasterRule) {
- return new IntegralPartSubstitution(pos, ruleSet, formatter, description, status);
+ return new IntegralPartSubstitution(pos, ruleSet, description, status);
}
// if the rule set containing the rule is a fraction
// rule set, return a NumeratorSubstitution
else if (ruleSet->isFractionRuleSet()) {
return new NumeratorSubstitution(pos, (double)rule->getBaseValue(),
- formatter->getDefaultRuleSet(), formatter, description, status);
+ formatter->getDefaultRuleSet(), description, status);
}
// otherwise, return a MultiplierSubstitution
else {
- return new MultiplierSubstitution(pos, rule->getDivisor(), ruleSet,
- formatter, description, status);
+ return new MultiplierSubstitution(pos, rule, ruleSet,
+ description, status);
}
// if the description begins with '>'...
// if the rule is a negative-number rule, return
// an AbsoluteValueSubstitution
if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
- return new AbsoluteValueSubstitution(pos, ruleSet, formatter, description, status);
+ return new AbsoluteValueSubstitution(pos, ruleSet, description, status);
}
// if the rule is a fraction rule, return a
else if (rule->getBaseValue() == NFRule::kImproperFractionRule
|| rule->getBaseValue() == NFRule::kProperFractionRule
|| rule->getBaseValue() == NFRule::kMasterRule) {
- return new FractionalPartSubstitution(pos, ruleSet, formatter, description, status);
+ return new FractionalPartSubstitution(pos, ruleSet, description, status);
}
// if the rule set owning the rule is a fraction rule set,
// otherwise, return a ModulusSubstitution
else {
- return new ModulusSubstitution(pos, rule->getDivisor(), predecessor,
- ruleSet, formatter, description, status);
+ return new ModulusSubstitution(pos, rule, predecessor,
+ ruleSet, description, status);
}
// if the description begins with '=', always return a
// SameValueSubstitution
case gEquals:
- return new SameValueSubstitution(pos, ruleSet, formatter, description, status);
+ return new SameValueSubstitution(pos, ruleSet, description, status);
// and if it's anything else, throw an exception
default:
NFSubstitution::NFSubstitution(int32_t _pos,
const NFRuleSet* _ruleSet,
- const RuleBasedNumberFormat* formatter,
const UnicodeString& description,
UErrorCode& status)
: pos(_pos), ruleSet(NULL), numberFormat(NULL)
return;
}
- // if the description was just two paired token characters
- // (i.e., "<<" or ">>"), it uses the rule set it belongs to to
- // format its result
if (workingDescription.length() == 0) {
+ // if the description was just two paired token characters
+ // (i.e., "<<" or ">>"), it uses the rule set it belongs to to
+ // format its result
this->ruleSet = _ruleSet;
}
- // if the description contains a rule set name, that's the rule
- // set we use to format the result: get a reference to the
- // names rule set
else if (workingDescription.charAt(0) == gPercent) {
- this->ruleSet = formatter->findRuleSet(workingDescription, status);
+ // if the description contains a rule set name, that's the rule
+ // set we use to format the result: get a reference to the
+ // names rule set
+ this->ruleSet = _ruleSet->getOwner()->findRuleSet(workingDescription, status);
}
- // if the description begins with 0 or #, treat it as a
- // DecimalFormat pattern, and initialize a DecimalFormat with
- // that pattern (then set it to use the DecimalFormatSymbols
- // belonging to our formatter)
else if (workingDescription.charAt(0) == gPound || workingDescription.charAt(0) ==gZero) {
- DecimalFormatSymbols* sym = formatter->getDecimalFormatSymbols();
+ // if the description begins with 0 or #, treat it as a
+ // DecimalFormat pattern, and initialize a DecimalFormat with
+ // that pattern (then set it to use the DecimalFormatSymbols
+ // belonging to our formatter)
+ const DecimalFormatSymbols* sym = _ruleSet->getOwner()->getDecimalFormatSymbols();
if (!sym) {
status = U_MISSING_RESOURCE_ERROR;
return;
}
- this->numberFormat = new DecimalFormat(workingDescription, *sym, status);
+ DecimalFormat *tempNumberFormat = new DecimalFormat(workingDescription, *sym, status);
/* test for NULL */
- if (this->numberFormat == 0) {
+ if (!tempNumberFormat) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
if (U_FAILURE(status)) {
- delete (DecimalFormat*)this->numberFormat;
- this->numberFormat = NULL;
+ delete tempNumberFormat;
return;
}
- // this->numberFormat->setDecimalFormatSymbols(formatter->getDecimalFormatSymbols());
+ this->numberFormat = tempNumberFormat;
}
- // if the description is ">>>", this substitution bypasses the
- // usual rule-search process and always uses the rule that precedes
- // it in its own rule set's rule list (this is used for place-value
- // notations: formats where you want to see a particular part of
- // a number even when it's 0)
else if (workingDescription.charAt(0) == gGreaterThan) {
+ // if the description is ">>>", this substitution bypasses the
+ // usual rule-search process and always uses the rule that precedes
+ // it in its own rule set's rule list (this is used for place-value
+ // notations: formats where you want to see a particular part of
+ // a number even when it's 0)
+
// this causes problems when >>> is used in a frationalPartSubstitution
// this->ruleSet = NULL;
this->ruleSet = _ruleSet;
this->numberFormat = NULL;
}
- // and of the description is none of these things, it's a syntax error
else {
+ // and of the description is none of these things, it's a syntax error
+
// throw new IllegalArgumentException("Illegal substitution syntax");
status = U_PARSE_ERROR;
}
NFSubstitution::~NFSubstitution()
{
- // cast away const
- delete (NumberFormat*)numberFormat; numberFormat = NULL;
+ delete numberFormat;
+ numberFormat = NULL;
}
/**
* @param exponent The exponent of the divisor
*/
void
-NFSubstitution::setDivisor(int32_t /*radix*/, int32_t /*exponent*/, UErrorCode& /*status*/) {
+NFSubstitution::setDivisor(int32_t /*radix*/, int16_t /*exponent*/, UErrorCode& /*status*/) {
// a no-op for all substitutions except multiplier and modulus substitutions
}
+void
+NFSubstitution::setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& /*status*/) {
+ if (numberFormat != NULL) {
+ numberFormat->setDecimalFormatSymbols(newSymbols);
+ }
+}
//-----------------------------------------------------------------------
// boilerplate
//-----------------------------------------------------------------------
-const char NFSubstitution::fgClassID = 0;
-
-UClassID
-NFSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NFSubstitution)
- /**
- * Compares two substitutions for equality
- * @param The substitution to compare this one to
- * @return true if the two substitutions are functionally equivalent
- */
+/**
+ * Compares two substitutions for equality
+ * @param The substitution to compare this one to
+ * @return true if the two substitutions are functionally equivalent
+ */
UBool
NFSubstitution::operator==(const NFSubstitution& rhs) const
{
// compare class and all of the fields all substitutions have
// in common
// this should be called by subclasses before their own equality tests
- return getDynamicClassID() == rhs.getDynamicClassID()
+ return typeid(*this) == typeid(rhs)
&& pos == rhs.pos
&& (ruleSet == NULL) == (rhs.ruleSet == NULL)
// && ruleSet == rhs.ruleSet causes circularity, other checks to make instead?
: (*numberFormat == *rhs.numberFormat));
}
- /**
- * Returns a textual description of the substitution
- * @return A textual description of the substitution. This might
- * not be identical to the description it was created from, but
- * it'll produce the same result.
- */
+/**
+ * Returns a textual description of the substitution
+ * @return A textual description of the substitution. This might
+ * not be identical to the description it was created from, but
+ * it'll produce the same result.
+ */
void
NFSubstitution::toString(UnicodeString& text) const
{
UnicodeString temp;
if (ruleSet != NULL) {
ruleSet->getName(temp);
- } else {
+ } else if (numberFormat != NULL) {
numberFormat->toPattern(temp);
}
text.append(temp);
* position to determine exactly where to insert the new text)
*/
void
-NFSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos) const
+NFSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
{
if (ruleSet != NULL) {
- // perform a transformation on the number that is dependent
+ // Perform a transformation on the number that is dependent
// on the type of substitution this is, then just call its
// rule set's format() method to format the result
- ruleSet->format(transformNumber(number), toInsertInto, _pos + this->pos);
- } else {
- // or perform the transformation on the number (preserving
- // the result's fractional part if the formatter it set
- // to show it), then use that formatter's format() method
- // to format the result
- double numberToFormat = transformNumber((double)number);
- if (numberFormat->getMaximumFractionDigits() == 0) {
- numberToFormat = uprv_floor(numberToFormat);
- }
+ ruleSet->format(transformNumber(number), toInsertInto, _pos + this->pos, recursionCount, status);
+ } else if (numberFormat != NULL) {
+ if (number <= MAX_INT64_IN_DOUBLE) {
+ // or perform the transformation on the number (preserving
+ // the result's fractional part if the formatter it set
+ // to show it), then use that formatter's format() method
+ // to format the result
+ double numberToFormat = transformNumber((double)number);
+ if (numberFormat->getMaximumFractionDigits() == 0) {
+ numberToFormat = uprv_floor(numberToFormat);
+ }
- UnicodeString temp;
- numberFormat->format(numberToFormat, temp);
- toInsertInto.insert(_pos + this->pos, temp);
+ UnicodeString temp;
+ numberFormat->format(numberToFormat, temp, status);
+ toInsertInto.insert(_pos + this->pos, temp);
+ }
+ else {
+ // We have gone beyond double precision. Something has to give.
+ // We're favoring accuracy of the large number over potential rules
+ // that round like a CompactDecimalFormat, which is not a common use case.
+ //
+ // Perform a transformation on the number that is dependent
+ // on the type of substitution this is, then just call its
+ // rule set's format() method to format the result
+ int64_t numberToFormat = transformNumber(number);
+ UnicodeString temp;
+ numberFormat->format(numberToFormat, temp, status);
+ toInsertInto.insert(_pos + this->pos, temp);
+ }
}
}
* position to determine exactly where to insert the new text)
*/
void
-NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const {
+NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const {
// perform a transformation on the number being formatted that
// is dependent on the type of substitution this is
double numberToFormat = transformNumber(number);
+ if (uprv_isInfinite(numberToFormat)) {
+ // This is probably a minus rule. Combine it with an infinite rule.
+ const NFRule *infiniteRule = ruleSet->findDoubleRule(uprv_getInfinity());
+ infiniteRule->doFormat(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
+ return;
+ }
+
// if the result is an integer, from here on out we work in integer
// space (saving time and memory and preserving accuracy)
if (numberToFormat == uprv_floor(numberToFormat) && ruleSet != NULL) {
- ruleSet->format(util64_fromDouble(numberToFormat), toInsertInto, _pos + this->pos);
+ ruleSet->format(util64_fromDouble(numberToFormat), toInsertInto, _pos + this->pos, recursionCount, status);
// if the result isn't an integer, then call either our rule set's
// format() method or our DecimalFormat's format() method to
// format the result
} else {
if (ruleSet != NULL) {
- ruleSet->format(numberToFormat, toInsertInto, _pos + this->pos);
- } else {
+ ruleSet->format(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
+ } else if (numberFormat != NULL) {
UnicodeString temp;
numberFormat->format(numberToFormat, temp);
toInsertInto.insert(_pos + this->pos, temp);
double baseValue,
double upperBound,
UBool lenientParse,
+ uint32_t nonNumericalExecutedRuleMask,
Formattable& result) const
{
#ifdef RBNF_DEBUG
// on), then also try parsing the text using a default-
// constructed NumberFormat
if (ruleSet != NULL) {
- ruleSet->parse(text, parsePosition, upperBound, result);
+ ruleSet->parse(text, parsePosition, upperBound, nonNumericalExecutedRuleMask, result);
if (lenientParse && !ruleSet->isFractionRuleSet() && parsePosition.getIndex() == 0) {
UErrorCode status = U_ZERO_ERROR;
NumberFormat* fmt = NumberFormat::createInstance(status);
}
// ...or use our DecimalFormat to parse the text
- } else {
+ } else if (numberFormat != NULL) {
numberFormat->parse(text, result, parsePosition);
}
}
}
-UBool
-NFSubstitution::isNullSubstitution() const {
- return FALSE;
-}
-
/**
* Returns true if this is a modulus substitution. (We didn't do this
* with instanceof partially because it causes source files to
return FALSE;
}
+ /**
+ * Apple addition
+ * @return true if this is a decimal format-only substitution
+ */
+UBool
+NFSubstitution::isDecimalFormatSubstitutionOnly() const {
+ return (ruleSet == NULL && getNumberFormat() != NULL);
+}
+
+ /**
+ * Apple addition, not currently used
+ * @return true if this substitution uses another ruleSet
+ */
+//UBool
+//NFSubstitution::isRuleSetSubstitutionOnly() const {
+// return (getNumberFormat() == NULL && ruleSet != NULL);
+//}
+
//===================================================================
// SameValueSubstitution
//===================================================================
*/
SameValueSubstitution::SameValueSubstitution(int32_t _pos,
const NFRuleSet* _ruleSet,
- const RuleBasedNumberFormat* formatter,
const UnicodeString& description,
UErrorCode& status)
-: NFSubstitution(_pos, _ruleSet, formatter, description, status)
+: NFSubstitution(_pos, _ruleSet, description, status)
{
- if (description == gEqualsEquals) {
+ if (0 == description.compare(gEqualsEquals, 2)) {
// throw new IllegalArgumentException("== is not a legal token");
status = U_PARSE_ERROR;
}
}
-const char SameValueSubstitution::fgClassID = 0;
-
-UClassID
-SameValueSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
-
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SameValueSubstitution)
//===================================================================
// MultiplierSubstitution
//===================================================================
-const char MultiplierSubstitution::fgClassID = 0;
-
-UClassID
-MultiplierSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MultiplierSubstitution)
UBool MultiplierSubstitution::operator==(const NFSubstitution& rhs) const
{
* regular rule.
*/
ModulusSubstitution::ModulusSubstitution(int32_t _pos,
- double _divisor,
+ const NFRule* rule,
const NFRule* predecessor,
const NFRuleSet* _ruleSet,
- const RuleBasedNumberFormat* formatter,
const UnicodeString& description,
UErrorCode& status)
- : NFSubstitution(_pos, _ruleSet, formatter, description, status)
- , divisor(_divisor)
+ : NFSubstitution(_pos, _ruleSet, description, status)
+ , divisor(rule->getDivisor())
, ruleToUse(NULL)
{
- ldivisor = util64_fromDouble(_divisor);
-
// the owning rule's divisor controls the behavior of this
// substitution: rather than keeping a backpointer to the rule,
// we keep a copy of the divisor
- if (ldivisor == 0) {
+ if (divisor == 0) {
status = U_PARSE_ERROR;
}
- if (description == gGreaterGreaterGreaterThan) {
+ if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
// the >>> token doesn't alter how this substituion calculates the
// values it uses for formatting and parsing, but it changes
// what's done with that value after it's obtained: >>> short-
}
}
-const char ModulusSubstitution::fgClassID = 0;
-
-UClassID
-ModulusSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ModulusSubstitution)
UBool ModulusSubstitution::operator==(const NFSubstitution& rhs) const
{
* @param pos The position of the rule text in toInsertInto
*/
void
-ModulusSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos) const
+ModulusSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
{
// if this isn't a >>> substitution, just use the inherited version
// of this function (which uses either a rule set or a DecimalFormat
// to format its substitution value)
if (ruleToUse == NULL) {
- NFSubstitution::doSubstitution(number, toInsertInto, _pos);
+ NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
// a >>> substitution goes straight to a particular rule to
// format the substitution value
} else {
int64_t numberToFormat = transformNumber(number);
- ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos());
+ ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
}
}
* @param pos The position of the rule text in toInsertInto
*/
void
-ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const
+ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
{
// if this isn't a >>> substitution, just use the inherited version
// of this function (which uses either a rule set or a DecimalFormat
// to format its substitution value)
if (ruleToUse == NULL) {
- NFSubstitution::doSubstitution(number, toInsertInto, _pos);
+ NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
// a >>> substitution goes straight to a particular rule to
// format the substitution value
} else {
double numberToFormat = transformNumber(number);
- ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos());
+ ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
}
}
double baseValue,
double upperBound,
UBool lenientParse,
+ uint32_t nonNumericalExecutedRuleMask,
Formattable& result) const
{
// if this isn't a >>> substitution, we can just use the
// inherited parse() routine to do the parsing
if (ruleToUse == NULL) {
- return NFSubstitution::doParse(text, parsePosition, baseValue, upperBound, lenientParse, result);
+ return NFSubstitution::doParse(text, parsePosition, baseValue, upperBound, lenientParse, nonNumericalExecutedRuleMask, result);
// but if it IS a >>> substitution, we have to do it here: we
// use the specific rule's doParse() method, and then we have to
// do some of the other work of NFRuleSet.parse()
} else {
- ruleToUse->doParse(text, parsePosition, FALSE, upperBound, result);
+ ruleToUse->doParse(text, parsePosition, FALSE, upperBound, nonNumericalExecutedRuleMask, result);
if (parsePosition.getIndex() != 0) {
- double tempResult = result.getDouble();
+ UErrorCode status = U_ZERO_ERROR;
+ double tempResult = result.getDouble(status);
tempResult = composeRuleValue(tempResult, baseValue);
result.setDouble(tempResult);
}
return TRUE;
}
}
+/**
+ * Returns a textual description of the substitution
+ * @return A textual description of the substitution. This might
+ * not be identical to the description it was created from, but
+ * it'll produce the same result.
+ */
+void
+ModulusSubstitution::toString(UnicodeString& text) const
+{
+ // use tokenChar() to get the character at the beginning and
+ // end of the substitutin token. In between them will go
+ // either the name of the rule set it uses, or the pattern of
+ // the DecimalFormat it uses
-
+ if ( ruleToUse != NULL ) { // Must have been a >>> substitution.
+ text.remove();
+ text.append(tokenChar());
+ text.append(tokenChar());
+ text.append(tokenChar());
+ } else { // Otherwise just use the super-class function.
+ NFSubstitution::toString(text);
+ }
+}
//===================================================================
// IntegralPartSubstitution
//===================================================================
-const char IntegralPartSubstitution::fgClassID = 0;
-
-UClassID
-IntegralPartSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution)
//===================================================================
*/
FractionalPartSubstitution::FractionalPartSubstitution(int32_t _pos,
const NFRuleSet* _ruleSet,
- const RuleBasedNumberFormat* formatter,
const UnicodeString& description,
UErrorCode& status)
- : NFSubstitution(_pos, _ruleSet, formatter, description, status)
+ : NFSubstitution(_pos, _ruleSet, description, status)
, byDigits(FALSE)
, useSpaces(TRUE)
{
// akk, ruleSet can change in superclass constructor
- if (description == gGreaterGreaterThan ||
- description == gGreaterGreaterGreaterThan ||
+ if (0 == description.compare(gGreaterGreaterThan, 2) ||
+ 0 == description.compare(gGreaterGreaterGreaterThan, 3) ||
_ruleSet == getRuleSet()) {
byDigits = TRUE;
- if (description == gGreaterGreaterGreaterThan) {
+ if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
useSpaces = FALSE;
}
} else {
* toInsertInto
*/
void
-FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos) const
+FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto,
+ int32_t _pos, int32_t recursionCount, UErrorCode& status) const
{
// if we're not in "byDigits" mode, just use the inherited
// doSubstitution() routine
if (!byDigits) {
- NFSubstitution::doSubstitution(number, toInsertInto, _pos);
+ NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
// if we're in "byDigits" mode, transform the value into an integer
// by moving the decimal point eight places to the right and
// }
DigitList dl;
- dl.set(number, 20, TRUE);
+ dl.set(number);
+ dl.roundFixedPoint(20); // round to 20 fraction digits.
+ dl.reduce(); // Removes any trailing zeros.
UBool pad = FALSE;
- while (dl.fCount > (dl.fDecimalAt <= 0 ? 0 : dl.fDecimalAt)) {
+ for (int32_t didx = dl.getCount()-1; didx>=dl.getDecimalAt(); didx--) {
+ // Loop iterates over fraction digits, starting with the LSD.
+ // include both real digits from the number, and zeros
+ // to the left of the MSD but to the right of the decimal point.
if (pad && useSpaces) {
toInsertInto.insert(_pos + getPos(), gSpace);
} else {
pad = TRUE;
}
- getRuleSet()->format((int64_t)(dl.fDigits[--dl.fCount] - '0'), toInsertInto, _pos + getPos());
- }
- while (dl.fDecimalAt < 0) {
- if (pad && useSpaces) {
- toInsertInto.insert(_pos + getPos(), gSpace);
- } else {
- pad = TRUE;
- }
- getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos());
- ++dl.fDecimalAt;
+ int64_t digit = didx>=0 ? dl.getDigit(didx) - '0' : 0;
+ getRuleSet()->format(digit, toInsertInto, _pos + getPos(), recursionCount, status);
}
if (!pad) {
// hack around lack of precision in digitlist. if we would end up with
// "foo point" make sure we add a " zero" to the end.
- getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos());
+ getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos(), recursionCount, status);
}
}
}
double baseValue,
double /*upperBound*/,
UBool lenientParse,
+ uint32_t nonNumericalExecutedRuleMask,
Formattable& resVal) const
{
// if we're not in byDigits mode, we can just use the inherited
// doParse()
if (!byDigits) {
- return NFSubstitution::doParse(text, parsePosition, baseValue, 0, lenientParse, resVal);
+ return NFSubstitution::doParse(text, parsePosition, baseValue, 0, lenientParse, nonNumericalExecutedRuleMask, resVal);
// if we ARE in byDigits mode, parse the text one digit at a time
// using this substitution's owning rule set (we do this by setting
while (workText.length() > 0 && workPos.getIndex() != 0) {
workPos.setIndex(0);
Formattable temp;
- getRuleSet()->parse(workText, workPos, 10, temp);
+ getRuleSet()->parse(workText, workPos, 10, nonNumericalExecutedRuleMask, temp);
UErrorCode status = U_ZERO_ERROR;
digit = temp.getLong(status);
// digit = temp.getType() == Formattable::kLong ?
}
if (fmt) {
fmt->parse(workText, temp, workPos);
- digit = temp.getLong();
+ digit = temp.getLong(status);
}
}
}
}
delete fmt;
- result = dl.fCount == 0 ? 0 : dl.getDouble();
+ result = dl.getCount() == 0 ? 0 : dl.getDouble();
result = composeRuleValue(result, baseValue);
resVal.setDouble(result);
return TRUE;
((const FractionalPartSubstitution*)&rhs)->byDigits == byDigits;
}
-const char FractionalPartSubstitution::fgClassID = 0;
-
-UClassID
-FractionalPartSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FractionalPartSubstitution)
//===================================================================
// AbsoluteValueSubstitution
//===================================================================
-const char AbsoluteValueSubstitution::fgClassID = 0;
-
-UClassID
-AbsoluteValueSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution)
//===================================================================
// NumeratorSubstitution
//===================================================================
+void
+NumeratorSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t apos, int32_t recursionCount, UErrorCode& status) const {
+ // perform a transformation on the number being formatted that
+ // is dependent on the type of substitution this is
+
+ double numberToFormat = transformNumber(number);
+ int64_t longNF = util64_fromDouble(numberToFormat);
+
+ const NFRuleSet* aruleSet = getRuleSet();
+ if (withZeros && aruleSet != NULL) {
+ // if there are leading zeros in the decimal expansion then emit them
+ int64_t nf =longNF;
+ int32_t len = toInsertInto.length();
+ while ((nf *= 10) < denominator) {
+ toInsertInto.insert(apos + getPos(), gSpace);
+ aruleSet->format((int64_t)0, toInsertInto, apos + getPos(), recursionCount, status);
+ }
+ apos += toInsertInto.length() - len;
+ }
+
+ // if the result is an integer, from here on out we work in integer
+ // space (saving time and memory and preserving accuracy)
+ if (numberToFormat == longNF && aruleSet != NULL) {
+ aruleSet->format(longNF, toInsertInto, apos + getPos(), recursionCount, status);
+
+ // if the result isn't an integer, then call either our rule set's
+ // format() method or our DecimalFormat's format() method to
+ // format the result
+ } else {
+ if (aruleSet != NULL) {
+ aruleSet->format(numberToFormat, toInsertInto, apos + getPos(), recursionCount, status);
+ } else {
+ UnicodeString temp;
+ getNumberFormat()->format(numberToFormat, temp, status);
+ toInsertInto.insert(apos + getPos(), temp);
+ }
+ }
+}
+
+UBool
+NumeratorSubstitution::doParse(const UnicodeString& text,
+ ParsePosition& parsePosition,
+ double baseValue,
+ double upperBound,
+ UBool /*lenientParse*/,
+ uint32_t nonNumericalExecutedRuleMask,
+ Formattable& result) const
+{
+ // we don't have to do anything special to do the parsing here,
+ // but we have to turn lenient parsing off-- if we leave it on,
+ // it SERIOUSLY messes up the algorithm
+
+ // if withZeros is true, we need to count the zeros
+ // and use that to adjust the parse result
+ UErrorCode status = U_ZERO_ERROR;
+ int32_t zeroCount = 0;
+ UnicodeString workText(text);
+
+ if (withZeros) {
+ ParsePosition workPos(1);
+ Formattable temp;
+
+ while (workText.length() > 0 && workPos.getIndex() != 0) {
+ workPos.setIndex(0);
+ getRuleSet()->parse(workText, workPos, 1, nonNumericalExecutedRuleMask, temp); // parse zero or nothing at all
+ if (workPos.getIndex() == 0) {
+ // we failed, either there were no more zeros, or the number was formatted with digits
+ // either way, we're done
+ break;
+ }
+
+ ++zeroCount;
+ parsePosition.setIndex(parsePosition.getIndex() + workPos.getIndex());
+ workText.remove(0, workPos.getIndex());
+ while (workText.length() > 0 && workText.charAt(0) == gSpace) {
+ workText.remove(0, 1);
+ parsePosition.setIndex(parsePosition.getIndex() + 1);
+ }
+ }
+
+ workText = text;
+ workText.remove(0, (int32_t)parsePosition.getIndex());
+ parsePosition.setIndex(0);
+ }
+
+ // we've parsed off the zeros, now let's parse the rest from our current position
+ NFSubstitution::doParse(workText, parsePosition, withZeros ? 1 : baseValue, upperBound, FALSE, nonNumericalExecutedRuleMask, result);
+
+ if (withZeros) {
+ // any base value will do in this case. is there a way to
+ // force this to not bother trying all the base values?
+
+ // compute the 'effective' base and prescale the value down
+ int64_t n = result.getLong(status); // force conversion!
+ int64_t d = 1;
+ int32_t pow = 0;
+ while (d <= n) {
+ d *= 10;
+ ++pow;
+ }
+ // now add the zeros
+ while (zeroCount > 0) {
+ d *= 10;
+ --zeroCount;
+ }
+ // d is now our true denominator
+ result.setDouble((double)n/(double)d);
+ }
+
+ return TRUE;
+}
+
UBool
NumeratorSubstitution::operator==(const NFSubstitution& rhs) const
{
denominator == ((const NumeratorSubstitution*)&rhs)->denominator;
}
-const char NumeratorSubstitution::fgClassID = 0;
-
-UClassID
-NumeratorSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
-
-//===================================================================
-// NullSubstitution
-//===================================================================
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumeratorSubstitution)
-const char NullSubstitution::fgClassID = 0;
-
-UClassID
-NullSubstitution::getDynamicClassID() const {
- return getStaticClassID();
-}
+const UChar NumeratorSubstitution::LTLT[] = { 0x003c, 0x003c };
+
+U_NAMESPACE_END
/* U_HAVE_RBNF */
#endif