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
21U_NAMESPACE_BEGIN
22
23class FieldPosition;
24class Formattable;
25class NFRuleList;
26class NFRuleSet;
27class NFSubstitution;
28class ParsePosition;
29class PluralFormat;
30class RuleBasedNumberFormat;
31class UnicodeString;
32
33class NFRule : public UMemory {
34public:
35
36    enum ERuleType {
37        kNoBase = 0,
38        kNegativeNumberRule = -1,
39        kImproperFractionRule = -2,
40        kProperFractionRule = -3,
41        kMasterRule = -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    UBool operator==(const NFRule& rhs) const;
58    UBool operator!=(const NFRule& rhs) const { return !operator==(rhs); }
59
60    ERuleType getType() const { return (ERuleType)(baseValue <= kNoBase ? (ERuleType)baseValue : kOtherRule); }
61    void setType(ERuleType ruleType) { baseValue = (int32_t)ruleType; }
62
63    int64_t getBaseValue() const { return baseValue; }
64    void setBaseValue(int64_t value, UErrorCode& status);
65
66    UChar getDecimalPoint() const { return decimalPoint; }
67
68    int64_t getDivisor() const;
69
70    void doFormat(int64_t number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
71    void doFormat(double  number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
72
73    UBool doParse(const UnicodeString& text,
74                  ParsePosition& pos,
75                  UBool isFractional,
76                  double upperBound,
77                  Formattable& result) const;
78
79    UBool shouldRollBack(int64_t number) const;
80
81    void _appendRuleText(UnicodeString& result) const;
82
83    int32_t findTextLenient(const UnicodeString& str, const UnicodeString& key,
84                     int32_t startingAt, int32_t* resultCount) const;
85
86    void setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& status);
87
88private:
89    void parseRuleDescriptor(UnicodeString& descriptor, UErrorCode& status);
90    void extractSubstitutions(const NFRuleSet* ruleSet, const UnicodeString &ruleText, const NFRule* predecessor, UErrorCode& status);
91    NFSubstitution* extractSubstitution(const NFRuleSet* ruleSet, const NFRule* predecessor, UErrorCode& status);
92
93    int16_t expectedExponent() const;
94    int32_t indexOfAnyRulePrefix() const;
95    double matchToDelimiter(const UnicodeString& text, int32_t startPos, double baseValue,
96                            const UnicodeString& delimiter, ParsePosition& pp, const NFSubstitution* sub,
97                            double upperBound) const;
98    void stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const;
99
100    int32_t prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const;
101    UBool allIgnorable(const UnicodeString& str, UErrorCode& status) const;
102    int32_t findText(const UnicodeString& str, const UnicodeString& key,
103                     int32_t startingAt, int32_t* resultCount) const;
104
105private:
106    int64_t baseValue;
107    int32_t radix;
108    int16_t exponent;
109    UChar decimalPoint;
110    UnicodeString ruleText;
111    NFSubstitution* sub1;
112    NFSubstitution* sub2;
113    const RuleBasedNumberFormat* formatter;
114    const PluralFormat* rulePatternFormat;
115
116    NFRule(const NFRule &other); // forbid copying of this class
117    NFRule &operator=(const NFRule &other); // forbid copying of this class
118};
119
120U_NAMESPACE_END
121
122/* U_HAVE_RBNF */
123#endif
124
125// NFRULE_H
126#endif
127
128