From c8c06e82609cfe7359486b44efbbb088e8a4bd12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=89=91?= Date: Mon, 18 Nov 2024 12:02:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王剑 --- frameworks/i18n/src/locale_info.cpp | 18 +++++------------- frameworks/i18n/src/measure_format_impl.cpp | 6 ++++-- frameworks/i18n/src/number_data.cpp | 2 +- frameworks/i18n/src/number_format_impl.cpp | 4 ++-- interfaces/kits/i18n/include/locale_info.h | 3 +++ 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/frameworks/i18n/src/locale_info.cpp b/frameworks/i18n/src/locale_info.cpp index 03c34ae..69e1776 100644 --- a/frameworks/i18n/src/locale_info.cpp +++ b/frameworks/i18n/src/locale_info.cpp @@ -21,6 +21,10 @@ using namespace OHOS::I18N; +const std::set LocaleInfo::SCRIPTS = { + "Hans", "Latn", "Hant", "Qaag", "Cyrl", "Deva", "Guru" +}; + void LocaleInfo::Init(const char *newLang, const char *newScript, const char *newRegion, int &status) { id = nullptr; @@ -408,19 +412,7 @@ bool LocaleInfo::IsScript(const char *start, uint8_t length) if (length != SCRIPT_LENGTH || start == nullptr) { return false; } - if (memcmp(start, "Hans", length) == 0) { - return true; - } else if (memcmp(start, "Latn", length) == 0) { - return true; - } else if (memcmp(start, "Hant", length) == 0) { - return true; - } else if (memcmp(start, "Qaag", length) == 0) { - return true; - } else if (memcmp(start, "Cyrl", length) == 0) { - return true; - } else if (memcmp(start, "Deva", length) == 0) { - return true; - } else if (memcmp(start, "Guru", length) == 0) { + if (SCRIPTS.find(std::string(start, length)) != SCRIPTS.end()) { return true; } else { return false; diff --git a/frameworks/i18n/src/measure_format_impl.cpp b/frameworks/i18n/src/measure_format_impl.cpp index 51910cf..23328b2 100644 --- a/frameworks/i18n/src/measure_format_impl.cpp +++ b/frameworks/i18n/src/measure_format_impl.cpp @@ -26,13 +26,13 @@ MeasureFormatImpl::MeasureFormatImpl(LocaleInfo &localeinfo, I18nStatus &status) return; } locale = localeinfo; - pluralFormat = new PluralFormat(localeinfo, status); + pluralFormat = new (std::nothrow) PluralFormat(localeinfo, status); if (status != I18nStatus::ISUCCESS || pluralFormat == nullptr) { status = I18nStatus::IERROR; return; } int numberFormatStatus = 0; - numberFormat = new NumberFormat(localeinfo, numberFormatStatus); + numberFormat = new (std::nothrow) NumberFormat(localeinfo, numberFormatStatus); if (numberFormatStatus || numberFormat == nullptr) { status = I18nStatus::IERROR; return; @@ -201,6 +201,7 @@ bool MeasureFormatImpl::InitMeasureFormat(std::string &unprocessedMeasureData) Split(unprocessedMeasureData, items, itemCount, PLURAL_SEP); // items[1] is unit list, such as h|min|kcal|time... if (!ParseUnits(items[1])) { + delete[] items; return false; } // items[2] is pattern @@ -213,6 +214,7 @@ bool MeasureFormatImpl::InitMeasureFormat(std::string &unprocessedMeasureData) } formattedUnitCount = unitCount * MEASURE_FORMAT_TYPE_NUM; if (!AllocaFormattedUnits()) { + delete[] items; return false; } int itemStartIndex = MEASURE_BASE_ITEM_COUNT; diff --git a/frameworks/i18n/src/number_data.cpp b/frameworks/i18n/src/number_data.cpp index 743672a..2a26e26 100644 --- a/frameworks/i18n/src/number_data.cpp +++ b/frameworks/i18n/src/number_data.cpp @@ -65,7 +65,7 @@ void NumberData::SetNumSystem(std::string *numSym, const int numSize) return; } ArrayCopy(nativeNums, NUM_SIZE, numSym, numSize); - if (!(numSym[0]).empty() && ((numSym[0]).at(0) != '0')) { + if (!(numSym[0]).empty() && (numSym[0][0] != '0')) { isNative = true; } } diff --git a/frameworks/i18n/src/number_format_impl.cpp b/frameworks/i18n/src/number_format_impl.cpp index 0c13a0c..4e8a137 100644 --- a/frameworks/i18n/src/number_format_impl.cpp +++ b/frameworks/i18n/src/number_format_impl.cpp @@ -96,7 +96,7 @@ bool NumberFormatImpl::Init(const DataResource &resource) adjust = std::string(origin, 0, size - 3); // strip the last 3 chars } const char *percentPat = adjust.c_str(); - defaultData = new(std::nothrow) NumberData(pat, percentPat, decSign, groupSign, perSign); + defaultData = new (std::nothrow) NumberData(pat, percentPat, decSign, groupSign, perSign); if (defaultData == nullptr) { return false; } @@ -397,7 +397,7 @@ bool NumberFormatImpl::SetMaxDecimalLength(int length) defaultData->SetMinDecimalLength(adjustValue); } maxDecimalLength = adjustValue; - defaultData->SetMaxDecimalLength(length); + defaultData->SetMaxDecimalLength(adjustValue); defaultData->UpdateNumberFormat(); return true; } diff --git a/interfaces/kits/i18n/include/locale_info.h b/interfaces/kits/i18n/include/locale_info.h index 109baa0..ec8d8a0 100644 --- a/interfaces/kits/i18n/include/locale_info.h +++ b/interfaces/kits/i18n/include/locale_info.h @@ -44,6 +44,8 @@ */ #include +#include +#include #include "types.h" namespace OHOS { @@ -211,6 +213,7 @@ private: void SetFail(); void Init(const char *lang, const char *script, const char *region, int &status); const int CHAR_OFF = 48; + static const std::set SCRIPTS; static constexpr uint16_t OPT_LANG = 0x0001; static constexpr uint16_t OPT_SCRIPT = 0x0002; static constexpr uint16_t OPT_REGION = 0x0004; -- Gitee