• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 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 __NUMBER_COMPACT_H__
8 #define __NUMBER_COMPACT_H__
9 
10 #include "standardplural.h"
11 #include "number_types.h"
12 #include "unicode/unum.h"
13 #include "uvector.h"
14 #include "resource.h"
15 #include "number_patternmodifier.h"
16 
17 U_NAMESPACE_BEGIN namespace number {
18 namespace impl {
19 
20 static const int32_t COMPACT_MAX_DIGITS = 15;
21 
22 class CompactData : public MultiplierProducer {
23   public:
24     CompactData();
25 
26     void populate(const Locale &locale, const char *nsName, CompactStyle compactStyle,
27                   CompactType compactType, UErrorCode &status);
28 
29     int32_t getMultiplier(int32_t magnitude) const U_OVERRIDE;
30 
31     const UChar *getPattern(
32         int32_t magnitude,
33         const PluralRules *rules,
34         const DecimalQuantity &dq) const;
35 
36     void getUniquePatterns(UVector &output, UErrorCode &status) const;
37 
38   private:
39     const UChar *patterns[(COMPACT_MAX_DIGITS + 1) * StandardPlural::COUNT];
40     int8_t multipliers[COMPACT_MAX_DIGITS + 1];
41     int8_t largestMagnitude;
42     UBool isEmpty;
43 
44     class CompactDataSink : public ResourceSink {
45       public:
CompactDataSink(CompactData & data)46         explicit CompactDataSink(CompactData &data) : data(data) {}
47 
48         void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &status) U_OVERRIDE;
49 
50       private:
51         CompactData &data;
52     };
53 };
54 
55 struct CompactModInfo {
56     const ImmutablePatternModifier *mod;
57     const UChar* patternString;
58 };
59 
60 class CompactHandler : public MicroPropsGenerator, public UMemory {
61   public:
62     CompactHandler(
63             CompactStyle compactStyle,
64             const Locale &locale,
65             const char *nsName,
66             CompactType compactType,
67             const PluralRules *rules,
68             MutablePatternModifier *buildReference,
69             bool safe,
70             const MicroPropsGenerator *parent,
71             UErrorCode &status);
72 
73     ~CompactHandler() U_OVERRIDE;
74 
75     void
76     processQuantity(DecimalQuantity &quantity, MicroProps &micros, UErrorCode &status) const U_OVERRIDE;
77 
78   private:
79     const PluralRules *rules;
80     const MicroPropsGenerator *parent;
81     // Initial capacity of 12 for 0K, 00K, 000K, ...M, ...B, and ...T
82     MaybeStackArray<CompactModInfo, 12> precomputedMods;
83     int32_t precomputedModsLength = 0;
84     CompactData data;
85     ParsedPatternInfo unsafePatternInfo;
86     MutablePatternModifier* unsafePatternModifier;
87     UBool safe;
88 
89     /** Used by the safe code path */
90     void precomputeAllModifiers(MutablePatternModifier &buildReference, UErrorCode &status);
91 };
92 
93 
94 } // namespace impl
95 } // namespace number
96 U_NAMESPACE_END
97 
98 #endif //__NUMBER_COMPACT_H__
99 
100 #endif /* #if !UCONFIG_NO_FORMATTING */
101