From dcc33482bef74092263e65c2e7d0c7d1cbef47ac Mon Sep 17 00:00:00 2001 From: sunyaozu Date: Thu, 24 Mar 2022 20:26:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=86=85=E6=BA=90=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunyaozu --- frameworks/intl/include/date_time_format.h | 4 +++ frameworks/intl/include/i18n_timezone.h | 2 ++ frameworks/intl/include/locale_config.h | 2 +- frameworks/intl/include/phone_number_format.h | 3 +++ frameworks/intl/src/collator.cpp | 9 +++++-- frameworks/intl/src/date_time_format.cpp | 25 +++++++++++++++++-- frameworks/intl/src/i18n_timezone.cpp | 14 +++++++++++ frameworks/intl/src/number_format.cpp | 7 ++++-- frameworks/intl/src/phone_number_format.cpp | 15 +++++++++++ frameworks/intl/src/preferred_language.cpp | 6 +++++ interfaces/js/kits/src/i18n_addon.cpp | 4 +-- interfaces/js/kits/src/intl_addon.cpp | 12 ++++----- 12 files changed, 87 insertions(+), 16 deletions(-) diff --git a/frameworks/intl/include/date_time_format.h b/frameworks/intl/include/date_time_format.h index 129027c..07f5686 100644 --- a/frameworks/intl/include/date_time_format.h +++ b/frameworks/intl/include/date_time_format.h @@ -59,6 +59,9 @@ public: std::string GetLocaleMatcher() const; std::string GetFormatMatcher() const; std::string GetFractionalSecondDigits() const; + static std::unique_ptr CreateInstance(const std::vector &localeTags, + std::map &configs); + private: std::string localeTag; std::string dateStyle; @@ -135,6 +138,7 @@ private: void FixPatternPartTwo(); void removeAmPmChar(); int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size); + bool CheckInitSuccess(); }; } // namespace I18n } // namespace Global diff --git a/frameworks/intl/include/i18n_timezone.h b/frameworks/intl/include/i18n_timezone.h index ca769e0..d1e31ce 100644 --- a/frameworks/intl/include/i18n_timezone.h +++ b/frameworks/intl/include/i18n_timezone.h @@ -32,8 +32,10 @@ public: std::string GetDisplayName(bool isDST); std::string GetDisplayName(std::string localeStr); std::string GetDisplayName(std::string localeStr, bool isDST); + static std::unique_ptr CreateInstance(std::string zoneID); private: + icu::TimeZone* GetTimeZone(); icu::TimeZone *timezone; }; } // namespace I18n diff --git a/frameworks/intl/include/locale_config.h b/frameworks/intl/include/locale_config.h index 2c0c48b..c695cb6 100644 --- a/frameworks/intl/include/locale_config.h +++ b/frameworks/intl/include/locale_config.h @@ -46,6 +46,7 @@ public: static std::string GetValidLocale(const std::string &localeTag); static bool Is24HourClock(); static bool Set24HourClock(bool option); + static bool CheckPermission(); private: static bool IsValidLanguage(const std::string &language); @@ -96,7 +97,6 @@ private: static std::set validHcTag; static bool listsInitialized; static bool InitializeLists(); - static bool CheckPermission(); }; } // namespace I18n } // namespace Global diff --git a/frameworks/intl/include/phone_number_format.h b/frameworks/intl/include/phone_number_format.h index 60ebce3..676fa90 100644 --- a/frameworks/intl/include/phone_number_format.h +++ b/frameworks/intl/include/phone_number_format.h @@ -31,8 +31,11 @@ public: virtual ~PhoneNumberFormat(); bool isValidPhoneNumber(const std::string &number) const; std::string format(const std::string &number) const; + static std::unique_ptr CreateInstance(const std::string &countryTag, + const std::map &options); private: + PhoneNumberUtil* GetPhoneNumberUtil(); PhoneNumberUtil *util; std::string country; PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat; diff --git a/frameworks/intl/src/collator.cpp b/frameworks/intl/src/collator.cpp index b3b5fc9..2f1b482 100644 --- a/frameworks/intl/src/collator.cpp +++ b/frameworks/intl/src/collator.cpp @@ -92,12 +92,17 @@ bool Collator::IsValidCollation(std::string &collation, UErrorCode &status) std::unique_ptr enumeration( icu::Collator::getKeywordValuesForLocale("collation", icu::Locale(locale.getBaseName()), false, status)); int length; - const char *validCollations = enumeration->next(&length, status); + const char *validCollations = nullptr; + if (enumeration != nullptr) { + validCollations = enumeration->next(&length, status); + } while (validCollations != nullptr) { if (!strcmp(validCollations, currentCollation)) { return true; } - validCollations = enumeration->next(&length, status); + if (enumeration != nullptr) { + validCollations = enumeration->next(&length, status); + } } } return false; diff --git a/frameworks/intl/src/date_time_format.cpp b/frameworks/intl/src/date_time_format.cpp index 3929550..79df7ef 100644 --- a/frameworks/intl/src/date_time_format.cpp +++ b/frameworks/intl/src/date_time_format.cpp @@ -77,6 +77,24 @@ DateTimeFormat::~DateTimeFormat() } } +bool DateTimeFormat::CheckInitSuccess() +{ + if (dateIntvFormat == nullptr || calendar == nullptr || dateFormat == nullptr || localeInfo == nullptr) { + return false; + } + return true; +} + +std::unique_ptr DateTimeFormat::CreateInstance(const std::vector &localeTags, + std::map &configs) +{ + std::unique_ptr dateTimeFormat = std::make_unique(localeTags, configs); + if (!dateTimeFormat->CheckInitSuccess()) { + return nullptr; + } + return dateTimeFormat; +} + void DateTimeFormat::InitWithLocale(const std::string &curLocale, std::map &configs) { UErrorCode status = U_ZERO_ERROR; @@ -228,10 +246,10 @@ void DateTimeFormat::InitDateFormat(UErrorCode &status) if (!dateStyle.empty() || !timeStyle.empty()) { DateFormat::EStyle dateStyleValue = DateFormat::EStyle::kNone; DateFormat::EStyle timeStyleValue = DateFormat::EStyle::kNone; - if (!dateStyle.empty()) { + if (!dateStyle.empty() && dateTimeStyle.count(dateStyle) > 0) { dateStyleValue = dateTimeStyle[dateStyle]; } - if (!timeStyle.empty()) { + if (!timeStyle.empty() && dateTimeStyle.count(timeStyle) > 0) { timeStyleValue = dateTimeStyle[timeStyle]; } dateFormat = DateFormat::createDateTimeInstance(dateStyleValue, timeStyleValue, locale); @@ -495,6 +513,9 @@ std::string DateTimeFormat::FormatRange(int64_t *fromDate, size_t fromDateSize, minute = GetArrayValue(toDate, MINUTE_INDEX, toDateSize); second = GetArrayValue(toDate, SECOND_INDEX, toDateSize); auto toCalendar = std::unique_ptr(Calendar::createInstance(locale, status)); + if (toCalendar == nullptr) { + return nullptr; + } toCalendar->clear(); toCalendar->set(year, month, day, hour, minute, second); if (!timeZone.empty()) { diff --git a/frameworks/intl/src/i18n_timezone.cpp b/frameworks/intl/src/i18n_timezone.cpp index 24ee048..5bb50a3 100644 --- a/frameworks/intl/src/i18n_timezone.cpp +++ b/frameworks/intl/src/i18n_timezone.cpp @@ -39,6 +39,20 @@ I18nTimeZone::~I18nTimeZone() } } +icu::TimeZone* I18nTimeZone::GetTimeZone() +{ + return timezone; +} + +std::unique_ptr I18nTimeZone::CreateInstance(std::string zoneID) +{ + std::unique_ptr i18nTimeZone = std::make_unique(zoneID); + if (i18nTimeZone->GetTimeZone() == nullptr) { + return nullptr; + } + return i18nTimeZone; +} + int32_t I18nTimeZone::GetOffset(double date) { int32_t rawOffset = 0; diff --git a/frameworks/intl/src/number_format.cpp b/frameworks/intl/src/number_format.cpp index 3ad769a..adee381 100644 --- a/frameworks/intl/src/number_format.cpp +++ b/frameworks/intl/src/number_format.cpp @@ -175,7 +175,9 @@ void NumberFormat::ParseConfigs(std::map &configs) unit = configs["unit"]; if (configs.count("unitDisplay") > 0) { unitDisplayString = configs["unitDisplay"]; - unitDisplay = unitStyle[unitDisplayString]; + if (unitStyle.count(unitDisplayString) > 0) { + unitDisplay = unitStyle[unitDisplayString]; + } } if (configs.count("unitUsage") > 0) { unitUsage = configs["unitUsage"]; @@ -185,7 +187,8 @@ void NumberFormat::ParseConfigs(std::map &configs) currency = configs["currency"]; if (configs.count("currencySign") > 0) { currencySign = configs["currencySign"]; - if (configs["currencySign"] != "accounting" && !signDisplayString.empty()) { + if (configs["currencySign"] != "accounting" && !signDisplayString.empty() && + signAccountingStyle.count(signDisplayString) > 0) { signDisplay = signAccountingStyle[signDisplayString]; } } diff --git a/frameworks/intl/src/phone_number_format.cpp b/frameworks/intl/src/phone_number_format.cpp index 21706e7..e1c5084 100644 --- a/frameworks/intl/src/phone_number_format.cpp +++ b/frameworks/intl/src/phone_number_format.cpp @@ -51,6 +51,21 @@ PhoneNumberFormat::~PhoneNumberFormat() { } +std::unique_ptr PhoneNumberFormat::CreateInstance(const std::string &countryTag, + const std::map &options) +{ + std::unique_ptr phoneNumberFormat = std::make_unique(countryTag, options); + if (phoneNumberFormat->GetPhoneNumberUtil() == nullptr) { + return nullptr; + } + return phoneNumberFormat; +} + +PhoneNumberUtil* PhoneNumberFormat::GetPhoneNumberUtil() +{ + return util; +} + bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const { i18n::phonenumbers::PhoneNumber phoneNumber; diff --git a/frameworks/intl/src/preferred_language.cpp b/frameworks/intl/src/preferred_language.cpp index f03ae4f..11c5084 100644 --- a/frameworks/intl/src/preferred_language.cpp +++ b/frameworks/intl/src/preferred_language.cpp @@ -66,6 +66,9 @@ bool PreferredLanguage::AddPreferredLanguageNonExist(std::vector &p bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int index) { + if (!LocaleConfig::CheckPermission()) { + return false; + } if (!IsValidTag(language)) { return false; } @@ -111,6 +114,9 @@ bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int in bool PreferredLanguage::RemovePreferredLanguage(int index) { + if (!LocaleConfig::CheckPermission()) { + return false; + } std::vector preferredLanguageList = GetPreferredLanguageList(); int idx = index; if (index < 0) { diff --git a/interfaces/js/kits/src/i18n_addon.cpp b/interfaces/js/kits/src/i18n_addon.cpp index 9fbcb17..45c6f09 100644 --- a/interfaces/js/kits/src/i18n_addon.cpp +++ b/interfaces/js/kits/src/i18n_addon.cpp @@ -958,7 +958,7 @@ bool I18nAddon::InitPhoneNumberFormatContext(napi_env env, napi_callback_info in return false; } env_ = env; - phonenumberfmt_ = std::make_unique(country, options); + phonenumberfmt_ = PhoneNumberFormat::CreateInstance(country, options); return phonenumberfmt_ != nullptr; } @@ -2456,7 +2456,7 @@ napi_value I18nAddon::I18nTimeZoneConstructor(napi_env env, napi_callback_info i HiLog::Error(LABEL, "Wrap II18nAddon failed"); return nullptr; } - obj->timezone_ = std::make_unique(zoneID); + obj->timezone_ = I18nTimeZone::CreateInstance(zoneID); if (!obj->timezone_) { HiLog::Error(LABEL, "Wrap TimeZone failed"); return nullptr; diff --git a/interfaces/js/kits/src/intl_addon.cpp b/interfaces/js/kits/src/intl_addon.cpp index 01005d6..31195e8 100644 --- a/interfaces/js/kits/src/intl_addon.cpp +++ b/interfaces/js/kits/src/intl_addon.cpp @@ -370,7 +370,6 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - std::vector localeTags; if (argv[0] != nullptr) { napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -389,31 +388,26 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info } } } - std::map map = {}; if (argv[1] != nullptr) { GetDateOptionValues(env, argv[1], map); } - std::unique_ptr obj = nullptr; obj = std::make_unique(); if (!obj) { HiLog::Error(LABEL, "Create IntlAddon failed"); return nullptr; } - status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), IntlAddon::Destructor, nullptr, &obj->wrapper_); if (status != napi_ok) { HiLog::Error(LABEL, "Wrap IntlAddon failed"); return nullptr; } - if (!obj->InitDateTimeFormatContext(env, info, localeTags, map)) { HiLog::Error(LABEL, "Init DateTimeFormat failed"); return nullptr; } - obj.release(); return thisVar; } @@ -428,7 +422,7 @@ bool IntlAddon::InitDateTimeFormatContext(napi_env env, napi_callback_info info, return false; } env_ = env; - datefmt_ = std::make_unique(localeTags, map); + datefmt_ = DateTimeFormat::CreateInstance(localeTags, map); return datefmt_ != nullptr; } @@ -538,6 +532,10 @@ napi_value IntlAddon::FormatDateTimeRange(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (argv[0] == nullptr || argv[1] == nullptr) { + HiLog::Error(LABEL, "Parameter wrong"); + return nullptr; + } int64_t firstYear = GetYear(env, argv, 0); int64_t firstMonth = GetMonth(env, argv, 0); int64_t firstDay = GetDay(env, argv, 0); -- GitLab