• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __SOURCE_NUMRANGE_TYPES_H__
8 #define __SOURCE_NUMRANGE_TYPES_H__
9 
10 #include "unicode/numberformatter.h"
11 #include "unicode/numberrangeformatter.h"
12 #include "unicode/simpleformatter.h"
13 #include "number_types.h"
14 #include "number_decimalquantity.h"
15 #include "number_formatimpl.h"
16 #include "number_stringbuilder.h"
17 
18 U_NAMESPACE_BEGIN namespace number {
19 namespace impl {
20 
21 
22 /**
23  * Class similar to UFormattedNumberData.
24  *
25  * Has incomplete magic number logic that will need to be finished
26  * if this is to be exposed as C API in the future.
27  */
28 struct UFormattedNumberRangeData : public UMemory {
29     // The magic number to identify incoming objects.
30     // Reads in ASCII as "FDR" (FormatteDnumberRange with room at the end)
31     static constexpr int32_t kMagic = 0x46445200;
32 
33     // Data members:
34     int32_t fMagic = kMagic;
35     DecimalQuantity quantity1;
36     DecimalQuantity quantity2;
37     NumberStringBuilder string;
38     UNumberRangeIdentityResult identityResult = UNUM_IDENTITY_RESULT_COUNT;
39 
40     // No C conversion methods (no C API yet)
41 };
42 
43 
44 class StandardPluralRanges : public UMemory {
45   public:
46     void initialize(const Locale& locale, UErrorCode& status);
47     StandardPlural::Form resolve(StandardPlural::Form first, StandardPlural::Form second) const;
48 
49     /** Used for data loading. */
50     void addPluralRange(
51         StandardPlural::Form first,
52         StandardPlural::Form second,
53         StandardPlural::Form result);
54 
55     /** Used for data loading. */
56     void setCapacity(int32_t length);
57 
58   private:
59     struct StandardPluralRangeTriple {
60         StandardPlural::Form first;
61         StandardPlural::Form second;
62         StandardPlural::Form result;
63     };
64 
65     // TODO: An array is simple here, but it results in linear lookup time.
66     // Certain locales have 20-30 entries in this list.
67     // Consider changing to a smarter data structure.
68     typedef MaybeStackArray<StandardPluralRangeTriple, 3> PluralRangeTriples;
69     PluralRangeTriples fTriples;
70     int32_t fTriplesLen = 0;
71 };
72 
73 
74 class NumberRangeFormatterImpl : public UMemory {
75   public:
76     NumberRangeFormatterImpl(const RangeMacroProps& macros, UErrorCode& status);
77 
78     void format(UFormattedNumberRangeData& data, bool equalBeforeRounding, UErrorCode& status) const;
79 
80   private:
81     NumberFormatterImpl formatterImpl1;
82     NumberFormatterImpl formatterImpl2;
83     bool fSameFormatters;
84 
85     UNumberRangeCollapse fCollapse;
86     UNumberRangeIdentityFallback fIdentityFallback;
87 
88     SimpleFormatter fRangeFormatter;
89     SimpleModifier fApproximatelyModifier;
90 
91     StandardPluralRanges fPluralRanges;
92 
93     void formatSingleValue(UFormattedNumberRangeData& data,
94                            MicroProps& micros1, MicroProps& micros2,
95                            UErrorCode& status) const;
96 
97     void formatApproximately(UFormattedNumberRangeData& data,
98                              MicroProps& micros1, MicroProps& micros2,
99                              UErrorCode& status) const;
100 
101     void formatRange(UFormattedNumberRangeData& data,
102                      MicroProps& micros1, MicroProps& micros2,
103                      UErrorCode& status) const;
104 
105     const Modifier& resolveModifierPlurals(const Modifier& first, const Modifier& second) const;
106 };
107 
108 
109 } // namespace impl
110 } // namespace number
111 U_NAMESPACE_END
112 
113 #endif //__SOURCE_NUMRANGE_TYPES_H__
114 #endif /* #if !UCONFIG_NO_FORMATTING */
115