提交 dcc33482 编写于 作者: S sunyaozu

修改内源问题

Signed-off-by: Nsunyaozu <sunyaozu@huawei.com>
上级 2f37a218
......@@ -59,6 +59,9 @@ public:
std::string GetLocaleMatcher() const;
std::string GetFormatMatcher() 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:
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
......
......@@ -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<I18nTimeZone> CreateInstance(std::string zoneID);
private:
icu::TimeZone* GetTimeZone();
icu::TimeZone *timezone;
};
} // namespace I18n
......
......@@ -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<std::string> validHcTag;
static bool listsInitialized;
static bool InitializeLists();
static bool CheckPermission();
};
} // namespace I18n
} // namespace Global
......
......@@ -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<PhoneNumberFormat> CreateInstance(const std::string &countryTag,
const std::map<std::string, std::string> &options);
private:
PhoneNumberUtil* GetPhoneNumberUtil();
PhoneNumberUtil *util;
std::string country;
PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat;
......
......@@ -92,12 +92,17 @@ bool Collator::IsValidCollation(std::string &collation, UErrorCode &status)
std::unique_ptr<icu::StringEnumeration> 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;
......
......@@ -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)
{
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>(Calendar::createInstance(locale, status));
if (toCalendar == nullptr) {
return nullptr;
}
toCalendar->clear();
toCalendar->set(year, month, day, hour, minute, second);
if (!timeZone.empty()) {
......
......@@ -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 rawOffset = 0;
......
......@@ -175,7 +175,9 @@ void NumberFormat::ParseConfigs(std::map<std::string, std::string> &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<std::string, std::string> &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];
}
}
......
......@@ -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
{
i18n::phonenumbers::PhoneNumber phoneNumber;
......
......@@ -66,6 +66,9 @@ bool PreferredLanguage::AddPreferredLanguageNonExist(std::vector<std::string> &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<std::string> preferredLanguageList = GetPreferredLanguageList();
int idx = index;
if (index < 0) {
......
......@@ -958,7 +958,7 @@ bool I18nAddon::InitPhoneNumberFormatContext(napi_env env, napi_callback_info in
return false;
}
env_ = env;
phonenumberfmt_ = std::make_unique<PhoneNumberFormat>(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<I18nTimeZone>(zoneID);
obj->timezone_ = I18nTimeZone::CreateInstance(zoneID);
if (!obj->timezone_) {
HiLog::Error(LABEL, "Wrap TimeZone failed");
return nullptr;
......
......@@ -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<std::string> 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<std::string, std::string> map = {};
if (argv[1] != nullptr) {
GetDateOptionValues(env, argv[1], map);
}
std::unique_ptr<IntlAddon> obj = nullptr;
obj = std::make_unique<IntlAddon>();
if (!obj) {
HiLog::Error(LABEL, "Create IntlAddon failed");
return nullptr;
}
status =
napi_wrap(env, thisVar, reinterpret_cast<void *>(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<DateTimeFormat>(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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册