提交 dcc33482 编写于 作者: S sunyaozu

修改内源问题

Signed-off-by: Nsunyaozu <sunyaozu@huawei.com>
上级 2f37a218
...@@ -59,6 +59,9 @@ public: ...@@ -59,6 +59,9 @@ public:
std::string GetLocaleMatcher() const; std::string GetLocaleMatcher() const;
std::string GetFormatMatcher() const; std::string GetFormatMatcher() const;
std::string GetFractionalSecondDigits() const; std::string GetFractionalSecondDigits() const;
static std::unique_ptr<DateTimeFormat> CreateInstance(const std::vector<std::string> &localeTags,
std::map<std::string, std::string> &configs);
private: private:
std::string localeTag; std::string localeTag;
std::string dateStyle; std::string dateStyle;
...@@ -135,6 +138,7 @@ private: ...@@ -135,6 +138,7 @@ private:
void FixPatternPartTwo(); void FixPatternPartTwo();
void removeAmPmChar(); void removeAmPmChar();
int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size); int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size);
bool CheckInitSuccess();
}; };
} // namespace I18n } // namespace I18n
} // namespace Global } // namespace Global
......
...@@ -32,8 +32,10 @@ public: ...@@ -32,8 +32,10 @@ public:
std::string GetDisplayName(bool isDST); std::string GetDisplayName(bool isDST);
std::string GetDisplayName(std::string localeStr); std::string GetDisplayName(std::string localeStr);
std::string GetDisplayName(std::string localeStr, bool isDST); std::string GetDisplayName(std::string localeStr, bool isDST);
static std::unique_ptr<I18nTimeZone> CreateInstance(std::string zoneID);
private: private:
icu::TimeZone* GetTimeZone();
icu::TimeZone *timezone; icu::TimeZone *timezone;
}; };
} // namespace I18n } // namespace I18n
......
...@@ -46,6 +46,7 @@ public: ...@@ -46,6 +46,7 @@ public:
static std::string GetValidLocale(const std::string &localeTag); static std::string GetValidLocale(const std::string &localeTag);
static bool Is24HourClock(); static bool Is24HourClock();
static bool Set24HourClock(bool option); static bool Set24HourClock(bool option);
static bool CheckPermission();
private: private:
static bool IsValidLanguage(const std::string &language); static bool IsValidLanguage(const std::string &language);
...@@ -96,7 +97,6 @@ private: ...@@ -96,7 +97,6 @@ private:
static std::set<std::string> validHcTag; static std::set<std::string> validHcTag;
static bool listsInitialized; static bool listsInitialized;
static bool InitializeLists(); static bool InitializeLists();
static bool CheckPermission();
}; };
} // namespace I18n } // namespace I18n
} // namespace Global } // namespace Global
......
...@@ -31,8 +31,11 @@ public: ...@@ -31,8 +31,11 @@ public:
virtual ~PhoneNumberFormat(); virtual ~PhoneNumberFormat();
bool isValidPhoneNumber(const std::string &number) const; bool isValidPhoneNumber(const std::string &number) const;
std::string format(const std::string &number) const; std::string format(const std::string &number) const;
static std::unique_ptr<PhoneNumberFormat> CreateInstance(const std::string &countryTag,
const std::map<std::string, std::string> &options);
private: private:
PhoneNumberUtil* GetPhoneNumberUtil();
PhoneNumberUtil *util; PhoneNumberUtil *util;
std::string country; std::string country;
PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat; PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat;
......
...@@ -92,12 +92,17 @@ bool Collator::IsValidCollation(std::string &collation, UErrorCode &status) ...@@ -92,12 +92,17 @@ bool Collator::IsValidCollation(std::string &collation, UErrorCode &status)
std::unique_ptr<icu::StringEnumeration> enumeration( std::unique_ptr<icu::StringEnumeration> enumeration(
icu::Collator::getKeywordValuesForLocale("collation", icu::Locale(locale.getBaseName()), false, status)); icu::Collator::getKeywordValuesForLocale("collation", icu::Locale(locale.getBaseName()), false, status));
int length; int length;
const char *validCollations = enumeration->next(&length, status); const char *validCollations = nullptr;
if (enumeration != nullptr) {
validCollations = enumeration->next(&length, status);
}
while (validCollations != nullptr) { while (validCollations != nullptr) {
if (!strcmp(validCollations, currentCollation)) { if (!strcmp(validCollations, currentCollation)) {
return true; return true;
} }
validCollations = enumeration->next(&length, status); if (enumeration != nullptr) {
validCollations = enumeration->next(&length, status);
}
} }
} }
return false; return false;
......
...@@ -77,6 +77,24 @@ DateTimeFormat::~DateTimeFormat() ...@@ -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> DateTimeFormat::CreateInstance(const std::vector<std::string> &localeTags,
std::map<std::string, std::string> &configs)
{
std::unique_ptr<DateTimeFormat> dateTimeFormat = std::make_unique<DateTimeFormat>(localeTags, configs);
if (!dateTimeFormat->CheckInitSuccess()) {
return nullptr;
}
return dateTimeFormat;
}
void DateTimeFormat::InitWithLocale(const std::string &curLocale, std::map<std::string, std::string> &configs) void DateTimeFormat::InitWithLocale(const std::string &curLocale, std::map<std::string, std::string> &configs)
{ {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
...@@ -228,10 +246,10 @@ void DateTimeFormat::InitDateFormat(UErrorCode &status) ...@@ -228,10 +246,10 @@ void DateTimeFormat::InitDateFormat(UErrorCode &status)
if (!dateStyle.empty() || !timeStyle.empty()) { if (!dateStyle.empty() || !timeStyle.empty()) {
DateFormat::EStyle dateStyleValue = DateFormat::EStyle::kNone; DateFormat::EStyle dateStyleValue = DateFormat::EStyle::kNone;
DateFormat::EStyle timeStyleValue = DateFormat::EStyle::kNone; DateFormat::EStyle timeStyleValue = DateFormat::EStyle::kNone;
if (!dateStyle.empty()) { if (!dateStyle.empty() && dateTimeStyle.count(dateStyle) > 0) {
dateStyleValue = dateTimeStyle[dateStyle]; dateStyleValue = dateTimeStyle[dateStyle];
} }
if (!timeStyle.empty()) { if (!timeStyle.empty() && dateTimeStyle.count(timeStyle) > 0) {
timeStyleValue = dateTimeStyle[timeStyle]; timeStyleValue = dateTimeStyle[timeStyle];
} }
dateFormat = DateFormat::createDateTimeInstance(dateStyleValue, timeStyleValue, locale); dateFormat = DateFormat::createDateTimeInstance(dateStyleValue, timeStyleValue, locale);
...@@ -495,6 +513,9 @@ std::string DateTimeFormat::FormatRange(int64_t *fromDate, size_t fromDateSize, ...@@ -495,6 +513,9 @@ std::string DateTimeFormat::FormatRange(int64_t *fromDate, size_t fromDateSize,
minute = GetArrayValue(toDate, MINUTE_INDEX, toDateSize); minute = GetArrayValue(toDate, MINUTE_INDEX, toDateSize);
second = GetArrayValue(toDate, SECOND_INDEX, toDateSize); second = GetArrayValue(toDate, SECOND_INDEX, toDateSize);
auto toCalendar = std::unique_ptr<Calendar>(Calendar::createInstance(locale, status)); auto toCalendar = std::unique_ptr<Calendar>(Calendar::createInstance(locale, status));
if (toCalendar == nullptr) {
return nullptr;
}
toCalendar->clear(); toCalendar->clear();
toCalendar->set(year, month, day, hour, minute, second); toCalendar->set(year, month, day, hour, minute, second);
if (!timeZone.empty()) { if (!timeZone.empty()) {
......
...@@ -39,6 +39,20 @@ I18nTimeZone::~I18nTimeZone() ...@@ -39,6 +39,20 @@ I18nTimeZone::~I18nTimeZone()
} }
} }
icu::TimeZone* I18nTimeZone::GetTimeZone()
{
return timezone;
}
std::unique_ptr<I18nTimeZone> I18nTimeZone::CreateInstance(std::string zoneID)
{
std::unique_ptr<I18nTimeZone> i18nTimeZone = std::make_unique<I18nTimeZone>(zoneID);
if (i18nTimeZone->GetTimeZone() == nullptr) {
return nullptr;
}
return i18nTimeZone;
}
int32_t I18nTimeZone::GetOffset(double date) int32_t I18nTimeZone::GetOffset(double date)
{ {
int32_t rawOffset = 0; int32_t rawOffset = 0;
......
...@@ -175,7 +175,9 @@ void NumberFormat::ParseConfigs(std::map<std::string, std::string> &configs) ...@@ -175,7 +175,9 @@ void NumberFormat::ParseConfigs(std::map<std::string, std::string> &configs)
unit = configs["unit"]; unit = configs["unit"];
if (configs.count("unitDisplay") > 0) { if (configs.count("unitDisplay") > 0) {
unitDisplayString = configs["unitDisplay"]; unitDisplayString = configs["unitDisplay"];
unitDisplay = unitStyle[unitDisplayString]; if (unitStyle.count(unitDisplayString) > 0) {
unitDisplay = unitStyle[unitDisplayString];
}
} }
if (configs.count("unitUsage") > 0) { if (configs.count("unitUsage") > 0) {
unitUsage = configs["unitUsage"]; unitUsage = configs["unitUsage"];
...@@ -185,7 +187,8 @@ void NumberFormat::ParseConfigs(std::map<std::string, std::string> &configs) ...@@ -185,7 +187,8 @@ void NumberFormat::ParseConfigs(std::map<std::string, std::string> &configs)
currency = configs["currency"]; currency = configs["currency"];
if (configs.count("currencySign") > 0) { if (configs.count("currencySign") > 0) {
currencySign = configs["currencySign"]; currencySign = configs["currencySign"];
if (configs["currencySign"] != "accounting" && !signDisplayString.empty()) { if (configs["currencySign"] != "accounting" && !signDisplayString.empty() &&
signAccountingStyle.count(signDisplayString) > 0) {
signDisplay = signAccountingStyle[signDisplayString]; signDisplay = signAccountingStyle[signDisplayString];
} }
} }
......
...@@ -51,6 +51,21 @@ PhoneNumberFormat::~PhoneNumberFormat() ...@@ -51,6 +51,21 @@ PhoneNumberFormat::~PhoneNumberFormat()
{ {
} }
std::unique_ptr<PhoneNumberFormat> PhoneNumberFormat::CreateInstance(const std::string &countryTag,
const std::map<std::string, std::string> &options)
{
std::unique_ptr<PhoneNumberFormat> phoneNumberFormat = std::make_unique<PhoneNumberFormat>(countryTag, options);
if (phoneNumberFormat->GetPhoneNumberUtil() == nullptr) {
return nullptr;
}
return phoneNumberFormat;
}
PhoneNumberUtil* PhoneNumberFormat::GetPhoneNumberUtil()
{
return util;
}
bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const
{ {
i18n::phonenumbers::PhoneNumber phoneNumber; i18n::phonenumbers::PhoneNumber phoneNumber;
......
...@@ -66,6 +66,9 @@ bool PreferredLanguage::AddPreferredLanguageNonExist(std::vector<std::string> &p ...@@ -66,6 +66,9 @@ bool PreferredLanguage::AddPreferredLanguageNonExist(std::vector<std::string> &p
bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int index) bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int index)
{ {
if (!LocaleConfig::CheckPermission()) {
return false;
}
if (!IsValidTag(language)) { if (!IsValidTag(language)) {
return false; return false;
} }
...@@ -111,6 +114,9 @@ bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int in ...@@ -111,6 +114,9 @@ bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int in
bool PreferredLanguage::RemovePreferredLanguage(int index) bool PreferredLanguage::RemovePreferredLanguage(int index)
{ {
if (!LocaleConfig::CheckPermission()) {
return false;
}
std::vector<std::string> preferredLanguageList = GetPreferredLanguageList(); std::vector<std::string> preferredLanguageList = GetPreferredLanguageList();
int idx = index; int idx = index;
if (index < 0) { if (index < 0) {
......
...@@ -958,7 +958,7 @@ bool I18nAddon::InitPhoneNumberFormatContext(napi_env env, napi_callback_info in ...@@ -958,7 +958,7 @@ bool I18nAddon::InitPhoneNumberFormatContext(napi_env env, napi_callback_info in
return false; return false;
} }
env_ = env; env_ = env;
phonenumberfmt_ = std::make_unique<PhoneNumberFormat>(country, options); phonenumberfmt_ = PhoneNumberFormat::CreateInstance(country, options);
return phonenumberfmt_ != nullptr; return phonenumberfmt_ != nullptr;
} }
...@@ -2456,7 +2456,7 @@ napi_value I18nAddon::I18nTimeZoneConstructor(napi_env env, napi_callback_info i ...@@ -2456,7 +2456,7 @@ napi_value I18nAddon::I18nTimeZoneConstructor(napi_env env, napi_callback_info i
HiLog::Error(LABEL, "Wrap II18nAddon failed"); HiLog::Error(LABEL, "Wrap II18nAddon failed");
return nullptr; return nullptr;
} }
obj->timezone_ = std::make_unique<I18nTimeZone>(zoneID); obj->timezone_ = I18nTimeZone::CreateInstance(zoneID);
if (!obj->timezone_) { if (!obj->timezone_) {
HiLog::Error(LABEL, "Wrap TimeZone failed"); HiLog::Error(LABEL, "Wrap TimeZone failed");
return nullptr; return nullptr;
......
...@@ -370,7 +370,6 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info ...@@ -370,7 +370,6 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info
napi_value thisVar = nullptr; napi_value thisVar = nullptr;
void *data = nullptr; void *data = nullptr;
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
std::vector<std::string> localeTags; std::vector<std::string> localeTags;
if (argv[0] != nullptr) { if (argv[0] != nullptr) {
napi_valuetype valueType = napi_valuetype::napi_undefined; napi_valuetype valueType = napi_valuetype::napi_undefined;
...@@ -389,31 +388,26 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info ...@@ -389,31 +388,26 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info
} }
} }
} }
std::map<std::string, std::string> map = {}; std::map<std::string, std::string> map = {};
if (argv[1] != nullptr) { if (argv[1] != nullptr) {
GetDateOptionValues(env, argv[1], map); GetDateOptionValues(env, argv[1], map);
} }
std::unique_ptr<IntlAddon> obj = nullptr; std::unique_ptr<IntlAddon> obj = nullptr;
obj = std::make_unique<IntlAddon>(); obj = std::make_unique<IntlAddon>();
if (!obj) { if (!obj) {
HiLog::Error(LABEL, "Create IntlAddon failed"); HiLog::Error(LABEL, "Create IntlAddon failed");
return nullptr; return nullptr;
} }
status = status =
napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), IntlAddon::Destructor, nullptr, &obj->wrapper_); napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), IntlAddon::Destructor, nullptr, &obj->wrapper_);
if (status != napi_ok) { if (status != napi_ok) {
HiLog::Error(LABEL, "Wrap IntlAddon failed"); HiLog::Error(LABEL, "Wrap IntlAddon failed");
return nullptr; return nullptr;
} }
if (!obj->InitDateTimeFormatContext(env, info, localeTags, map)) { if (!obj->InitDateTimeFormatContext(env, info, localeTags, map)) {
HiLog::Error(LABEL, "Init DateTimeFormat failed"); HiLog::Error(LABEL, "Init DateTimeFormat failed");
return nullptr; return nullptr;
} }
obj.release(); obj.release();
return thisVar; return thisVar;
} }
...@@ -428,7 +422,7 @@ bool IntlAddon::InitDateTimeFormatContext(napi_env env, napi_callback_info info, ...@@ -428,7 +422,7 @@ bool IntlAddon::InitDateTimeFormatContext(napi_env env, napi_callback_info info,
return false; return false;
} }
env_ = env; env_ = env;
datefmt_ = std::make_unique<DateTimeFormat>(localeTags, map); datefmt_ = DateTimeFormat::CreateInstance(localeTags, map);
return datefmt_ != nullptr; return datefmt_ != nullptr;
} }
...@@ -538,6 +532,10 @@ napi_value IntlAddon::FormatDateTimeRange(napi_env env, napi_callback_info info) ...@@ -538,6 +532,10 @@ napi_value IntlAddon::FormatDateTimeRange(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr; napi_value thisVar = nullptr;
void *data = nullptr; void *data = nullptr;
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 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 firstYear = GetYear(env, argv, 0);
int64_t firstMonth = GetMonth(env, argv, 0); int64_t firstMonth = GetMonth(env, argv, 0);
int64_t firstDay = GetDay(env, argv, 0); int64_t firstDay = GetDay(env, argv, 0);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册