| 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(int32_t magnitude, StandardPlural::Form plural) const; |
| 32 | |
| 33 | void getUniquePatterns(UVector &output, UErrorCode &status) const; |
| 34 | |
| 35 | private: |
| 36 | const UChar *patterns[(COMPACT_MAX_DIGITS + 1) * StandardPlural::COUNT]; |
| 37 | int8_t multipliers[COMPACT_MAX_DIGITS + 1]; |
| 38 | int8_t largestMagnitude; |
| 39 | UBool isEmpty; |
| 40 | |
| 41 | class CompactDataSink : public ResourceSink { |
| 42 | public: |
| 43 | explicit CompactDataSink(CompactData &data) : data(data) {} |
| 44 | |
| 45 | void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &status) U_OVERRIDE; |
| 46 | |
| 47 | private: |
| 48 | CompactData &data; |
| 49 | }; |
| 50 | }; |
| 51 | |
| 52 | struct CompactModInfo { |
| 53 | const ImmutablePatternModifier *mod; |
| 54 | const UChar* patternString; |
| 55 | }; |
| 56 | |
| 57 | class CompactHandler : public MicroPropsGenerator, public UMemory { |
| 58 | public: |
| 59 | CompactHandler( |
| 60 | CompactStyle compactStyle, |
| 61 | const Locale &locale, |
| 62 | const char *nsName, |
| 63 | CompactType compactType, |
| 64 | const PluralRules *rules, |
| 65 | MutablePatternModifier *buildReference, |
| 66 | bool safe, |
| 67 | const MicroPropsGenerator *parent, |
| 68 | UErrorCode &status); |
| 69 | |
| 70 | ~CompactHandler() U_OVERRIDE; |
| 71 | |
| 72 | void |
| 73 | processQuantity(DecimalQuantity &quantity, MicroProps µs, UErrorCode &status) const U_OVERRIDE; |
| 74 | |
| 75 | private: |
| 76 | const PluralRules *rules; |
| 77 | const MicroPropsGenerator *parent; |
| 78 | // Initial capacity of 12 for 0K, 00K, 000K, ...M, ...B, and ...T |
| 79 | MaybeStackArray<CompactModInfo, 12> precomputedMods; |
| 80 | int32_t precomputedModsLength = 0; |
| 81 | CompactData data; |
| 82 | ParsedPatternInfo unsafePatternInfo; |
| 83 | MutablePatternModifier* unsafePatternModifier; |
| 84 | UBool safe; |
| 85 | |
| 86 | /** Used by the safe code path */ |
| 87 | void precomputeAllModifiers(MutablePatternModifier &buildReference, UErrorCode &status); |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | } // namespace impl |
| 92 | } // namespace number |
| 93 | U_NAMESPACE_END |
| 94 | |
| 95 | #endif //__NUMBER_COMPACT_H__ |
| 96 | |
| 97 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 98 | |