]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/nfrule.h
ICU-551.30.tar.gz
[apple/icu.git] / icuSources / i18n / nfrule.h
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,
73 UBool isDecimFmtParseable=TRUE) const;
74
75 UBool shouldRollBack(double number) const;
76
77 void _appendRuleText(UnicodeString& result) const;
78
79 int32_t findTextLenient(const UnicodeString& str, const UnicodeString& key,
80 int32_t startingAt, int32_t* resultCount) const;
81
82 private:
83 void parseRuleDescriptor(UnicodeString& descriptor, UErrorCode& status);
84 void extractSubstitutions(const NFRuleSet* ruleSet, const UnicodeString &ruleText, const NFRule* predecessor, UErrorCode& status);
85 NFSubstitution* extractSubstitution(const NFRuleSet* ruleSet, const NFRule* predecessor, UErrorCode& status);
86
87 int16_t expectedExponent() const;
88 int32_t indexOfAny(const UChar* const strings[]) const;
89 double matchToDelimiter(const UnicodeString& text, int32_t startPos, double baseValue,
90 const UnicodeString& delimiter, ParsePosition& pp, const NFSubstitution* sub,
91 double upperBound) const;
92 void stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const;
93
94 int32_t prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const;
95 UBool allIgnorable(const UnicodeString& str, UErrorCode& status) const;
96 int32_t findText(const UnicodeString& str, const UnicodeString& key,
97 int32_t startingAt, int32_t* resultCount) const;
98
99 private:
100 int64_t baseValue;
101 int32_t radix;
102 int16_t exponent;
103 UnicodeString ruleText;
104 NFSubstitution* sub1;
105 NFSubstitution* sub2;
106 const RuleBasedNumberFormat* formatter;
107 const PluralFormat* rulePatternFormat;
108
109 NFRule(const NFRule &other); // forbid copying of this class
110 NFRule &operator=(const NFRule &other); // forbid copying of this class
111 };
112
113 U_NAMESPACE_END
114
115 /* U_HAVE_RBNF */
116 #endif
117
118 // NFRULE_H
119 #endif
120