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 "formatted_string_builder.h"
17 #include "formattedval_impl.h"
18 
19 U_NAMESPACE_BEGIN namespace number {
20 namespace impl {
21 
22 
23 /**
24  * Class similar to UFormattedNumberData.
25  *
26  * Has incomplete magic number logic that will need to be finished
27  * if this is to be exposed as C API in the future.
28  *
29  * Possible magic number: 0x46445200
30  * Reads in ASCII as "FDR" (FormatteDnumberRange with room at the end)
31  */
32 class UFormattedNumberRangeData : public FormattedValueStringBuilderImpl {
33 public:
UFormattedNumberRangeData()34     UFormattedNumberRangeData() : FormattedValueStringBuilderImpl(0) {}
35     virtual ~UFormattedNumberRangeData();
36 
37     DecimalQuantity quantity1;
38     DecimalQuantity quantity2;
39     UNumberRangeIdentityResult identityResult = UNUM_IDENTITY_RESULT_COUNT;
40 };
41 
42 
43 class StandardPluralRanges : public UMemory {
44   public:
45     void initialize(const Locale& locale, UErrorCode& status);
46     StandardPlural::Form resolve(StandardPlural::Form first, StandardPlural::Form second) const;
47 
48     /** Used for data loading. */
49     void addPluralRange(
50         StandardPlural::Form first,
51         StandardPlural::Form second,
52         StandardPlural::Form result);
53 
54     /** Used for data loading. */
55     void setCapacity(int32_t length);
56 
57   private:
58     struct StandardPluralRangeTriple {
59         StandardPlural::Form first;
60         StandardPlural::Form second;
61         StandardPlural::Form result;
62     };
63 
64     // TODO: An array is simple here, but it results in linear lookup time.
65     // Certain locales have 20-30 entries in this list.
66     // Consider changing to a smarter data structure.
67     typedef MaybeStackArray<StandardPluralRangeTriple, 3> PluralRangeTriples;
68     PluralRangeTriples fTriples;
69     int32_t fTriplesLen = 0;
70 };
71 
72 
73 class NumberRangeFormatterImpl : public UMemory {
74   public:
75     NumberRangeFormatterImpl(const RangeMacroProps& macros, UErrorCode& status);
76 
77     void format(UFormattedNumberRangeData& data, bool equalBeforeRounding, UErrorCode& status) const;
78 
79   private:
80     NumberFormatterImpl formatterImpl1;
81     NumberFormatterImpl formatterImpl2;
82     bool fSameFormatters;
83 
84     UNumberRangeCollapse fCollapse;
85     UNumberRangeIdentityFallback fIdentityFallback;
86 
87     SimpleFormatter fRangeFormatter;
88     SimpleModifier fApproximatelyModifier;
89 
90     StandardPluralRanges fPluralRanges;
91 
92     void formatSingleValue(UFormattedNumberRangeData& data,
93                            MicroProps& micros1, MicroProps& micros2,
94                            UErrorCode& status) const;
95 
96     void formatApproximately(UFormattedNumberRangeData& data,
97                              MicroProps& micros1, MicroProps& micros2,
98                              UErrorCode& status) const;
99 
100     void formatRange(UFormattedNumberRangeData& data,
101                      MicroProps& micros1, MicroProps& micros2,
102                      UErrorCode& status) const;
103 
104     const Modifier& resolveModifierPlurals(const Modifier& first, const Modifier& second) const;
105 };
106 
107 
108 } // namespace impl
109 } // namespace number
110 U_NAMESPACE_END
111 
112 #endif //__SOURCE_NUMRANGE_TYPES_H__
113 #endif /* #if !UCONFIG_NO_FORMATTING */
114