• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_JS_NUMBER_FORMAT_H
17 #define ECMASCRIPT_JS_NUMBER_FORMAT_H
18 
19 #include "ecmascript/intl/locale_helper.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/js_hclass.h"
23 #include "ecmascript/js_intl.h"
24 #include "ecmascript/js_locale.h"
25 #include "ecmascript/js_object.h"
26 
27 namespace panda::ecmascript {
28 enum class StyleOption : uint8_t { DECIMAL = 0x01, CURRENCY, PERCENT, UNIT, EXCEPTION };
29 
30 enum class CompactDisplayOption : uint8_t { SHORT = 0x01, LONG, EXCEPTION };
31 
32 enum class SignDisplayOption : uint8_t { AUTO = 0x01, ALWAYS, NEVER, EXCEPTZERO, EXCEPTION };
33 
34 enum class CurrencyDisplayOption : uint8_t { CODE = 0x01, SYMBOL, NARROWSYMBOL, NAME, EXCEPTION };
35 
36 enum class CurrencySignOption : uint8_t { STANDARD = 0x01, ACCOUNTING, EXCEPTION };
37 
38 enum class UnitDisplayOption : uint8_t { SHORT = 0x01, NARROW, LONG, EXCEPTION };
39 
40 struct FractionDigitsOption {
41     int32_t mnfdDefault = 0;
42     int32_t mxfdDefault = 0;
43 };
44 
45 class JSNumberFormat : public JSObject {
46 public:
47     static constexpr uint32_t DEFAULT_FRACTION_DIGITS = 2;
48     static constexpr uint32_t PERUNIT_STRING = 5;
49     static const std::vector<StyleOption> STYLE_OPTION;
50     static const std::vector<std::string> STYLE_OPTION_NAME;
51 
52     static const std::vector<CurrencyDisplayOption> CURRENCY_DISPLAY_OPTION;
53     static const std::vector<std::string> CURRENCY_DISPLAY_OPTION_NAME;
54 
55     static const std::vector<CurrencySignOption> CURRENCY_SIGN_OPTION;
56     static const std::vector<std::string> CURRENCY_SIGN_OPTION_NAME;
57 
58     static const std::vector<UnitDisplayOption> UNIT_DISPLAY_OPTION;
59     static const std::vector<std::string> UNIT_DISPLAY_OPTION_NAME;
60 
61     static const std::vector<LocaleMatcherOption> LOCALE_MATCHER_OPTION;
62     static const std::vector<std::string> LOCALE_MATCHER_OPTION_NAME;
63 
64     static const std::vector<NotationOption> NOTATION_OPTION;
65     static const std::vector<std::string> NOTATION_OPTION_NAME;
66 
67     static const std::vector<SignDisplayOption> SIGN_DISPLAY_OPTION;
68     static const std::vector<std::string> SIGN_DISPLAY_OPTION_NAME;
69 
70     static const std::vector<CompactDisplayOption> COMPACT_DISPLAY_OPTION;
71     static const std::vector<std::string> COMPACT_DISPLAY_OPTION_NAME;
72     CAST_CHECK(JSNumberFormat, IsJSNumberFormat);
73 
74     static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
75     ACCESSORS(Locale, LOCALE_OFFSET, NUMBER_STRING_SYSTEM_OFFSET)
76     ACCESSORS(NumberingSystem, NUMBER_STRING_SYSTEM_OFFSET, CURRENCY_OFFSET)
77     ACCESSORS(Currency, CURRENCY_OFFSET, UNIT_OFFSET)
78     ACCESSORS(Unit, UNIT_OFFSET, MINIMUM_INTEGER_DIGITS_OFFSET)
79     ACCESSORS(MinimumIntegerDigits, MINIMUM_INTEGER_DIGITS_OFFSET, MINIMUM_FRACTION_DIGITS_OFFSET)
80     ACCESSORS(MinimumFractionDigits, MINIMUM_FRACTION_DIGITS_OFFSET, MAXIMUM_FRACTION_DIGITS_OFFSET)
81     ACCESSORS(MaximumFractionDigits, MAXIMUM_FRACTION_DIGITS_OFFSET, MINIMUM_SIGNIFICANT_DIGITS_OFFSET)
82     ACCESSORS(MinimumSignificantDigits, MINIMUM_SIGNIFICANT_DIGITS_OFFSET, MAXIMUM_SIGNIFICANT_DIGITS_OFFSET)
83     ACCESSORS(MaximumSignificantDigits, MAXIMUM_SIGNIFICANT_DIGITS_OFFSET, USER_GROUPING_OFFSET)
84     ACCESSORS(UseGrouping, USER_GROUPING_OFFSET, BOUND_FORMAT_OFFSET)
85     ACCESSORS(BoundFormat, BOUND_FORMAT_OFFSET, ICU_FIELD_OFFSET)
86     ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
87     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
88     DEFINE_ALIGN_SIZE(LAST_OFFSET);
89 
90     // define BitField
91     static constexpr size_t STYLE_BITS = 3;
92     static constexpr size_t CURRENCY_SIGN_BITS = 2;
93     static constexpr size_t CURRENCY_DISPLAY_BITS = 3;
94     static constexpr size_t UNIT_DISPLAY_BITS = 3;
95     static constexpr size_t SIGN_DISPLAY_BITS = 3;
96     static constexpr size_t COMPACT_DISPLAY_BITS = 2;
97     static constexpr size_t NOTATION_BITS = 3;
98     static constexpr size_t ROUNDING_TYPE_BITS = 3;
FIRST_BIT_FIELD(BitField,Style,StyleOption,STYLE_BITS)99     FIRST_BIT_FIELD(BitField, Style, StyleOption, STYLE_BITS)
100     NEXT_BIT_FIELD(BitField, CurrencySign, CurrencySignOption, CURRENCY_SIGN_BITS, Style)
101     NEXT_BIT_FIELD(BitField, CurrencyDisplay, CurrencyDisplayOption, CURRENCY_DISPLAY_BITS, CurrencySign)
102     NEXT_BIT_FIELD(BitField, UnitDisplay, UnitDisplayOption, UNIT_DISPLAY_BITS, CurrencyDisplay)
103     NEXT_BIT_FIELD(BitField, SignDisplay, SignDisplayOption, SIGN_DISPLAY_BITS, UnitDisplay)
104     NEXT_BIT_FIELD(BitField, CompactDisplay, CompactDisplayOption, COMPACT_DISPLAY_BITS, SignDisplay)
105     NEXT_BIT_FIELD(BitField, Notation, NotationOption, NOTATION_BITS, CompactDisplay)
106     NEXT_BIT_FIELD(BitField, RoundingType, RoundingType, ROUNDING_TYPE_BITS, Notation)
107 
108     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
109     DECL_DUMP()
110 
111     icu::number::LocalizedNumberFormatter *GetIcuCallTarget() const
112     {
113         ASSERT(GetIcuField().IsJSNativePointer());
114         auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
115         return reinterpret_cast<icu::number::LocalizedNumberFormatter *>(result);
116     }
117 
FreeIcuNumberformat(void * env,void * pointer,void * data)118     static void FreeIcuNumberformat([[maybe_unused]] void *env, void *pointer, void *data)
119     {
120         if (pointer == nullptr) {
121             return;
122         }
123         auto icuNumberformat = reinterpret_cast<icu::number::LocalizedNumberFormatter *>(pointer);
124         icuNumberformat->~LocalizedNumberFormatter();
125         if (data != nullptr) {
126             reinterpret_cast<EcmaVM *>(data)->GetNativeAreaAllocator()->FreeBuffer(icuNumberformat);
127         }
128     }
129 
130     // 12.1.2 InitializeNumberFormat ( numberFormat, locales, options )
131     static void InitializeNumberFormat(JSThread *thread,
132                                        const JSHandle<JSNumberFormat> &numberFormat,
133                                        const JSHandle<JSTaggedValue> &locales,
134                                        const JSHandle<JSTaggedValue> &options,
135                                        bool forIcuCache = false);
136 
137     // 12.1.3 CurrencyDigits ( currency )
138     static int32_t CurrencyDigits(const icu::UnicodeString &currency);
139 
140     static icu::number::LocalizedNumberFormatter *GetCachedIcuNumberFormatter(JSThread *thread,
141                                                                               const JSHandle<JSTaggedValue> &locales);
142 
143     // 12.1.8 FormatNumeric( numberFormat, x )
144     static JSHandle<JSTaggedValue> FormatNumeric(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat,
145                                                  JSTaggedValue x);
146     static JSHandle<JSTaggedValue> FormatNumeric(JSThread *thread,
147                                                  const icu::number::LocalizedNumberFormatter *icuNumberFormat,
148                                                  JSTaggedValue x);
149 
150     // 12.1.9 FormatNumericToParts( numberFormat, x )
151     static JSHandle<JSArray> FormatNumericToParts(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat,
152                                                   JSTaggedValue x);
153 
154     // 12.1.12 UnwrapNumberFormat( nf )
155     static JSHandle<JSTaggedValue> UnwrapNumberFormat(JSThread *thread, const JSHandle<JSTaggedValue> &nf);
156 
157     static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
158     static void ResolvedOptions(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat,
159                                 const JSHandle<JSObject> &options);
160 
161     template<typename T>
SetICUFormatterDigitOptions(icu::number::LocalizedNumberFormatter & icuNumberformatter,const JSHandle<T> & formatter)162     static icu::number::LocalizedNumberFormatter SetICUFormatterDigitOptions(
163         icu::number::LocalizedNumberFormatter &icuNumberformatter, const JSHandle<T> &formatter)
164     {
165         int minimumIntegerDigits = formatter->GetMinimumIntegerDigits().GetInt();
166         // Set ICU formatter IntegerWidth to MinimumIntegerDigits
167         icuNumberformatter =
168             icuNumberformatter.integerWidth(icu::number::IntegerWidth::zeroFillTo(minimumIntegerDigits));
169 
170         int minimumSignificantDigits = formatter->GetMinimumSignificantDigits().GetInt();
171         int maximumSignificantDigits = formatter->GetMaximumSignificantDigits().GetInt();
172         int minimumFractionDigits = formatter->GetMinimumFractionDigits().GetInt();
173         int maximumFractionDigits = formatter->GetMaximumFractionDigits().GetInt();
174 
175         // If roundingtype is "compact-rounding" return ICU formatter
176         RoundingType roundingType = formatter->GetRoundingType();
177         if (roundingType == RoundingType::COMPACTROUNDING) {
178             return icuNumberformatter;
179         }
180         // Else, Set ICU formatter FractionDigits and SignificantDigits
181         //   a. Set ICU formatter minFraction, maxFraction to MinimumFractionDigits, MaximumFractionDigits
182         icu::number::Precision precision =
183             icu::number::Precision::minMaxFraction(minimumFractionDigits, maximumFractionDigits);
184         //   b. if MinimumSignificantDigits is not 0,
185         //      Set ICU formatter minSignificantDigits, maxSignificantDigits to MinimumSignificantDigits,
186         //      MaximumSignificantDigits
187         if (minimumSignificantDigits != 0) {
188             precision =
189                 icu::number::Precision::minMaxSignificantDigits(minimumSignificantDigits, maximumSignificantDigits);
190         }
191         return icuNumberformatter.precision(precision);
192     }
193 };
194 }  // namespace panda::ecmascript
195 #endif  // ECMASCRIPT_JS_NUMBER_FORMAT_H