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 static const std::set<std::string> SANCTIONED_UNIT({ "acre", "bit", "byte", "celsius", "centimeter", "day", "degree", 46 "fahrenheit", "fluid-ounce", "foot", "gallon", "gigabit", "gigabyte", 47 "gram", "hectare", "hour", "inch", "kilobit", "kilobyte", "kilogram", 48 "kilometer", "liter", "megabit", "megabyte", "meter", "mile", 49 "mile-scandinavian", "millimeter", "milliliter", "millisecond", 50 "minute", "month", "ounce", "percent", "petabyte", "pound", "second", 51 "stone", "terabit", "terabyte", "week", "yard", "year" }); 52 53 class JSNumberFormat : public JSObject { 54 public: 55 CAST_CHECK(JSNumberFormat, IsJSNumberFormat); 56 57 static constexpr size_t LOCALE_OFFSET = JSObject::SIZE; 58 ACCESSORS(Locale, LOCALE_OFFSET, NUMBER_STRING_SYSTEM_OFFSET) 59 ACCESSORS(NumberingSystem, NUMBER_STRING_SYSTEM_OFFSET, CURRENCY_OFFSET) 60 ACCESSORS(Currency, CURRENCY_OFFSET, UNIT_OFFSET) 61 ACCESSORS(Unit, UNIT_OFFSET, MINIMUM_INTEGER_DIGITS_OFFSET) 62 ACCESSORS(MinimumIntegerDigits, MINIMUM_INTEGER_DIGITS_OFFSET, MINIMUM_FRACTION_DIGITS_OFFSET) 63 ACCESSORS(MinimumFractionDigits, MINIMUM_FRACTION_DIGITS_OFFSET, MAXIMUM_FRACTION_DIGITS_OFFSET) 64 ACCESSORS(MaximumFractionDigits, MAXIMUM_FRACTION_DIGITS_OFFSET, MINIMUM_SIGNIFICANT_DIGITS_OFFSET) 65 ACCESSORS(MinimumSignificantDigits, MINIMUM_SIGNIFICANT_DIGITS_OFFSET, MAXIMUM_SIGNIFICANT_DIGITS_OFFSET) 66 ACCESSORS(MaximumSignificantDigits, MAXIMUM_SIGNIFICANT_DIGITS_OFFSET, USER_GROUPING_OFFSET) 67 ACCESSORS(UseGrouping, USER_GROUPING_OFFSET, BOUND_FORMAT_OFFSET) 68 ACCESSORS(BoundFormat, BOUND_FORMAT_OFFSET, ICU_FIELD_OFFSET) 69 ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET) 70 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 71 DEFINE_ALIGN_SIZE(LAST_OFFSET); 72 73 // define BitField 74 static constexpr size_t STYLE_BITS = 3; 75 static constexpr size_t CURRENCY_SIGN_BITS = 2; 76 static constexpr size_t CURRENCY_DISPLAY_BITS = 3; 77 static constexpr size_t UNIT_DISPLAY_BITS = 3; 78 static constexpr size_t SIGN_DISPLAY_BITS = 3; 79 static constexpr size_t COMPACT_DISPLAY_BITS = 2; 80 static constexpr size_t NOTATION_BITS = 3; 81 static constexpr size_t ROUNDING_TYPE_BITS = 3; FIRST_BIT_FIELD(BitField,Style,StyleOption,STYLE_BITS)82 FIRST_BIT_FIELD(BitField, Style, StyleOption, STYLE_BITS) 83 NEXT_BIT_FIELD(BitField, CurrencySign, CurrencySignOption, CURRENCY_SIGN_BITS, Style) 84 NEXT_BIT_FIELD(BitField, CurrencyDisplay, CurrencyDisplayOption, CURRENCY_DISPLAY_BITS, CurrencySign) 85 NEXT_BIT_FIELD(BitField, UnitDisplay, UnitDisplayOption, UNIT_DISPLAY_BITS, CurrencyDisplay) 86 NEXT_BIT_FIELD(BitField, SignDisplay, SignDisplayOption, SIGN_DISPLAY_BITS, UnitDisplay) 87 NEXT_BIT_FIELD(BitField, CompactDisplay, CompactDisplayOption, COMPACT_DISPLAY_BITS, SignDisplay) 88 NEXT_BIT_FIELD(BitField, Notation, NotationOption, NOTATION_BITS, CompactDisplay) 89 NEXT_BIT_FIELD(BitField, RoundingType, RoundingType, ROUNDING_TYPE_BITS, Notation) 90 91 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET) 92 DECL_DUMP() 93 94 icu::number::LocalizedNumberFormatter *GetIcuCallTarget() const 95 { 96 ASSERT(GetIcuField().IsJSNativePointer()); 97 auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer(); 98 return reinterpret_cast<icu::number::LocalizedNumberFormatter *>(result); 99 } 100 FreeIcuNumberformat(void * pointer,void * data)101 static void FreeIcuNumberformat(void *pointer, void *data) 102 { 103 if (pointer == nullptr) { 104 return; 105 } 106 auto icuNumberformat = reinterpret_cast<icu::number::LocalizedNumberFormatter *>(pointer); 107 icuNumberformat->~LocalizedNumberFormatter(); 108 if (data != nullptr) { 109 reinterpret_cast<EcmaVM *>(data)->GetNativeAreaAllocator()->FreeBuffer(icuNumberformat); 110 } 111 } 112 113 // 12.1.2 InitializeNumberFormat ( numberFormat, locales, options ) 114 static void InitializeNumberFormat(JSThread *thread, 115 const JSHandle<JSNumberFormat> &numberFormat, 116 const JSHandle<JSTaggedValue> &locales, 117 const JSHandle<JSTaggedValue> &options, 118 bool forIcuCache = false); 119 120 // 12.1.3 CurrencyDigits ( currency ) 121 static int32_t CurrencyDigits(const icu::UnicodeString ¤cy); 122 123 static icu::number::LocalizedNumberFormatter *GetCachedIcuNumberFormatter(JSThread *thread, 124 const JSHandle<JSTaggedValue> &locales); 125 126 // 12.1.8 FormatNumeric( numberFormat, x ) 127 static JSHandle<JSTaggedValue> FormatNumeric(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat, 128 JSTaggedValue x); 129 static JSHandle<JSTaggedValue> FormatNumeric(JSThread *thread, 130 const icu::number::LocalizedNumberFormatter *icuNumberFormat, 131 JSTaggedValue x); 132 133 // 12.1.9 FormatNumericToParts( numberFormat, x ) 134 static JSHandle<JSArray> FormatNumericToParts(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat, 135 JSTaggedValue x); 136 137 // 12.1.12 UnwrapNumberFormat( nf ) 138 static JSHandle<JSTaggedValue> UnwrapNumberFormat(JSThread *thread, const JSHandle<JSTaggedValue> &nf); 139 140 static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread); 141 static void ResolvedOptions(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat, 142 const JSHandle<JSObject> &options); 143 144 template<typename T> SetICUFormatterDigitOptions(icu::number::LocalizedNumberFormatter & icuNumberformatter,const JSHandle<T> & formatter)145 static icu::number::LocalizedNumberFormatter SetICUFormatterDigitOptions( 146 icu::number::LocalizedNumberFormatter &icuNumberformatter, const JSHandle<T> &formatter) 147 { 148 int minimumIntegerDigits = formatter->GetMinimumIntegerDigits().GetInt(); 149 // Set ICU formatter IntegerWidth to MinimumIntegerDigits 150 icuNumberformatter = 151 icuNumberformatter.integerWidth(icu::number::IntegerWidth::zeroFillTo(minimumIntegerDigits)); 152 153 int minimumSignificantDigits = formatter->GetMinimumSignificantDigits().GetInt(); 154 int maximumSignificantDigits = formatter->GetMaximumSignificantDigits().GetInt(); 155 int minimumFractionDigits = formatter->GetMinimumFractionDigits().GetInt(); 156 int maximumFractionDigits = formatter->GetMaximumFractionDigits().GetInt(); 157 158 // If roundingtype is "compact-rounding" return ICU formatter 159 RoundingType roundingType = formatter->GetRoundingType(); 160 if (roundingType == RoundingType::COMPACTROUNDING) { 161 return icuNumberformatter; 162 } 163 // Else, Set ICU formatter FractionDigits and SignificantDigits 164 // a. Set ICU formatter minFraction, maxFraction to MinimumFractionDigits, MaximumFractionDigits 165 icu::number::Precision precision = 166 icu::number::Precision::minMaxFraction(minimumFractionDigits, maximumFractionDigits); 167 // b. if MinimumSignificantDigits is not 0, 168 // Set ICU formatter minSignificantDigits, maxSignificantDigits to MinimumSignificantDigits, 169 // MaximumSignificantDigits 170 if (minimumSignificantDigits != 0) { 171 precision = 172 icu::number::Precision::minMaxSignificantDigits(minimumSignificantDigits, maximumSignificantDigits); 173 } 174 return icuNumberformatter.precision(precision); 175 } 176 }; 177 } // namespace panda::ecmascript 178 #endif // ECMASCRIPT_JS_NUMBER_FORMAT_H