1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*   Copyright (C) 1997-2009, International Business Machines
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   file name:  nfrs.h
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   encoding:   US-ASCII
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   tab size:   8 (not used)
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   indentation:4
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Modification history
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Date        Name      Comments
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 10/11/2001  Doug      Ported from ICU4J
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef NFRS_H
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define NFRS_H
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uobject.h"
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/rbnf.h"
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if U_HAVE_RBNF
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h"
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/umisc.h"
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "nfrlist.h"
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruclass NFRuleSet : public UMemory {
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru public:
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRuleSet(UnicodeString* descriptions, int32_t index, UErrorCode& status);
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  void parseRules(UnicodeString& rules, const RuleBasedNumberFormat* owner, UErrorCode& status);
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  void makeIntoFractionRuleSet() { fIsFractionRuleSet = TRUE; }
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  ~NFRuleSet();
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool operator==(const NFRuleSet& rhs) const;
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool operator!=(const NFRuleSet& rhs) const { return !operator==(rhs); }
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool isPublic() const { return fIsPublic; }
4385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
4485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  UBool isParseable() const {
4585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho      UnicodeString prefixpart = UNICODE_STRING_SIMPLE("-prefixpart");
4685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho      UnicodeString postfix = UNICODE_STRING_SIMPLE("-postfix");
4785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho      UnicodeString postfx = UNICODE_STRING_SIMPLE("-postfx");
4885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
4985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho      return ( name.indexOf(prefixpart) == -1 && name.indexOf(postfix) == -1 && name.indexOf(postfx) == -1 );
5085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  }
5185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool isFractionRuleSet() const { return fIsFractionRuleSet; }
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  void  getName(UnicodeString& result) const { result.setTo(name); }
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool isNamed(const UnicodeString& _name) const { return this->name == _name; }
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  void  format(int64_t number, UnicodeString& toAppendTo, int32_t pos) const;
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  void  format(double number, UnicodeString& toAppendTo, int32_t pos) const;
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool parse(const UnicodeString& text, ParsePosition& pos, double upperBound, Formattable& result) const;
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  void appendRules(UnicodeString& result) const; // toString
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru private:
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRule * findNormalRule(int64_t number) const;
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRule * findDoubleRule(double number) const;
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRule * findFractionRuleSetRule(double number) const;
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru private:
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UnicodeString name;
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRuleList rules;
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRule *negativeNumberRule;
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRule *fractionRules[3];
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool fIsFractionRuleSet;
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  UBool fIsPublic;
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  int32_t fRecursionCount;
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRuleSet(const NFRuleSet &other); // forbid copying of this class
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  NFRuleSet &operator=(const NFRuleSet &other); // forbid copying of this class
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// utilities from old llong.h
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// convert mantissa portion of double to int64
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint64_t util64_fromDouble(double d);
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// raise radix to the power exponent, only non-negative exponents
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint64_t util64_pow(int32_t radix, uint32_t exponent);
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// convert n to digit string in buffer, return length of string
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuint32_t util64_tou(int64_t n, UChar* buffer, uint32_t buflen, uint32_t radix = 10, UBool raw = FALSE);
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifdef RBNF_DEBUG
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint64_t util64_utoi(const UChar* str, uint32_t radix = 10);
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuint32_t util64_toa(int64_t n, char* buffer, uint32_t buflen, uint32_t radix = 10, UBool raw = FALSE);
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint64_t util64_atoi(const char* str, uint32_t radix);
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* U_HAVE_RBNF */
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// NFRS_H
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
107