From 65508d3c205d31d9a7089e98b05fd627820bd7ea Mon Sep 17 00:00:00 2001 From: liuzhongyu101 Date: Thu, 14 Mar 2024 10:01:03 +0800 Subject: [PATCH] bigint/tolocalestring && unified/percent --- ecmascript/js_locale.cpp | 2 +- ecmascript/js_number_format.cpp | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ecmascript/js_locale.cpp b/ecmascript/js_locale.cpp index 2575fc168..1c6bf78db 100644 --- a/ecmascript/js_locale.cpp +++ b/ecmascript/js_locale.cpp @@ -693,7 +693,7 @@ JSHandle JSLocale::GetNumberFieldType(JSThread *thread, JSTaggedV auto globalConst = thread->GlobalConstants(); if (static_cast(fieldId) == UNUM_INTEGER_FIELD) { number = x.GetNumber(); - if (std::isfinite(number)) { + if (std::isfinite(number) || x.IsBigInt()) { return globalConst->GetHandledIntegerString(); } if (std::isnan(number)) { diff --git a/ecmascript/js_number_format.cpp b/ecmascript/js_number_format.cpp index e70fd23bf..494ecc269 100644 --- a/ecmascript/js_number_format.cpp +++ b/ecmascript/js_number_format.cpp @@ -964,9 +964,7 @@ void GroupToParts(JSThread *thread, const icu::number::FormattedNumber &formatte int32_t fieldId = cfpo.getField(); int32_t start = cfpo.getStart(); int32_t limit = cfpo.getLimit(); - // typeString.Update(globalConst->GetLiteralString()); - typeString.Update(globalConst->GetPercentSignString()); - + typeString.Update(globalConst->GetLiteralString()); // If start greater than previousLimit, means a literal type exists before number fields // so add a literal type with value of formattedText.sub(0, start) // Special case when fieldId is UNUM_GROUPING_SEPARATOR_FIELD @@ -999,14 +997,13 @@ void GroupToParts(JSThread *thread, const icu::number::FormattedNumber &formatte start = start + groupLeapLength; lastFieldGroup = false; } - // Special case in ICU when style is unit and unit is percent - // if (styleOption == StyleOption::PERCENT && static_cast(fieldId) == UNUM_MEASURE_UNIT_FIELD) { + // Special case in ICU when style is unit and unit is percentU if (styleOption == StyleOption::UNIT && static_cast(fieldId) == UNUM_PERCENT_FIELD) { typeString.Update(globalConst->GetUnitString()); } else { typeString.Update(JSLocale::GetNumberFieldType(thread, x, fieldId).GetTaggedValue()); } - JSHandle substring = intl::LocaleHelper::UStringToString(thread, formattedText, start, limit); + JSHandle substring = intl::LocaleHelper::UStringToString(thread, formattedText, previousLimit, limit); JSLocale::PutElement(thread, index, receiver, typeString, JSHandle::Cast(substring)); RETURN_IF_ABRUPT_COMPLETION(thread); index++; @@ -1015,8 +1012,7 @@ void GroupToParts(JSThread *thread, const icu::number::FormattedNumber &formatte // If iterated length is smaller than formattedText.length, means a literal type exists after number fields // so add a literal type with value of formattedText.sub(previousLimit, formattedText.length) if (formattedText.length() > previousLimit) { - // typeString.Update(globalConst->GetLiteralString()); - typeString.Update(globalConst->GetPercentSignString()); + typeString.Update(globalConst->GetLiteralString()); JSHandle substring = intl::LocaleHelper::UStringToString(thread, formattedText, previousLimit, formattedText.length()); JSLocale::PutElement(thread, index, receiver, typeString, JSHandle::Cast(substring)); @@ -1033,8 +1029,16 @@ JSHandle JSNumberFormat::FormatNumericToParts(JSThread *thread, const J ASSERT(icuNumberFormatter != nullptr); UErrorCode status = U_ZERO_ERROR; - double number = x.GetNumber(); - icu::number::FormattedNumber formattedNumber = icuNumberFormatter->formatDouble(number, status); + icu::number::FormattedNumber formattedNumber; + if (x.IsBigInt()) { + JSHandle bigint(thread, x); + JSHandle bigintStr = BigInt::ToString(thread, bigint); + std::string stdString = EcmaStringAccessor(bigintStr).ToStdString(); + formattedNumber = icuNumberFormatter->formatDecimal(icu::StringPiece(stdString), status); + } else { + double number = x.GetNumber(); + formattedNumber = icuNumberFormatter->formatDouble(number, status); + } if (U_FAILURE(status)) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle emptyArray = factory->NewJSArray(); -- Gitee