Home | History | Annotate | Download | only in i18n
      1 /*
      2 *******************************************************************************
      3 * Copyright (C) 1997-2014, International Business Machines
      4 * Corporation and others. All Rights Reserved.
      5 *******************************************************************************
      6 */
      7 
      8 #ifndef NFRULE_H
      9 #define NFRULE_H
     10 
     11 #include "unicode/rbnf.h"
     12 
     13 #if U_HAVE_RBNF
     14 
     15 #include "unicode/utypes.h"
     16 #include "unicode/uobject.h"
     17 #include "unicode/unistr.h"
     18 #include "putilimp.h"
     19 
     20 U_NAMESPACE_BEGIN
     21 
     22 class FieldPosition;
     23 class Formattable;
     24 class NFRuleList;
     25 class NFRuleSet;
     26 class NFSubstitution;
     27 class ParsePosition;
     28 class PluralFormat;
     29 class RuleBasedNumberFormat;
     30 class UnicodeString;
     31 
     32 class NFRule : public UMemory {
     33 public:
     34 
     35     enum ERuleType {
     36         kNoBase = 0,
     37         kNegativeNumberRule = -1,
     38         kImproperFractionRule = -2,
     39         kProperFractionRule = -3,
     40         kMasterRule = -4,
     41         kOtherRule = -5
     42     };
     43 
     44     static void makeRules(UnicodeString& definition,
     45                           const NFRuleSet* ruleSet,
     46                           const NFRule* predecessor,
     47                           const RuleBasedNumberFormat* rbnf,
     48                           NFRuleList& ruleList,
     49                           UErrorCode& status);
     50 
     51     NFRule(const RuleBasedNumberFormat* rbnf);
     52     ~NFRule();
     53 
     54     UBool operator==(const NFRule& rhs) const;
     55     UBool operator!=(const NFRule& rhs) const { return !operator==(rhs); }
     56 
     57     ERuleType getType() const { return (ERuleType)(baseValue <= kNoBase ? (ERuleType)baseValue : kOtherRule); }
     58     void setType(ERuleType ruleType) { baseValue = (int32_t)ruleType; }
     59 
     60     int64_t getBaseValue() const { return baseValue; }
     61     void setBaseValue(int64_t value, UErrorCode& status);
     62 
     63     double getDivisor() const { return uprv_pow(radix, exponent); }
     64 
     65     void doFormat(int64_t number, UnicodeString& toAppendTo, int32_t pos, UErrorCode& status) const;
     66     void doFormat(double  number, UnicodeString& toAppendTo, int32_t pos, UErrorCode& status) const;
     67 
     68     UBool doParse(const UnicodeString& text,
     69                   ParsePosition& pos,
     70                   UBool isFractional,
     71                   double upperBound,
     72                   Formattable& result) const;
     73 
     74     UBool shouldRollBack(double number) const;
     75 
     76     void _appendRuleText(UnicodeString& result) const;
     77 
     78     int32_t findTextLenient(const UnicodeString& str, const UnicodeString& key,
     79                      int32_t startingAt, int32_t* resultCount) const;
     80 
     81 private:
     82     void parseRuleDescriptor(UnicodeString& descriptor, UErrorCode& status);
     83     void extractSubstitutions(const NFRuleSet* ruleSet, const UnicodeString &ruleText, const NFRule* predecessor, UErrorCode& status);
     84     NFSubstitution* extractSubstitution(const NFRuleSet* ruleSet, const NFRule* predecessor, UErrorCode& status);
     85 
     86     int16_t expectedExponent() const;
     87     int32_t indexOfAny(const UChar* const strings[]) const;
     88     double matchToDelimiter(const UnicodeString& text, int32_t startPos, double baseValue,
     89                             const UnicodeString& delimiter, ParsePosition& pp, const NFSubstitution* sub,
     90                             double upperBound) const;
     91     void stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const;
     92 
     93     int32_t prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const;
     94     UBool allIgnorable(const UnicodeString& str, UErrorCode& status) const;
     95     int32_t findText(const UnicodeString& str, const UnicodeString& key,
     96                      int32_t startingAt, int32_t* resultCount) const;
     97 
     98 private:
     99     int64_t baseValue;
    100     int32_t radix;
    101     int16_t exponent;
    102     UnicodeString ruleText;
    103     NFSubstitution* sub1;
    104     NFSubstitution* sub2;
    105     const RuleBasedNumberFormat* formatter;
    106     const PluralFormat* rulePatternFormat;
    107 
    108     NFRule(const NFRule &other); // forbid copying of this class
    109     NFRule &operator=(const NFRule &other); // forbid copying of this class
    110 };
    111 
    112 U_NAMESPACE_END
    113 
    114 /* U_HAVE_RBNF */
    115 #endif
    116 
    117 // NFRULE_H
    118 #endif
    119 
    120