diff --git a/frameworks/intl/include/date_rule_init.h b/frameworks/intl/include/date_rule_init.h index 1848b8b5..9f8bc9d4 100644 --- a/frameworks/intl/include/date_rule_init.h +++ b/frameworks/intl/include/date_rule_init.h @@ -39,6 +39,8 @@ private: std::vector GetMatches(icu::UnicodeString& message); std::vector ClearFind(icu::UnicodeString& message); std::vector PastFind(icu::UnicodeString& message); + void GetMatchedInfo(std::vector& matches, MatchedDateTimeInfo match, + icu::UnicodeString& message); std::string locale; DateTimeRule* dateTimeRule; diff --git a/frameworks/intl/include/taboo.h b/frameworks/intl/include/taboo.h index 80516dca..f7e14ae1 100644 --- a/frameworks/intl/include/taboo.h +++ b/frameworks/intl/include/taboo.h @@ -20,6 +20,8 @@ #include #include #include +#include +#include namespace OHOS { namespace Global { @@ -49,11 +51,11 @@ private: void ReadResourceList(); std::string GetLanguageFromFileName(const std::string& fileName); - std::set supportedRegions; // Indicates which regions support name replacement using taboo data. - std::set supportedLanguages; // Indicates which languages support name replacement using taboo data. + static std::set supportedRegions; // Indicates which regions support name replacement using taboo data. + static std::set supportedLanguages; // Indicates which languages support name replacement using taboo data. // cache the name replacement taboo data of different locale. std::map> localeTabooData; - std::map resources; // Indicates which locales are supported to find taboo data. + static std::map RESOURCES; // Indicates which locales are supported to find taboo data. std::string tabooDataPath = ""; std::string tabooConfigFileName = "taboo-config.xml"; @@ -64,6 +66,7 @@ private: std::string regionKey = "region_"; // start part of region name replacement data key. std::string languageKey = "language_"; // start part of language name replacement data key. bool isTabooDataExist = false; + static std::mutex tabooMutex; static const char* ROOT_TAG; static const char* ITEM_TAG; diff --git a/frameworks/intl/src/date_rule_init.cpp b/frameworks/intl/src/date_rule_init.cpp index 2b96acf4..a1161dcf 100644 --- a/frameworks/intl/src/date_rule_init.cpp +++ b/frameworks/intl/src/date_rule_init.cpp @@ -114,30 +114,30 @@ std::vector DateRuleInit::GetMatches(icu::UnicodeString& me std::vector matches; for (auto& detect : universalAndLocaleRules) { std::vector tempMatches = detect.Match(message); - matches.insert(matches.end(), tempMatches.begin(), tempMatches.end()); - } - auto matchIterator = matches.begin(); - std::vector subResult; - while (matchIterator != matches.end()) { - MatchedDateTimeInfo match = (*matchIterator); - // splitting results based on subRules. - if (subDetectsMap.find(match.GetRegex()) != subDetectsMap.end()) { - RulesEngine subRulesEngine = subDetectsMap[match.GetRegex()]; - icu::UnicodeString subMessage = message.tempSubString(match.GetBegin(), match.GetEnd() - match.GetBegin()); - std::vector subMatches = subRulesEngine.Match(subMessage); - for (auto& subMatch : subMatches) { - subMatch.SetBegin(subMatch.GetBegin() + match.GetBegin()); - subMatch.SetEnd(subMatch.GetEnd() + match.GetBegin()); - } - subResult.insert(subResult.end(), subMatches.begin(), subMatches.end()); - matches.erase(matchIterator); - } else { - matchIterator++; + for (MatchedDateTimeInfo& match : tempMatches) { + GetMatchedInfo(matches, match, message); } } - matches.insert(matches.end(), subResult.begin(), subResult.end()); return matches; } + +void DateRuleInit::GetMatchedInfo(std::vector& matches, MatchedDateTimeInfo match, + icu::UnicodeString& message) +{ + // splitting results based on subRules. + if (subDetectsMap.find(match.GetRegex()) != subDetectsMap.end()) { + RulesEngine subRulesEngine = subDetectsMap[match.GetRegex()]; + icu::UnicodeString subMessage = message.tempSubString(match.GetBegin(), match.GetEnd() - match.GetBegin()); + std::vector subMatches = subRulesEngine.Match(subMessage); + for (auto& subMatch : subMatches) { + subMatch.SetBegin(subMatch.GetBegin() + match.GetBegin()); + subMatch.SetEnd(subMatch.GetEnd() + match.GetBegin()); + } + matches.insert(matches.end(), subMatches.begin(), subMatches.end()); + } else { + matches.push_back(match); + } +} } // namespace I18n } // namespace Global } // namespace OHOS \ No newline at end of file diff --git a/frameworks/intl/src/taboo.cpp b/frameworks/intl/src/taboo.cpp index 854573ae..4e3cf2b6 100644 --- a/frameworks/intl/src/taboo.cpp +++ b/frameworks/intl/src/taboo.cpp @@ -32,6 +32,10 @@ const char* Taboo::ROOT_TAG = "taboo"; const char* Taboo::ITEM_TAG = "item"; const char* Taboo::NAME_TAG = "name"; const char* Taboo::VALUE_TAG = "value"; +std::set Taboo::supportedRegions; +std::set Taboo::supportedLanguages; +std::map Taboo::RESOURCES = {}; +std::mutex Taboo::tabooMutex; Taboo::Taboo() { @@ -45,9 +49,12 @@ Taboo::Taboo(const std::string& path) : tabooDataPath(path) struct stat s; isTabooDataExist = stat(tabooConfigFilePath.c_str(), &s) == 0; if (isTabooDataExist) { - // parse taboo-config.xml to obtain supported regions and languages for name replacement. - ParseTabooData(tabooConfigFilePath, DataFileType::CONFIG_FILE); - ReadResourceList(); + std::lock_guard tabooLock(tabooMutex); + if (RESOURCES.size() == 0) { + // parse taboo-config.xml to obtain supported regions and languages for name replacement. + ParseTabooData(tabooConfigFilePath, DataFileType::CONFIG_FILE); + ReadResourceList(); + } } } @@ -167,6 +174,7 @@ void Taboo::ParseTabooData(const std::string& path, DataFileType fileType, const xmlFree(value); cur = cur->next; } + xmlFreeDoc(doc); } void Taboo::ProcessTabooConfigData(const std::string& name, const std::string& value) @@ -217,7 +225,7 @@ std::tuple Taboo::LanguageFallBack(const std::string& std::string fileName; int32_t bestScore = -1; - for (auto it = resources.begin(); it != resources.end(); ++it) { + for (auto it = RESOURCES.begin(); it != RESOURCES.end(); ++it) { std::string resLanguage = it->first; int32_t score = LocaleCompare::Compare(language, resLanguage); if (score > bestScore) { @@ -245,7 +253,7 @@ void Taboo::ReadResourceList() if (s.st_mode & S_IFDIR) { std::string fileName = path.substr(tabooDataPath.length()); std::string language = GetLanguageFromFileName(fileName); - resources[language] = fileName; + RESOURCES[language] = fileName; } } } diff --git a/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.cpp b/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.cpp index 3330a46c..5f9c0174 100644 --- a/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.cpp +++ b/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except", "in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.h b/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.h index 5bbe2106..56dcb0f4 100644 --- a/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.h +++ b/frameworks/intl/test/fuzztest/datetimefilter_fuzzer/datetimefilter_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except", "in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.cpp b/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.cpp index 90e33413..89dbef5a 100644 --- a/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.cpp +++ b/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except", "in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.h b/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.h index dda48636..16fbb6a6 100644 --- a/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.h +++ b/frameworks/intl/test/fuzztest/datetimerule_fuzzer/datetimerule_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except", "in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.cpp b/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.cpp index 104e6365..830a2ff5 100644 --- a/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.cpp +++ b/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except", "in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.h b/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.h index 12a15a8f..46bc022c 100644 --- a/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.h +++ b/frameworks/intl/test/fuzztest/entityrecongnizer_fuzzer/entityrecongnizer_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved. + * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except", "in compliance with the License. * You may obtain a copy of the License at