From 4ddcc6985d97120ebbf29e65e1eeabce5ed9e05b Mon Sep 17 00:00:00 2001 From: xiezhiyu Date: Wed, 19 Jun 2024 14:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=B5=AE=E7=82=B9=E6=95=B0?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E5=B0=8F=E6=95=B0=E4=BD=8D=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I7268387ff42aff7bef696ba8c4ead027c0f52588 --- frameworks/resmgr/src/utils/string_utils.cpp | 69 ++++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/frameworks/resmgr/src/utils/string_utils.cpp b/frameworks/resmgr/src/utils/string_utils.cpp index 28d4b71..74aa2cd 100644 --- a/frameworks/resmgr/src/utils/string_utils.cpp +++ b/frameworks/resmgr/src/utils/string_utils.cpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "hilog_wrapper.h" #ifdef SUPPORT_GRAPHICS @@ -37,7 +39,7 @@ namespace OHOS { namespace Global { namespace Resource { -const std::regex PLACEHOLDER_MATCHING_RULES(R"((%%)|%((\d+)\$){0,1}([dsf]))"); +const std::regex PLACEHOLDER_MATCHING_RULES(R"((%%)|%((\d+)\$){0,1}(\.\d)([dsf]))"); const std::string SIZE_T_MAX_STR = std::to_string(std::numeric_limits::max()); #ifdef SUPPORT_GRAPHICS const int PRECISION_OF_NUMBER = 6; @@ -141,7 +143,7 @@ bool parseArgs(const std::string &inputOutputValue, va_list args, continue; } std::string placeholderIndex = matches[3]; - std::string placeholderType = matches[4]; + std::string placeholderType = matches[5]; size_t paramIndex; if (placeholderIndex.length() != 0) { if (placeholderIndex.size() > SIZE_T_MAX_STR.size() || @@ -163,7 +165,8 @@ bool parseArgs(const std::string &inputOutputValue, va_list args, return getJsParams(inputOutputValue, args, paramsWithOutNum, paramsWithNum, jsParams); } -bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig, bool isKeepPrecision = true) +bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig, + bool isKeepPrecision = true, bool precision = 6) { #ifdef SUPPORT_GRAPHICS const ResLocale *resLocale = resConfig.GetResLocale(); @@ -200,7 +203,7 @@ bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig, icu::number::LocalizedNumberFormatter numberFormat = icu::number::NumberFormatter::withLocale(locale); numberFormat = numberFormat.grouping(UNumberGroupingStrategy::UNUM_GROUPING_OFF); if (isKeepPrecision) { - numberFormat = numberFormat.precision(icu::number::Precision::minFraction(PRECISION_OF_NUMBER)); + numberFormat = numberFormat.precision(icu::number::Precision::minFraction(precision)); } UErrorCode status = U_ZERO_ERROR; @@ -218,7 +221,8 @@ bool LocalizeNumber(std::string &inputOutputNum, const ResConfigImpl &resConfig, } bool GetReplaceStr(const std::vector> &jsParams, - const size_t ¶mIndex, const std::string &placeHolderType, const ResConfigImpl &config, std::string &replaceStr) + const size_t ¶mIndex, const std::string &placeHolderType, const ResConfigImpl &config, + std::string &replaceStr, int precision) { if (paramIndex >= jsParams.size()) { RESMGR_HILOGE(RESMGR_TAG, "index of placeholder out of range"); @@ -252,8 +256,29 @@ bool GetReplaceStr(const std::vector SIZE_T_MAX_STR.size() || + (placeholderIndex.size() == SIZE_T_MAX_STR.size() && placeholderIndex > SIZE_T_MAX_STR)) { + RESMGR_HILOGE(RESMGR_TAG, "index of placeholder is too large"); + return false; + } + if (std::stoul(placeholderIndex) < 1) { + return false; + } + paramIndex = std::stoul(placeholderIndex) - 1; + } else { + paramIndex = matchCount++; + } + return true; } bool ReplacePlaceholderWithParams(std::string &inputOutputValue, const ResConfigImpl &resConfig, @@ -262,13 +287,11 @@ bool ReplacePlaceholderWithParams(std::string &inputOutputValue, const ResConfig if (inputOutputValue.empty()) { return true; } - std::string::const_iterator start = inputOutputValue.begin(); std::string::const_iterator end = inputOutputValue.end(); std::smatch matches; size_t matchCount = 0; int prefixLength = 0; - while (std::regex_search(start, end, matches, PLACEHOLDER_MATCHING_RULES)) { prefixLength = matches[0].first - inputOutputValue.begin(); // Matched to %%, replace it with % @@ -281,32 +304,24 @@ bool ReplacePlaceholderWithParams(std::string &inputOutputValue, const ResConfig start = inputOutputValue.begin() + prefixLength + matches[0].length(); continue; } - // Matched to placeholder, check and parse param index + std::string placeholderIndex = matches[3]; - std::string placeholderType = matches[4]; + std::string precisionStr = matches[4]; + std::string placeholderType = matches[5]; + int precision = precisionStr[1] - '0'; + if (precision > PRECISION_OF_NUMBER) { + return false; + } size_t paramIndex; - if (placeholderIndex.length() != 0) { - if (placeholderIndex.size() > SIZE_T_MAX_STR.size() || - (placeholderIndex.size() == SIZE_T_MAX_STR.size() && placeholderIndex > SIZE_T_MAX_STR)) { - RESMGR_HILOGE(RESMGR_TAG, "index of placeholder is too large"); - return false; - } - if (std::stoul(placeholderIndex) < 1) { - return false; - } - paramIndex = std::stoul(placeholderIndex) - 1; - } else { - paramIndex = matchCount++; + if (!matchPlaceholder(placeholderIndex, paramIndex, matchCount)) { + return false; } - - // Replace placeholder with corresponding param std::string replaceStr; - if (!GetReplaceStr(jsParams, paramIndex, placeholderType, resConfig, replaceStr)) { + if (!GetReplaceStr(jsParams, paramIndex, placeholderType, resConfig, replaceStr, precision)) { return false; } inputOutputValue.replace(prefixLength, matches[0].length(), replaceStr); - // Update iterator start = inputOutputValue.begin() + prefixLength + replaceStr.length(); end = inputOutputValue.end(); -- Gitee