• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 1997-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 */
9 
10 #ifndef NFRULE_H
11 #define NFRULE_H
12 
13 #include "unicode/rbnf.h"
14 
15 #if U_HAVE_RBNF
16 
17 #include "unicode/utypes.h"
18 #include "unicode/uobject.h"
19 #include "unicode/unistr.h"
20 
21 U_NAMESPACE_BEGIN
22 
23 class FieldPosition;
24 class Formattable;
25 class NFRuleList;
26 class NFRuleSet;
27 class NFSubstitution;
28 class ParsePosition;
29 class PluralFormat;
30 class RuleBasedNumberFormat;
31 class UnicodeString;
32 
33 class NFRule : public UMemory {
34 public:
35 
36     enum ERuleType {
37         kNoBase = 0,
38         kNegativeNumberRule = -1,
39         kImproperFractionRule = -2,
40         kProperFractionRule = -3,
41         kDefaultRule = -4,
42         kInfinityRule = -5,
43         kNaNRule = -6,
44         kOtherRule = -7
45     };
46 
47     static void makeRules(UnicodeString& definition,
48                           NFRuleSet* ruleSet,
49                           const NFRule* predecessor,
50                           const RuleBasedNumberFormat* rbnf,
51                           NFRuleList& ruleList,
52                           UErrorCode& status);
53 
54     NFRule(const RuleBasedNumberFormat* rbnf, const UnicodeString &ruleText, UErrorCode &status);
55     ~NFRule();
56 
57     bool operator==(const NFRule& rhs) const;
58     bool operator!=(const NFRule& rhs) const { return !operator==(rhs); }
59 
getType()60     ERuleType getType() const { return (baseValue <= kNoBase ? static_cast<ERuleType>(baseValue) : kOtherRule); }
setType(ERuleType ruleType)61     void setType(ERuleType ruleType) { baseValue = static_cast<int32_t>(ruleType); }
62 
getBaseValue()63     int64_t getBaseValue() const { return baseValue; }
64     void setBaseValue(int64_t value, UErrorCode& status);
65 
getDecimalPoint()66     char16_t getDecimalPoint() const { return decimalPoint; }
67 
68     int64_t getDivisor() const;
69 
70     bool hasModulusSubstitution() const;
71 
72     void doFormat(int64_t number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
73     void doFormat(double  number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
74 
75     UBool doParse(const UnicodeString& text,
76                   ParsePosition& pos,
77                   UBool isFractional,
78                   double upperBound,
79                   uint32_t nonNumericalExecutedRuleMask,
80                   int32_t recursionCount,
81                   Formattable& result) const;
82 
83     UBool shouldRollBack(int64_t number) const;
84 
85     void _appendRuleText(UnicodeString& result) const;
86 
87     int32_t findTextLenient(const UnicodeString& str, const UnicodeString& key,
88                      int32_t startingAt, int32_t* resultCount) const;
89 
90     void setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& status);
91 
92 private:
93     void parseRuleDescriptor(UnicodeString& descriptor, UErrorCode& status);
94     void extractSubstitutions(const NFRuleSet* ruleSet, const UnicodeString &ruleText, const NFRule* predecessor, UErrorCode& status);
95     NFSubstitution* extractSubstitution(const NFRuleSet* ruleSet, const NFRule* predecessor, UErrorCode& status);
96 
97     int16_t expectedExponent() const;
98     int32_t indexOfAnyRulePrefix() const;
99     double matchToDelimiter(const UnicodeString& text, int32_t startPos, double baseValue,
100                             const UnicodeString& delimiter, ParsePosition& pp, const NFSubstitution* sub,
101                             uint32_t nonNumericalExecutedRuleMask,
102                             int32_t recursionCount,
103                             double upperBound) const;
104     void stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const;
105 
106     int32_t prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const;
107     UBool allIgnorable(const UnicodeString& str, UErrorCode& status) const;
108     int32_t findText(const UnicodeString& str, const UnicodeString& key,
109                      int32_t startingAt, int32_t* resultCount) const;
110 
111 private:
112     int64_t baseValue;
113     int32_t radix;
114     int16_t exponent;
115     char16_t decimalPoint;
116     UnicodeString fRuleText;
117     NFSubstitution* sub1;
118     NFSubstitution* sub2;
119     const RuleBasedNumberFormat* formatter;
120     const PluralFormat* rulePatternFormat;
121 
122     NFRule(const NFRule &other); // forbid copying of this class
123     NFRule &operator=(const NFRule &other); // forbid copying of this class
124 
125     // TODO: temporary hack to allow MultiplierSubstitution to get to formatter's rounding mode
126     friend class MultiplierSubstitution;
127 };
128 
129 U_NAMESPACE_END
130 
131 /* U_HAVE_RBNF */
132 #endif
133 
134 // NFRULE_H
135 #endif
136 
137