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