14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JS_NUMBER_FORMAT_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_NUMBER_FORMAT_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/intl/locale_helper.h"
204514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_intl.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_locale.h"
254514f5e3Sopenharmony_ci#include "ecmascript/js_object.h"
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_cinamespace panda::ecmascript {
284514f5e3Sopenharmony_cienum class StyleOption : uint8_t { DECIMAL = 0x01, CURRENCY, PERCENT, UNIT, EXCEPTION };
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_cienum class CompactDisplayOption : uint8_t { SHORT = 0x01, LONG, EXCEPTION };
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_cienum class SignDisplayOption : uint8_t { AUTO = 0x01, ALWAYS, NEVER, EXCEPTZERO, EXCEPTION };
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_cienum class CurrencyDisplayOption : uint8_t { CODE = 0x01, SYMBOL, NARROWSYMBOL, NAME, EXCEPTION };
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_cienum class CurrencySignOption : uint8_t { STANDARD = 0x01, ACCOUNTING, EXCEPTION };
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_cienum class UnitDisplayOption : uint8_t { SHORT = 0x01, NARROW, LONG, EXCEPTION };
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_cistruct FractionDigitsOption {
414514f5e3Sopenharmony_ci    int32_t mnfdDefault = 0;
424514f5e3Sopenharmony_ci    int32_t mxfdDefault = 0;
434514f5e3Sopenharmony_ci};
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ciclass JSNumberFormat : public JSObject {
464514f5e3Sopenharmony_cipublic:
474514f5e3Sopenharmony_ci    static constexpr uint32_t DEFAULT_FRACTION_DIGITS = 2;
484514f5e3Sopenharmony_ci    static constexpr uint32_t PERUNIT_STRING = 5;
494514f5e3Sopenharmony_ci    static const std::vector<StyleOption> STYLE_OPTION;
504514f5e3Sopenharmony_ci    static const std::vector<std::string> STYLE_OPTION_NAME;
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    static const std::vector<CurrencyDisplayOption> CURRENCY_DISPLAY_OPTION;
534514f5e3Sopenharmony_ci    static const std::vector<std::string> CURRENCY_DISPLAY_OPTION_NAME;
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    static const std::vector<CurrencySignOption> CURRENCY_SIGN_OPTION;
564514f5e3Sopenharmony_ci    static const std::vector<std::string> CURRENCY_SIGN_OPTION_NAME;
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    static const std::vector<UnitDisplayOption> UNIT_DISPLAY_OPTION;
594514f5e3Sopenharmony_ci    static const std::vector<std::string> UNIT_DISPLAY_OPTION_NAME;
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    static const std::vector<LocaleMatcherOption> LOCALE_MATCHER_OPTION;
624514f5e3Sopenharmony_ci    static const std::vector<std::string> LOCALE_MATCHER_OPTION_NAME;
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci    static const std::vector<NotationOption> NOTATION_OPTION;
654514f5e3Sopenharmony_ci    static const std::vector<std::string> NOTATION_OPTION_NAME;
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    static const std::vector<SignDisplayOption> SIGN_DISPLAY_OPTION;
684514f5e3Sopenharmony_ci    static const std::vector<std::string> SIGN_DISPLAY_OPTION_NAME;
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci    static const std::vector<CompactDisplayOption> COMPACT_DISPLAY_OPTION;
714514f5e3Sopenharmony_ci    static const std::vector<std::string> COMPACT_DISPLAY_OPTION_NAME;
724514f5e3Sopenharmony_ci    CAST_CHECK(JSNumberFormat, IsJSNumberFormat);
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci    static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
754514f5e3Sopenharmony_ci    ACCESSORS(Locale, LOCALE_OFFSET, NUMBER_STRING_SYSTEM_OFFSET)
764514f5e3Sopenharmony_ci    ACCESSORS(NumberingSystem, NUMBER_STRING_SYSTEM_OFFSET, CURRENCY_OFFSET)
774514f5e3Sopenharmony_ci    ACCESSORS(Currency, CURRENCY_OFFSET, UNIT_OFFSET)
784514f5e3Sopenharmony_ci    ACCESSORS(Unit, UNIT_OFFSET, MINIMUM_INTEGER_DIGITS_OFFSET)
794514f5e3Sopenharmony_ci    ACCESSORS(MinimumIntegerDigits, MINIMUM_INTEGER_DIGITS_OFFSET, MINIMUM_FRACTION_DIGITS_OFFSET)
804514f5e3Sopenharmony_ci    ACCESSORS(MinimumFractionDigits, MINIMUM_FRACTION_DIGITS_OFFSET, MAXIMUM_FRACTION_DIGITS_OFFSET)
814514f5e3Sopenharmony_ci    ACCESSORS(MaximumFractionDigits, MAXIMUM_FRACTION_DIGITS_OFFSET, MINIMUM_SIGNIFICANT_DIGITS_OFFSET)
824514f5e3Sopenharmony_ci    ACCESSORS(MinimumSignificantDigits, MINIMUM_SIGNIFICANT_DIGITS_OFFSET, MAXIMUM_SIGNIFICANT_DIGITS_OFFSET)
834514f5e3Sopenharmony_ci    ACCESSORS(MaximumSignificantDigits, MAXIMUM_SIGNIFICANT_DIGITS_OFFSET, USER_GROUPING_OFFSET)
844514f5e3Sopenharmony_ci    ACCESSORS(UseGrouping, USER_GROUPING_OFFSET, BOUND_FORMAT_OFFSET)
854514f5e3Sopenharmony_ci    ACCESSORS(BoundFormat, BOUND_FORMAT_OFFSET, ICU_FIELD_OFFSET)
864514f5e3Sopenharmony_ci    ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
874514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
884514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci    // define BitField
914514f5e3Sopenharmony_ci    static constexpr size_t STYLE_BITS = 3;
924514f5e3Sopenharmony_ci    static constexpr size_t CURRENCY_SIGN_BITS = 2;
934514f5e3Sopenharmony_ci    static constexpr size_t CURRENCY_DISPLAY_BITS = 3;
944514f5e3Sopenharmony_ci    static constexpr size_t UNIT_DISPLAY_BITS = 3;
954514f5e3Sopenharmony_ci    static constexpr size_t SIGN_DISPLAY_BITS = 3;
964514f5e3Sopenharmony_ci    static constexpr size_t COMPACT_DISPLAY_BITS = 2;
974514f5e3Sopenharmony_ci    static constexpr size_t NOTATION_BITS = 3;
984514f5e3Sopenharmony_ci    static constexpr size_t ROUNDING_TYPE_BITS = 3;
994514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, Style, StyleOption, STYLE_BITS)
1004514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, CurrencySign, CurrencySignOption, CURRENCY_SIGN_BITS, Style)
1014514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, CurrencyDisplay, CurrencyDisplayOption, CURRENCY_DISPLAY_BITS, CurrencySign)
1024514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, UnitDisplay, UnitDisplayOption, UNIT_DISPLAY_BITS, CurrencyDisplay)
1034514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, SignDisplay, SignDisplayOption, SIGN_DISPLAY_BITS, UnitDisplay)
1044514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, CompactDisplay, CompactDisplayOption, COMPACT_DISPLAY_BITS, SignDisplay)
1054514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, Notation, NotationOption, NOTATION_BITS, CompactDisplay)
1064514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, RoundingType, RoundingType, ROUNDING_TYPE_BITS, Notation)
1074514f5e3Sopenharmony_ci
1084514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
1094514f5e3Sopenharmony_ci    DECL_DUMP()
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ci    icu::number::LocalizedNumberFormatter *GetIcuCallTarget() const
1124514f5e3Sopenharmony_ci    {
1134514f5e3Sopenharmony_ci        ASSERT(GetIcuField().IsJSNativePointer());
1144514f5e3Sopenharmony_ci        auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
1154514f5e3Sopenharmony_ci        return reinterpret_cast<icu::number::LocalizedNumberFormatter *>(result);
1164514f5e3Sopenharmony_ci    }
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci    static void FreeIcuNumberformat([[maybe_unused]] void *env, void *pointer, void *data)
1194514f5e3Sopenharmony_ci    {
1204514f5e3Sopenharmony_ci        if (pointer == nullptr) {
1214514f5e3Sopenharmony_ci            return;
1224514f5e3Sopenharmony_ci        }
1234514f5e3Sopenharmony_ci        auto icuNumberformat = reinterpret_cast<icu::number::LocalizedNumberFormatter *>(pointer);
1244514f5e3Sopenharmony_ci        icuNumberformat->~LocalizedNumberFormatter();
1254514f5e3Sopenharmony_ci        if (data != nullptr) {
1264514f5e3Sopenharmony_ci            reinterpret_cast<EcmaVM *>(data)->GetNativeAreaAllocator()->FreeBuffer(icuNumberformat);
1274514f5e3Sopenharmony_ci        }
1284514f5e3Sopenharmony_ci    }
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_ci    // 12.1.2 InitializeNumberFormat ( numberFormat, locales, options )
1314514f5e3Sopenharmony_ci    static void InitializeNumberFormat(JSThread *thread,
1324514f5e3Sopenharmony_ci                                       const JSHandle<JSNumberFormat> &numberFormat,
1334514f5e3Sopenharmony_ci                                       const JSHandle<JSTaggedValue> &locales,
1344514f5e3Sopenharmony_ci                                       const JSHandle<JSTaggedValue> &options,
1354514f5e3Sopenharmony_ci                                       bool forIcuCache = false);
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci    // 12.1.3 CurrencyDigits ( currency )
1384514f5e3Sopenharmony_ci    static int32_t CurrencyDigits(const icu::UnicodeString &currency);
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_ci    static icu::number::LocalizedNumberFormatter *GetCachedIcuNumberFormatter(JSThread *thread,
1414514f5e3Sopenharmony_ci                                                                              const JSHandle<JSTaggedValue> &locales);
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_ci    // 12.1.8 FormatNumeric( numberFormat, x )
1444514f5e3Sopenharmony_ci    static JSHandle<JSTaggedValue> FormatNumeric(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat,
1454514f5e3Sopenharmony_ci                                                 JSTaggedValue x);
1464514f5e3Sopenharmony_ci    static JSHandle<JSTaggedValue> FormatNumeric(JSThread *thread,
1474514f5e3Sopenharmony_ci                                                 const icu::number::LocalizedNumberFormatter *icuNumberFormat,
1484514f5e3Sopenharmony_ci                                                 JSTaggedValue x);
1494514f5e3Sopenharmony_ci
1504514f5e3Sopenharmony_ci    // 12.1.9 FormatNumericToParts( numberFormat, x )
1514514f5e3Sopenharmony_ci    static JSHandle<JSArray> FormatNumericToParts(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat,
1524514f5e3Sopenharmony_ci                                                  JSTaggedValue x);
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci    // 12.1.12 UnwrapNumberFormat( nf )
1554514f5e3Sopenharmony_ci    static JSHandle<JSTaggedValue> UnwrapNumberFormat(JSThread *thread, const JSHandle<JSTaggedValue> &nf);
1564514f5e3Sopenharmony_ci
1574514f5e3Sopenharmony_ci    static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
1584514f5e3Sopenharmony_ci    static void ResolvedOptions(JSThread *thread, const JSHandle<JSNumberFormat> &numberFormat,
1594514f5e3Sopenharmony_ci                                const JSHandle<JSObject> &options);
1604514f5e3Sopenharmony_ci
1614514f5e3Sopenharmony_ci    template<typename T>
1624514f5e3Sopenharmony_ci    static icu::number::LocalizedNumberFormatter SetICUFormatterDigitOptions(
1634514f5e3Sopenharmony_ci        icu::number::LocalizedNumberFormatter &icuNumberformatter, const JSHandle<T> &formatter)
1644514f5e3Sopenharmony_ci    {
1654514f5e3Sopenharmony_ci        int minimumIntegerDigits = formatter->GetMinimumIntegerDigits().GetInt();
1664514f5e3Sopenharmony_ci        // Set ICU formatter IntegerWidth to MinimumIntegerDigits
1674514f5e3Sopenharmony_ci        icuNumberformatter =
1684514f5e3Sopenharmony_ci            icuNumberformatter.integerWidth(icu::number::IntegerWidth::zeroFillTo(minimumIntegerDigits));
1694514f5e3Sopenharmony_ci
1704514f5e3Sopenharmony_ci        int minimumSignificantDigits = formatter->GetMinimumSignificantDigits().GetInt();
1714514f5e3Sopenharmony_ci        int maximumSignificantDigits = formatter->GetMaximumSignificantDigits().GetInt();
1724514f5e3Sopenharmony_ci        int minimumFractionDigits = formatter->GetMinimumFractionDigits().GetInt();
1734514f5e3Sopenharmony_ci        int maximumFractionDigits = formatter->GetMaximumFractionDigits().GetInt();
1744514f5e3Sopenharmony_ci
1754514f5e3Sopenharmony_ci        // If roundingtype is "compact-rounding" return ICU formatter
1764514f5e3Sopenharmony_ci        RoundingType roundingType = formatter->GetRoundingType();
1774514f5e3Sopenharmony_ci        if (roundingType == RoundingType::COMPACTROUNDING) {
1784514f5e3Sopenharmony_ci            return icuNumberformatter;
1794514f5e3Sopenharmony_ci        }
1804514f5e3Sopenharmony_ci        // Else, Set ICU formatter FractionDigits and SignificantDigits
1814514f5e3Sopenharmony_ci        //   a. Set ICU formatter minFraction, maxFraction to MinimumFractionDigits, MaximumFractionDigits
1824514f5e3Sopenharmony_ci        icu::number::Precision precision =
1834514f5e3Sopenharmony_ci            icu::number::Precision::minMaxFraction(minimumFractionDigits, maximumFractionDigits);
1844514f5e3Sopenharmony_ci        //   b. if MinimumSignificantDigits is not 0,
1854514f5e3Sopenharmony_ci        //      Set ICU formatter minSignificantDigits, maxSignificantDigits to MinimumSignificantDigits,
1864514f5e3Sopenharmony_ci        //      MaximumSignificantDigits
1874514f5e3Sopenharmony_ci        if (minimumSignificantDigits != 0) {
1884514f5e3Sopenharmony_ci            precision =
1894514f5e3Sopenharmony_ci                icu::number::Precision::minMaxSignificantDigits(minimumSignificantDigits, maximumSignificantDigits);
1904514f5e3Sopenharmony_ci        }
1914514f5e3Sopenharmony_ci        return icuNumberformatter.precision(precision);
1924514f5e3Sopenharmony_ci    }
1934514f5e3Sopenharmony_ci};
1944514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1954514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_NUMBER_FORMAT_H