• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2015, 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         kInfinityRule = -5,
42         kNaNRule = -6,
43         kOtherRule = -7
44     };
45 
46     static void makeRules(UnicodeString& definition,
47                           NFRuleSet* ruleSet,
48                           const NFRule* predecessor,
49                           const RuleBasedNumberFormat* rbnf,
50                           NFRuleList& ruleList,
51                           UErrorCode& status);
52 
53     NFRule(const RuleBasedNumberFormat* rbnf, const UnicodeString &ruleText, UErrorCode &status);
54     ~NFRule();
55 
56     UBool operator==(const NFRule& rhs) const;
57     UBool operator!=(const NFRule& rhs) const { return !operator==(rhs); }
58 
getType()59     ERuleType getType() const { return (ERuleType)(baseValue <= kNoBase ? (ERuleType)baseValue : kOtherRule); }
setType(ERuleType ruleType)60     void setType(ERuleType ruleType) { baseValue = (int32_t)ruleType; }
61 
getBaseValue()62     int64_t getBaseValue() const { return baseValue; }
63     void setBaseValue(int64_t value, UErrorCode& status);
64 
getDecimalPoint()65     UChar getDecimalPoint() const { return decimalPoint; }
66 
getDivisor()67     double getDivisor() const { return uprv_pow(radix, exponent); }
68 
69     void doFormat(int64_t number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
70     void doFormat(double  number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
71 
72     UBool doParse(const UnicodeString& text,
73                   ParsePosition& pos,
74                   UBool isFractional,
75                   double upperBound,
76                   Formattable& result) const;
77 
78     UBool shouldRollBack(double number) const;
79 
80     void _appendRuleText(UnicodeString& result) const;
81 
82     int32_t findTextLenient(const UnicodeString& str, const UnicodeString& key,
83                      int32_t startingAt, int32_t* resultCount) const;
84 
85     void setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& status);
86 
87 private:
88     void parseRuleDescriptor(UnicodeString& descriptor, UErrorCode& status);
89     void extractSubstitutions(const NFRuleSet* ruleSet, const UnicodeString &ruleText, const NFRule* predecessor, UErrorCode& status);
90     NFSubstitution* extractSubstitution(const NFRuleSet* ruleSet, const NFRule* predecessor, UErrorCode& status);
91 
92     int16_t expectedExponent() const;
93     int32_t indexOfAnyRulePrefix() const;
94     double matchToDelimiter(const UnicodeString& text, int32_t startPos, double baseValue,
95                             const UnicodeString& delimiter, ParsePosition& pp, const NFSubstitution* sub,
96                             double upperBound) const;
97     void stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const;
98 
99     int32_t prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const;
100     UBool allIgnorable(const UnicodeString& str, UErrorCode& status) const;
101     int32_t findText(const UnicodeString& str, const UnicodeString& key,
102                      int32_t startingAt, int32_t* resultCount) const;
103 
104 private:
105     int64_t baseValue;
106     int32_t radix;
107     int16_t exponent;
108     UChar decimalPoint;
109     UnicodeString ruleText;
110     NFSubstitution* sub1;
111     NFSubstitution* sub2;
112     const RuleBasedNumberFormat* formatter;
113     const PluralFormat* rulePatternFormat;
114 
115     NFRule(const NFRule &other); // forbid copying of this class
116     NFRule &operator=(const NFRule &other); // forbid copying of this class
117 };
118 
119 U_NAMESPACE_END
120 
121 /* U_HAVE_RBNF */
122 #endif
123 
124 // NFRULE_H
125 #endif
126 
127