From b61384d936addde2520a2f0a3224896b0ea6a090 Mon Sep 17 00:00:00 2001 From: w00617344 <1323883142@qq.com> Date: Sat, 19 Feb 2022 15:14:47 +0800 Subject: [PATCH] 1323883142@qq.com Signed-off-by: w00617344 <1323883142@qq.com> --- frameworks/resmgr_lite/include/res_common.h | 2 - .../resmgr_lite/include/utils/date_utils.h | 1 - frameworks/resmgr_lite/src/hap_manager.cpp | 44 ++++++------- frameworks/resmgr_lite/src/locale_matcher.cpp | 5 +- frameworks/resmgr_lite/src/res_desc.cpp | 8 ++- .../resmgr_lite/src/resource_manager_impl.cpp | 19 +++--- frameworks/resmgr_lite/src/utils/utils.cpp | 61 ++++++++----------- 7 files changed, 69 insertions(+), 71 deletions(-) diff --git a/frameworks/resmgr_lite/include/res_common.h b/frameworks/resmgr_lite/include/res_common.h index 5cebb9a..a3e93ff 100644 --- a/frameworks/resmgr_lite/include/res_common.h +++ b/frameworks/resmgr_lite/include/res_common.h @@ -16,8 +16,6 @@ #ifndef OHOS_RES_COMMON_H #define OHOS_RES_COMMON_H -#include - namespace OHOS { namespace Global { namespace Resource { diff --git a/frameworks/resmgr_lite/include/utils/date_utils.h b/frameworks/resmgr_lite/include/utils/date_utils.h index 555f50e..3937ca1 100644 --- a/frameworks/resmgr_lite/include/utils/date_utils.h +++ b/frameworks/resmgr_lite/include/utils/date_utils.h @@ -17,7 +17,6 @@ #define RESOURCE_MANAGER_DATEUTILS_H #include -#include #include "common.h" namespace OHOS { diff --git a/frameworks/resmgr_lite/src/hap_manager.cpp b/frameworks/resmgr_lite/src/hap_manager.cpp index b5f5e40..b387c0c 100644 --- a/frameworks/resmgr_lite/src/hap_manager.cpp +++ b/frameworks/resmgr_lite/src/hap_manager.cpp @@ -133,16 +133,17 @@ const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueByName( for (i = 0; i < len; i++) { HapResource::ValueUnderQualifierDir *path = paths[i]; const ResConfigImpl *resConfig = path->GetResConfig(); - if (this->resConfig_->Match(resConfig)) { - if (bestResConfig == nullptr) { - bestIndex = i; - bestResConfig = resConfig; - } else if (bestResConfig->IsMoreSuitable(resConfig, currentResConfig)) { - continue; - } else { - bestResConfig = resConfig; - bestIndex = i; - } + if (!this->resConfig_->Match(resConfig)) { + continue; + } + if (bestResConfig == nullptr) { + bestIndex = i; + bestResConfig = resConfig; + continue; + } + if (!bestResConfig->IsMoreSuitable(resConfig, currentResConfig)) { + bestResConfig = resConfig; + bestIndex = i; } } return paths[bestIndex]; @@ -164,18 +165,17 @@ const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueById(ui for (i = 0; i < len; i++) { HapResource::ValueUnderQualifierDir *path = paths[i]; const ResConfigImpl *resConfig = path->GetResConfig(); - if (this->resConfig_->Match(resConfig)) { - if (bestResConfig == nullptr) { - bestIndex = i; - bestResConfig = resConfig; - } else { - if (bestResConfig->IsMoreSuitable(resConfig, currentResConfig)) { - continue; - } else { - bestResConfig = resConfig; - bestIndex = i; - } - } + if (!this->resConfig_->Match(resConfig)) { + continue; + } + if (bestResConfig == nullptr) { + bestIndex = i; + bestResConfig = resConfig; + continue; + } + if (!bestResConfig->IsMoreSuitable(resConfig, currentResConfig)) { + bestResConfig = resConfig; + bestIndex = i; } } return paths[bestIndex]; diff --git a/frameworks/resmgr_lite/src/locale_matcher.cpp b/frameworks/resmgr_lite/src/locale_matcher.cpp index 7ccac93..b54f059 100644 --- a/frameworks/resmgr_lite/src/locale_matcher.cpp +++ b/frameworks/resmgr_lite/src/locale_matcher.cpp @@ -232,7 +232,7 @@ size_t ComputeTrackPathDistance(const uint64_t *requestPaths, } } } - return len * 2; + return len * 2; } int8_t CompareRegionWhenQaag(const ResLocale *current, @@ -269,6 +269,7 @@ bool CompareLanguage(const ResLocale *current, const ResLocale *other) Utils::EncodeLanguageByResLocale(current); uint16_t otherEncodedLanguage = Utils::EncodeLanguageByResLocale( other); + // 0-4 NEW/OLD language code means iw/he,tl/fil,ji/yi,jw/jv,in/id return ((currentEncodedLanguage == otherEncodedLanguage) || ((currentEncodedLanguage == NEW_LANGUAGES_CODES[0]) && (otherEncodedLanguage == OLD_LANGUAGES_CODES[0])) || @@ -687,9 +688,11 @@ bool LocaleMatcher::IsRegionTag(const char *str, int32_t len) if (len < 0) { len = strlen(str); } + // region is 2 letters if is alpha string if (len == 2 && Utils::IsAlphaString(str, len)) { return true; } + // region is 3 letters if is numeric string if (len == 3 && Utils::IsNumericString(str, len)) { return true; } diff --git a/frameworks/resmgr_lite/src/res_desc.cpp b/frameworks/resmgr_lite/src/res_desc.cpp index d143db0..81c7b1b 100644 --- a/frameworks/resmgr_lite/src/res_desc.cpp +++ b/frameworks/resmgr_lite/src/res_desc.cpp @@ -13,9 +13,6 @@ * limitations under the License. */ #include "res_desc.h" - -#include - #include "hilog_wrapper.h" #include "securec.h" #include "utils/common.h" @@ -91,7 +88,9 @@ const std::string KeyParam::ConvertToStr() const HILOG_ERROR("memcpy_s error : %d", eret); } int j = 0; + // 4 means langauages/region/script key value max length for (int i = 0; i < 4; ++i) { + // 3 means reverse temp value to temp2 if (tmp[3 - i]) { tmp2[j++] = tmp[3 - i]; } @@ -145,6 +144,8 @@ bool IdItem::HaveParent() const if (!(resType_ == THEME || resType_ == PATTERN)) { return false; } + // the values_ storage map(key, value) and parent ref + // if have parent, size would be odd number return (values_.size() % 2 == 1); } @@ -156,6 +157,7 @@ bool IdItem::IsRef(const std::string &value, ResType &resType, int &id) return false; } auto index = value.find(":"); + // there are atleast one letter between '$' and ':' if (index == std::string::npos || index < 2) { return false; } diff --git a/frameworks/resmgr_lite/src/resource_manager_impl.cpp b/frameworks/resmgr_lite/src/resource_manager_impl.cpp index b2c4665..65c5592 100644 --- a/frameworks/resmgr_lite/src/resource_manager_impl.cpp +++ b/frameworks/resmgr_lite/src/resource_manager_impl.cpp @@ -240,6 +240,7 @@ RState ResourceManagerImpl::GetPluralString(const HapResource::ValueUnderQualifi size_t startIdx = 0; size_t loop = idItem->values_.size() / 2; for (size_t i = 0; i < loop; ++i) { + // 2 means key and value appear in pairs std::string key(idItem->values_[startIdx + i * 2]); std::string value(idItem->values_[startIdx + i * 2 + 1]); auto iter = map.find(key); @@ -331,18 +332,20 @@ RState ResourceManagerImpl::ResolveParentReference(const IdItem *idItem, std::ma // this make sure child covers parent size_t loop = currItem->values_.size() / 2; for (size_t i = 0; i < loop; ++i) { + // 2 means key and value appear in pairs std::string key(currItem->values_[startIdx + i * 2]); std::string value(currItem->values_[startIdx + i * 2 + 1]); auto iter = outValue.find(key); - if (iter == outValue.end()) { - std::string resolvedValue; - RState rrRet = ResolveReference(value, resolvedValue); - if (rrRet != SUCCESS) { - HILOG_ERROR("ResolveReference failed, value:%s", value.c_str()); - return ERROR; - } - outValue[key] = resolvedValue; + if (iter != outValue.end()) { + continue; } + std::string resolvedValue; + RState rrRet = ResolveReference(value, resolvedValue); + if (rrRet != SUCCESS) { + HILOG_ERROR("ResolveReference failed, value:%s", value.c_str()); + return ERROR; + } + outValue[key] = resolvedValue; } if (haveParent) { // get parent diff --git a/frameworks/resmgr_lite/src/utils/utils.cpp b/frameworks/resmgr_lite/src/utils/utils.cpp index f636f8d..490eec9 100644 --- a/frameworks/resmgr_lite/src/utils/utils.cpp +++ b/frameworks/resmgr_lite/src/utils/utils.cpp @@ -197,40 +197,33 @@ uint16_t Utils::EncodeLanguageOrRegion(const char *str, char base) bool Utils::StrCompare(const char *left, const char *right, size_t len, bool isCaseSensitive) { - if (left == nullptr) { - if (right == nullptr) { + if (left == nullptr && right == nullptr) { + return true; + } + if (left == nullptr || right == nullptr) { + return false; + } + int rc; + unsigned char c1, c2; + while (len--) { + c1 = (unsigned char)*left; + c2 = (unsigned char)*right; + if (c1 == 0 && c2 == 0) { return true; - } else { + } + if (c1 == 0 || c2 == 0) { return false; } - } else if (right == nullptr) { - return false; - } else { - int rc; - unsigned char c1, c2; - while (len--) { - c1 = (unsigned char)*left; - c2 = (unsigned char)*right; - if (c1 == 0) { - if (c2 == 0) { - return true; - } - return false; - } else if (c2 == 0) { - return false; - } else { - if (isCaseSensitive) { - rc = (int)(c1) - (int)(c2); - } else { - rc = tolower(c1) - tolower(c2); - } - if (rc != 0) { - return false; - } - } - ++left; - ++right; + if (isCaseSensitive) { + rc = (int)(c1) - (int)(c2); + } else { + rc = tolower(c1) - tolower(c2); + } + if (rc != 0) { + return false; } + ++left; + ++right; } return true; } @@ -274,7 +267,7 @@ RState Utils::ConvertColorToUInt32(const char *s, uint32_t &outValue) RState parseState = SUCCESS; size_t len = strlen(s); if (*s == '#') { - if (len == 4) { + if (len == 4) { // 4 means #rgb color |= 0xFF000000; color |= ParseHex(s[1], parseState) << 20; color |= ParseHex(s[1], parseState) << 16; @@ -282,7 +275,7 @@ RState Utils::ConvertColorToUInt32(const char *s, uint32_t &outValue) color |= ParseHex(s[2], parseState) << 8; color |= ParseHex(s[3], parseState) << 4; color |= ParseHex(s[3], parseState); - } else if (len == 5) { + } else if (len == 5) { // 5 means #argb color |= ParseHex(s[1], parseState) << 28; color |= ParseHex(s[1], parseState) << 24; color |= ParseHex(s[2], parseState) << 20; @@ -291,7 +284,7 @@ RState Utils::ConvertColorToUInt32(const char *s, uint32_t &outValue) color |= ParseHex(s[3], parseState) << 8; color |= ParseHex(s[4], parseState) << 4; color |= ParseHex(s[4], parseState); - } else if (len == 7) { + } else if (len == 7) { // 7 means #rrggbb color |= 0xFF000000; color |= ParseHex(s[1], parseState) << 20; color |= ParseHex(s[2], parseState) << 16; @@ -299,7 +292,7 @@ RState Utils::ConvertColorToUInt32(const char *s, uint32_t &outValue) color |= ParseHex(s[4], parseState) << 8; color |= ParseHex(s[5], parseState) << 4; color |= ParseHex(s[6], parseState); - } else if (len == 9) { + } else if (len == 9) { // 9 means #aarrggbb color |= ParseHex(s[1], parseState) << 28; color |= ParseHex(s[2], parseState) << 24; color |= ParseHex(s[3], parseState) << 20; -- Gitee