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