123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_UTILS_H 1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_UTILS_H 1823b3eb3cSopenharmony_ci 1923b3eb3cSopenharmony_ci#include <algorithm> 2023b3eb3cSopenharmony_ci#include <cctype> 2123b3eb3cSopenharmony_ci#include <climits> 2223b3eb3cSopenharmony_ci#include <cmath> 2323b3eb3cSopenharmony_ci#include <cstring> 2423b3eb3cSopenharmony_ci#include <iomanip> 2523b3eb3cSopenharmony_ci#include <map> 2623b3eb3cSopenharmony_ci#include <optional> 2723b3eb3cSopenharmony_ci#include <sstream> 2823b3eb3cSopenharmony_ci#include <string> 2923b3eb3cSopenharmony_ci#include <unordered_map> 3023b3eb3cSopenharmony_ci#include <unordered_set> 3123b3eb3cSopenharmony_ci 3223b3eb3cSopenharmony_ci#include "base/error/error_code.h" 3323b3eb3cSopenharmony_ci#include "base/geometry/axis.h" 3423b3eb3cSopenharmony_ci#include "base/json/json_util.h" 3523b3eb3cSopenharmony_ci#include "base/log/log.h" 3623b3eb3cSopenharmony_ci#include "base/resource/asset_manager.h" 3723b3eb3cSopenharmony_ci#include "base/utils/linear_map.h" 3823b3eb3cSopenharmony_ci#include "base/utils/string_utils.h" 3923b3eb3cSopenharmony_ci#include "base/utils/utils.h" 4023b3eb3cSopenharmony_ci#include "core/animation/animation_pub.h" 4123b3eb3cSopenharmony_ci#include "core/animation/curve.h" 4223b3eb3cSopenharmony_ci#include "core/animation/curves.h" 4323b3eb3cSopenharmony_ci#include "core/animation/spring_curve.h" 4423b3eb3cSopenharmony_ci#include "core/common/ime/text_input_action.h" 4523b3eb3cSopenharmony_ci#include "core/common/ime/text_input_type.h" 4623b3eb3cSopenharmony_ci#include "core/components/common/layout/constants.h" 4723b3eb3cSopenharmony_ci#include "core/components/common/properties/clip_path.h" 4823b3eb3cSopenharmony_ci#include "core/components/common/properties/decoration.h" 4923b3eb3cSopenharmony_ci#include "core/components/common/properties/text_style.h" 5023b3eb3cSopenharmony_ci#include "frameworks/bridge/common/dom/dom_type.h" 5123b3eb3cSopenharmony_ci 5223b3eb3cSopenharmony_cinamespace OHOS::Ace::Framework { 5323b3eb3cSopenharmony_ci 5423b3eb3cSopenharmony_ciconstexpr int32_t OFFSET_VALUE_NUMBER = 2; 5523b3eb3cSopenharmony_ciconstexpr uint8_t UTF8_CHARATER_HEAD = 0xc0; 5623b3eb3cSopenharmony_ciconstexpr uint8_t UTF8_CHARATER_BODY = 0x80; 5723b3eb3cSopenharmony_ciconstexpr int32_t UNICODE_LENGTH = 4; 5823b3eb3cSopenharmony_ciconstexpr int32_t STRTOL_BASE = 10; 5923b3eb3cSopenharmony_ciconstexpr int32_t INVALID_PARAM = -1; 6023b3eb3cSopenharmony_ciconstexpr int32_t PLACE_HOLDER_LENGTH = 3; 6123b3eb3cSopenharmony_ci 6223b3eb3cSopenharmony_ciconstexpr char INPUT_ACTION_NEXT[] = "next"; 6323b3eb3cSopenharmony_ciconstexpr char INPUT_ACTION_GO[] = "go"; 6423b3eb3cSopenharmony_ciconstexpr char INPUT_ACTION_DONE[] = "done"; 6523b3eb3cSopenharmony_ciconstexpr char INPUT_ACTION_SEND[] = "send"; 6623b3eb3cSopenharmony_ciconstexpr char INPUT_ACTION_SEARCH[] = "search"; 6723b3eb3cSopenharmony_ciconstexpr char PLURAL_COUNT_POS[] = "{count}"; 6823b3eb3cSopenharmony_ciconstexpr char DEFAULT_PLURAL_CHOICE[] = "other"; 6923b3eb3cSopenharmony_ci 7023b3eb3cSopenharmony_ciconst char DOUBLE_QUOTATION = '"'; 7123b3eb3cSopenharmony_ciconst char BACKSLASH = '\\'; 7223b3eb3cSopenharmony_ciconst char ESCAPE_CHARATER_START = '\x00'; 7323b3eb3cSopenharmony_ciconst char ESCAPE_CHARATER_END = '\x1f'; 7423b3eb3cSopenharmony_ciconst char UNICODE_BODY = '0'; 7523b3eb3cSopenharmony_ciconst char UNICODE_HEAD[] = "\\u"; 7623b3eb3cSopenharmony_ciconst char LEFT_CURLY_BRACES = '{'; 7723b3eb3cSopenharmony_ciconst char RIGHT_CURLY_BRACES = '}'; 7823b3eb3cSopenharmony_ci 7923b3eb3cSopenharmony_citemplate<class T> 8023b3eb3cSopenharmony_cibool GetAssetContentImpl(const RefPtr<AssetManager>& assetManager, const std::string& url, T& content) 8123b3eb3cSopenharmony_ci{ 8223b3eb3cSopenharmony_ci if (!assetManager) { 8323b3eb3cSopenharmony_ci return false; 8423b3eb3cSopenharmony_ci } 8523b3eb3cSopenharmony_ci auto jsAsset = assetManager->GetAsset(url); 8623b3eb3cSopenharmony_ci if (jsAsset == nullptr) { 8723b3eb3cSopenharmony_ci return false; 8823b3eb3cSopenharmony_ci } 8923b3eb3cSopenharmony_ci auto bufLen = jsAsset->GetSize(); 9023b3eb3cSopenharmony_ci auto buffer = jsAsset->GetData(); 9123b3eb3cSopenharmony_ci if ((buffer == nullptr) || (bufLen <= 0)) { 9223b3eb3cSopenharmony_ci return false; 9323b3eb3cSopenharmony_ci } 9423b3eb3cSopenharmony_ci content.assign(buffer, buffer + bufLen); 9523b3eb3cSopenharmony_ci return true; 9623b3eb3cSopenharmony_ci} 9723b3eb3cSopenharmony_ci 9823b3eb3cSopenharmony_citemplate<class T> 9923b3eb3cSopenharmony_cibool GetAssetContentAllowEmpty(const RefPtr<AssetManager>& assetManager, const std::string& url, T& content) 10023b3eb3cSopenharmony_ci{ 10123b3eb3cSopenharmony_ci if (!assetManager) { 10223b3eb3cSopenharmony_ci return false; 10323b3eb3cSopenharmony_ci } 10423b3eb3cSopenharmony_ci auto jsAsset = assetManager->GetAsset(url); 10523b3eb3cSopenharmony_ci if (jsAsset == nullptr) { 10623b3eb3cSopenharmony_ci return false; 10723b3eb3cSopenharmony_ci } 10823b3eb3cSopenharmony_ci auto bufLen = jsAsset->GetSize(); 10923b3eb3cSopenharmony_ci auto buffer = jsAsset->GetData(); 11023b3eb3cSopenharmony_ci content.assign(buffer, buffer + bufLen); 11123b3eb3cSopenharmony_ci return true; 11223b3eb3cSopenharmony_ci} 11323b3eb3cSopenharmony_ci 11423b3eb3cSopenharmony_ciinline std::string GetAssetPathImpl(const RefPtr<AssetManager>& assetManager, const std::string& url) 11523b3eb3cSopenharmony_ci{ 11623b3eb3cSopenharmony_ci if (!assetManager) { 11723b3eb3cSopenharmony_ci return {}; 11823b3eb3cSopenharmony_ci } 11923b3eb3cSopenharmony_ci return assetManager->GetAssetPath(url, true); 12023b3eb3cSopenharmony_ci} 12123b3eb3cSopenharmony_ci 12223b3eb3cSopenharmony_ciinline std::unique_ptr<JsonValue> ParseFileData(const std::string& data) 12323b3eb3cSopenharmony_ci{ 12423b3eb3cSopenharmony_ci const char* endMsg = nullptr; 12523b3eb3cSopenharmony_ci auto fileData = JsonUtil::ParseJsonString(data, &endMsg); 12623b3eb3cSopenharmony_ci if (!fileData) { 12723b3eb3cSopenharmony_ci return nullptr; 12823b3eb3cSopenharmony_ci } 12923b3eb3cSopenharmony_ci return fileData; 13023b3eb3cSopenharmony_ci} 13123b3eb3cSopenharmony_ci 13223b3eb3cSopenharmony_ciinline double StringToDouble(const std::string& value) 13323b3eb3cSopenharmony_ci{ 13423b3eb3cSopenharmony_ci return StringUtils::StringToDouble(value); 13523b3eb3cSopenharmony_ci} 13623b3eb3cSopenharmony_ci 13723b3eb3cSopenharmony_ciinline Dimension StringToDimension(const std::string& value) 13823b3eb3cSopenharmony_ci{ 13923b3eb3cSopenharmony_ci return StringUtils::StringToDimension(value); 14023b3eb3cSopenharmony_ci} 14123b3eb3cSopenharmony_ci 14223b3eb3cSopenharmony_ciinline Dimension StringToDimensionWithUnit(const std::string& value, DimensionUnit defaultUnit = DimensionUnit::PX) 14323b3eb3cSopenharmony_ci{ 14423b3eb3cSopenharmony_ci return StringUtils::StringToDimensionWithUnit(value, defaultUnit); 14523b3eb3cSopenharmony_ci} 14623b3eb3cSopenharmony_ci 14723b3eb3cSopenharmony_ciinline int32_t StringToInt(const std::string& value) 14823b3eb3cSopenharmony_ci{ 14923b3eb3cSopenharmony_ci return StringUtils::StringToInt(value); 15023b3eb3cSopenharmony_ci} 15123b3eb3cSopenharmony_ci 15223b3eb3cSopenharmony_ciinline bool StringToBool(const std::string& value) 15323b3eb3cSopenharmony_ci{ 15423b3eb3cSopenharmony_ci return value == "true"; 15523b3eb3cSopenharmony_ci} 15623b3eb3cSopenharmony_ci 15723b3eb3cSopenharmony_ciinline BorderStyle ConvertStrToBorderStyle(const std::string& style) 15823b3eb3cSopenharmony_ci{ 15923b3eb3cSopenharmony_ci static const LinearMapNode<BorderStyle> borderStyleTable[] = { 16023b3eb3cSopenharmony_ci { "dashed", BorderStyle::DASHED }, 16123b3eb3cSopenharmony_ci { "dotted", BorderStyle::DOTTED }, 16223b3eb3cSopenharmony_ci { "solid", BorderStyle::SOLID }, 16323b3eb3cSopenharmony_ci }; 16423b3eb3cSopenharmony_ci 16523b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(borderStyleTable, ArraySize(borderStyleTable), style.c_str()); 16623b3eb3cSopenharmony_ci return index < 0 ? BorderStyle::NONE : borderStyleTable[index].value; 16723b3eb3cSopenharmony_ci} 16823b3eb3cSopenharmony_ci 16923b3eb3cSopenharmony_ciinline BorderImageRepeat ConvertStrToBorderImageRepeat(const std::string& repeat) 17023b3eb3cSopenharmony_ci{ 17123b3eb3cSopenharmony_ci static const LinearMapNode<BorderImageRepeat> borderImageRepeatTable[] = { 17223b3eb3cSopenharmony_ci { "repeat", BorderImageRepeat::REPEAT }, 17323b3eb3cSopenharmony_ci { "round", BorderImageRepeat::ROUND }, 17423b3eb3cSopenharmony_ci { "space", BorderImageRepeat::SPACE }, 17523b3eb3cSopenharmony_ci { "stretch", BorderImageRepeat::STRETCH }, 17623b3eb3cSopenharmony_ci }; 17723b3eb3cSopenharmony_ci 17823b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(borderImageRepeatTable, ArraySize(borderImageRepeatTable), repeat.c_str()); 17923b3eb3cSopenharmony_ci return index < 0 ? BorderImageRepeat::STRETCH : borderImageRepeatTable[index].value; 18023b3eb3cSopenharmony_ci} 18123b3eb3cSopenharmony_ci 18223b3eb3cSopenharmony_ciinline BadgePosition ConvertStrToBadgePosition(const std::string& badgePosition) 18323b3eb3cSopenharmony_ci{ 18423b3eb3cSopenharmony_ci static const LinearMapNode<BadgePosition> badgePositionTable[] = { 18523b3eb3cSopenharmony_ci { "left", BadgePosition::LEFT }, 18623b3eb3cSopenharmony_ci { "right", BadgePosition::RIGHT }, 18723b3eb3cSopenharmony_ci { "rightTop", BadgePosition::RIGHT_TOP }, 18823b3eb3cSopenharmony_ci }; 18923b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(badgePositionTable, ArraySize(badgePositionTable), badgePosition.c_str()); 19023b3eb3cSopenharmony_ci return index < 0 ? BadgePosition::RIGHT_TOP : badgePositionTable[index].value; 19123b3eb3cSopenharmony_ci} 19223b3eb3cSopenharmony_ci 19323b3eb3cSopenharmony_ciinline BoxSizing ConvertStrToBoxSizing(const std::string& value) 19423b3eb3cSopenharmony_ci{ 19523b3eb3cSopenharmony_ci static const LinearMapNode<BoxSizing> boxSizingTable[] = { 19623b3eb3cSopenharmony_ci { "border-box", BoxSizing::BORDER_BOX }, 19723b3eb3cSopenharmony_ci { "content-box", BoxSizing::CONTENT_BOX }, 19823b3eb3cSopenharmony_ci }; 19923b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(boxSizingTable, ArraySize(boxSizingTable), value.c_str()); 20023b3eb3cSopenharmony_ci return index < 0 ? BoxSizing::BORDER_BOX : boxSizingTable[index].value; 20123b3eb3cSopenharmony_ci} 20223b3eb3cSopenharmony_ci 20323b3eb3cSopenharmony_ciinline ImageRepeat ConvertStrToImageRepeat(const std::string& repeat) 20423b3eb3cSopenharmony_ci{ 20523b3eb3cSopenharmony_ci static const LinearMapNode<ImageRepeat> imageRepeatTable[] = { 20623b3eb3cSopenharmony_ci { "no-repeat", ImageRepeat::NO_REPEAT }, 20723b3eb3cSopenharmony_ci { "repeat", ImageRepeat::REPEAT }, 20823b3eb3cSopenharmony_ci { "repeat-x", ImageRepeat::REPEAT_X }, 20923b3eb3cSopenharmony_ci { "repeat-y", ImageRepeat::REPEAT_Y }, 21023b3eb3cSopenharmony_ci }; 21123b3eb3cSopenharmony_ci 21223b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(imageRepeatTable, ArraySize(imageRepeatTable), repeat.c_str()); 21323b3eb3cSopenharmony_ci return index < 0 ? ImageRepeat::NO_REPEAT : imageRepeatTable[index].value; 21423b3eb3cSopenharmony_ci} 21523b3eb3cSopenharmony_ci 21623b3eb3cSopenharmony_ciinline std::pair<bool, FontWeight> ParseFontWeight( 21723b3eb3cSopenharmony_ci const std::string& weight, FontWeight defaultFontWeight = FontWeight::NORMAL) 21823b3eb3cSopenharmony_ci{ 21923b3eb3cSopenharmony_ci return StringUtils::ParseFontWeight(weight, defaultFontWeight); 22023b3eb3cSopenharmony_ci} 22123b3eb3cSopenharmony_ci 22223b3eb3cSopenharmony_ciinline FontWeight ConvertStrToFontWeight(const std::string& weight, FontWeight defaultFontWeight = FontWeight::NORMAL) 22323b3eb3cSopenharmony_ci{ 22423b3eb3cSopenharmony_ci return StringUtils::StringToFontWeight(weight, defaultFontWeight); 22523b3eb3cSopenharmony_ci} 22623b3eb3cSopenharmony_ci 22723b3eb3cSopenharmony_ciinline TextDecoration ConvertStrToTextDecoration(const std::string& textDecoration) 22823b3eb3cSopenharmony_ci{ 22923b3eb3cSopenharmony_ci // this map should be sorted by key. 23023b3eb3cSopenharmony_ci static const LinearMapNode<TextDecoration> textDecorationTable[] = { 23123b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_INHERIT, TextDecoration::INHERIT }, 23223b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_LINETHROUGH, TextDecoration::LINE_THROUGH }, 23323b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_NONE, TextDecoration::NONE }, 23423b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_OVERLINE, TextDecoration::OVERLINE }, 23523b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_UNDERLINE, TextDecoration::UNDERLINE }, 23623b3eb3cSopenharmony_ci }; 23723b3eb3cSopenharmony_ci 23823b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(textDecorationTable, ArraySize(textDecorationTable), textDecoration.c_str()); 23923b3eb3cSopenharmony_ci return index < 0 ? TextDecoration::NONE : textDecorationTable[index].value; 24023b3eb3cSopenharmony_ci} 24123b3eb3cSopenharmony_ci 24223b3eb3cSopenharmony_ciinline TextDecorationStyle ConvertStrToTextDecorationStyle(const std::string& textDecorationStyle) 24323b3eb3cSopenharmony_ci{ 24423b3eb3cSopenharmony_ci // this map should be sorted by key. 24523b3eb3cSopenharmony_ci static const LinearMapNode<TextDecorationStyle> textDecorationStyleTable[] = { 24623b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_STYLE_DASHED, TextDecorationStyle::DASHED }, 24723b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_STYLE_DOTTED, TextDecorationStyle::DOTTED }, 24823b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_STYLE_DOUBLE, TextDecorationStyle::DOUBLE }, 24923b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_STYLE_SOLID, TextDecorationStyle::SOLID }, 25023b3eb3cSopenharmony_ci { DOM_TEXT_DECORATION_STYLE_WAVY, TextDecorationStyle::WAVY }, 25123b3eb3cSopenharmony_ci }; 25223b3eb3cSopenharmony_ci 25323b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex( 25423b3eb3cSopenharmony_ci textDecorationStyleTable, ArraySize(textDecorationStyleTable), textDecorationStyle.c_str()); 25523b3eb3cSopenharmony_ci return index < 0 ? TextDecorationStyle::SOLID : textDecorationStyleTable[index].value; 25623b3eb3cSopenharmony_ci} 25723b3eb3cSopenharmony_ci 25823b3eb3cSopenharmony_ciinline WhiteSpace ConvertStrToWhiteSpace(const std::string& whiteSpace) 25923b3eb3cSopenharmony_ci{ 26023b3eb3cSopenharmony_ci // this map should be sorted by key. 26123b3eb3cSopenharmony_ci static const LinearMapNode<WhiteSpace> whiteSpaceTable[] = { 26223b3eb3cSopenharmony_ci { DOM_WHITE_SPACE_INHERIT, WhiteSpace::INHERIT }, 26323b3eb3cSopenharmony_ci { DOM_WHITE_SPACE_NORMAL, WhiteSpace::NORMAL }, 26423b3eb3cSopenharmony_ci { DOM_WHITE_SPACE_NOWRAP, WhiteSpace::NOWRAP }, 26523b3eb3cSopenharmony_ci { DOM_WHITE_SPACE_PRE, WhiteSpace::PRE }, 26623b3eb3cSopenharmony_ci { DOM_WHITE_SPACE_PRELINE, WhiteSpace::PRE_LINE }, 26723b3eb3cSopenharmony_ci { DOM_WHITE_SPACE_PREWRAP, WhiteSpace::PRE_WRAP }, 26823b3eb3cSopenharmony_ci }; 26923b3eb3cSopenharmony_ci 27023b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(whiteSpaceTable, ArraySize(whiteSpaceTable), whiteSpace.c_str()); 27123b3eb3cSopenharmony_ci return index < 0 ? WhiteSpace::NORMAL : whiteSpaceTable[index].value; 27223b3eb3cSopenharmony_ci} 27323b3eb3cSopenharmony_ci 27423b3eb3cSopenharmony_ciinline VerticalAlign ConvertStrToTextVerticalAlign(const std::string& align) 27523b3eb3cSopenharmony_ci{ 27623b3eb3cSopenharmony_ci static const LinearMapNode<VerticalAlign> textVerticalAlignTable[] = { 27723b3eb3cSopenharmony_ci { DOM_BOTTOM, VerticalAlign::BOTTOM }, 27823b3eb3cSopenharmony_ci { DOM_MIDDLE, VerticalAlign::CENTER }, 27923b3eb3cSopenharmony_ci { DOM_TOP, VerticalAlign::TOP }, 28023b3eb3cSopenharmony_ci }; 28123b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(textVerticalAlignTable, ArraySize(textVerticalAlignTable), align.c_str()); 28223b3eb3cSopenharmony_ci return index < 0 ? VerticalAlign::NONE : textVerticalAlignTable[index].value; 28323b3eb3cSopenharmony_ci} 28423b3eb3cSopenharmony_ci 28523b3eb3cSopenharmony_ciinline FontStyle ConvertStrToFontStyle(const std::string& fontStyle) 28623b3eb3cSopenharmony_ci{ 28723b3eb3cSopenharmony_ci return fontStyle == DOM_TEXT_FONT_STYLE_ITALIC ? FontStyle::ITALIC : FontStyle::NORMAL; 28823b3eb3cSopenharmony_ci} 28923b3eb3cSopenharmony_ci 29023b3eb3cSopenharmony_ciinline TextAlign ConvertStrToTextAlign(const std::string& align) 29123b3eb3cSopenharmony_ci{ 29223b3eb3cSopenharmony_ci static const LinearMapNode<TextAlign> textAlignTable[] = { 29323b3eb3cSopenharmony_ci { DOM_CENTER, TextAlign::CENTER }, 29423b3eb3cSopenharmony_ci { DOM_END, TextAlign::END }, 29523b3eb3cSopenharmony_ci { DOM_LEFT, TextAlign::LEFT }, 29623b3eb3cSopenharmony_ci { DOM_RIGHT, TextAlign::RIGHT }, 29723b3eb3cSopenharmony_ci { DOM_START, TextAlign::START }, 29823b3eb3cSopenharmony_ci }; 29923b3eb3cSopenharmony_ci 30023b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(textAlignTable, ArraySize(textAlignTable), align.c_str()); 30123b3eb3cSopenharmony_ci return index < 0 ? TextAlign::CENTER : textAlignTable[index].value; 30223b3eb3cSopenharmony_ci} 30323b3eb3cSopenharmony_ci 30423b3eb3cSopenharmony_ciinline TextOverflow ConvertStrToTextOverflow(const std::string& overflow) 30523b3eb3cSopenharmony_ci{ 30623b3eb3cSopenharmony_ci return overflow == DOM_ELLIPSIS ? TextOverflow::ELLIPSIS : TextOverflow::CLIP; 30723b3eb3cSopenharmony_ci} 30823b3eb3cSopenharmony_ci 30923b3eb3cSopenharmony_ciinline Overflow ConvertStrToOverflow(const std::string& val) 31023b3eb3cSopenharmony_ci{ 31123b3eb3cSopenharmony_ci const LinearMapNode<Overflow> overflowTable[] = { 31223b3eb3cSopenharmony_ci { "auto", Overflow::SCROLL }, 31323b3eb3cSopenharmony_ci { "hidden", Overflow::FORCE_CLIP }, 31423b3eb3cSopenharmony_ci { "scroll", Overflow::SCROLL }, 31523b3eb3cSopenharmony_ci { "visible", Overflow::OBSERVABLE }, 31623b3eb3cSopenharmony_ci }; 31723b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(overflowTable, ArraySize(overflowTable), val.c_str()); 31823b3eb3cSopenharmony_ci return index < 0 ? Overflow::CLIP : overflowTable[index].value; 31923b3eb3cSopenharmony_ci} 32023b3eb3cSopenharmony_ci 32123b3eb3cSopenharmony_ciinline TextDirection ConvertStrToTextDirection(const std::string& val) 32223b3eb3cSopenharmony_ci{ 32323b3eb3cSopenharmony_ci const LinearMapNode<TextDirection> textDirectionTable[] = { 32423b3eb3cSopenharmony_ci { "inherit", TextDirection::INHERIT }, 32523b3eb3cSopenharmony_ci { "ltr", TextDirection::LTR }, 32623b3eb3cSopenharmony_ci { "rtl", TextDirection::RTL }, 32723b3eb3cSopenharmony_ci }; 32823b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(textDirectionTable, ArraySize(textDirectionTable), val.c_str()); 32923b3eb3cSopenharmony_ci return index < 0 ? TextDirection::LTR : textDirectionTable[index].value; 33023b3eb3cSopenharmony_ci} 33123b3eb3cSopenharmony_ci 33223b3eb3cSopenharmony_ciinline TextFieldOverflowX ConvertStrToTextFieldOverflowX(const std::string& val) 33323b3eb3cSopenharmony_ci{ 33423b3eb3cSopenharmony_ci const LinearMapNode<TextFieldOverflowX> textFieldOverflowXTable[] = { 33523b3eb3cSopenharmony_ci { "auto", TextFieldOverflowX::AUTO }, 33623b3eb3cSopenharmony_ci { "hidden", TextFieldOverflowX::HIDDEN }, 33723b3eb3cSopenharmony_ci { "scroll", TextFieldOverflowX::SCROLL }, 33823b3eb3cSopenharmony_ci }; 33923b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(textFieldOverflowXTable, ArraySize(textFieldOverflowXTable), val.c_str()); 34023b3eb3cSopenharmony_ci return index < 0 ? TextFieldOverflowX::HIDDEN : textFieldOverflowXTable[index].value; 34123b3eb3cSopenharmony_ci} 34223b3eb3cSopenharmony_ci 34323b3eb3cSopenharmony_ciinline std::vector<std::string> ConvertStrToFontFamilies(const std::string& family) 34423b3eb3cSopenharmony_ci{ 34523b3eb3cSopenharmony_ci std::vector<std::string> fontFamilies; 34623b3eb3cSopenharmony_ci std::stringstream stream(family); 34723b3eb3cSopenharmony_ci std::string fontFamily; 34823b3eb3cSopenharmony_ci while (getline(stream, fontFamily, ',')) { 34923b3eb3cSopenharmony_ci fontFamilies.emplace_back(fontFamily); 35023b3eb3cSopenharmony_ci } 35123b3eb3cSopenharmony_ci return fontFamilies; 35223b3eb3cSopenharmony_ci} 35323b3eb3cSopenharmony_ci 35423b3eb3cSopenharmony_ciinline FlexDirection ConvertStrToFlexDirection(const std::string& flexKey) 35523b3eb3cSopenharmony_ci{ 35623b3eb3cSopenharmony_ci return flexKey == DOM_FLEX_COLUMN ? FlexDirection::COLUMN : FlexDirection::ROW; 35723b3eb3cSopenharmony_ci} 35823b3eb3cSopenharmony_ci 35923b3eb3cSopenharmony_ciinline FlexAlign ConvertStrToFlexAlign(const std::string& flexKey) 36023b3eb3cSopenharmony_ci{ 36123b3eb3cSopenharmony_ci static const LinearMapNode<FlexAlign> flexMap[] = { 36223b3eb3cSopenharmony_ci { DOM_ALIGN_ITEMS_BASELINE, FlexAlign::BASELINE }, 36323b3eb3cSopenharmony_ci { DOM_JUSTIFY_CONTENT_CENTER, FlexAlign::CENTER }, 36423b3eb3cSopenharmony_ci { DOM_JUSTIFY_CONTENT_END, FlexAlign::FLEX_END }, 36523b3eb3cSopenharmony_ci { DOM_JUSTIFY_CONTENT_START, FlexAlign::FLEX_START }, 36623b3eb3cSopenharmony_ci { DOM_JUSTIFY_CONTENT_AROUND, FlexAlign::SPACE_AROUND }, 36723b3eb3cSopenharmony_ci { DOM_JUSTIFY_CONTENT_BETWEEN, FlexAlign::SPACE_BETWEEN }, 36823b3eb3cSopenharmony_ci { DOM_JUSTIFY_CONTENT_EVENLY, FlexAlign::SPACE_EVENLY }, 36923b3eb3cSopenharmony_ci { DOM_ALIGN_ITEMS_STRETCH, FlexAlign::STRETCH }, 37023b3eb3cSopenharmony_ci }; 37123b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(flexMap, ArraySize(flexMap), flexKey.c_str()); 37223b3eb3cSopenharmony_ci return index < 0 ? FlexAlign::FLEX_START : flexMap[index].value; 37323b3eb3cSopenharmony_ci} 37423b3eb3cSopenharmony_ci 37523b3eb3cSopenharmony_ciinline Offset ConvertStrToOffset(const std::string& value) 37623b3eb3cSopenharmony_ci{ 37723b3eb3cSopenharmony_ci Offset offset; 37823b3eb3cSopenharmony_ci std::vector<std::string> offsetValues; 37923b3eb3cSopenharmony_ci std::stringstream stream(value); 38023b3eb3cSopenharmony_ci std::string offsetValue; 38123b3eb3cSopenharmony_ci while (getline(stream, offsetValue, ' ')) { 38223b3eb3cSopenharmony_ci offsetValues.emplace_back(offsetValue); 38323b3eb3cSopenharmony_ci } 38423b3eb3cSopenharmony_ci // To avoid illegal input, such as "100px ". 38523b3eb3cSopenharmony_ci offsetValues.resize(OFFSET_VALUE_NUMBER); 38623b3eb3cSopenharmony_ci offset.SetX(StringToDouble(offsetValues[0])); 38723b3eb3cSopenharmony_ci offset.SetY(StringToDouble(offsetValues[1])); 38823b3eb3cSopenharmony_ci return offset; 38923b3eb3cSopenharmony_ci} 39023b3eb3cSopenharmony_ci 39123b3eb3cSopenharmony_ciinline QrcodeType ConvertStrToQrcodeType(const std::string& value) 39223b3eb3cSopenharmony_ci{ 39323b3eb3cSopenharmony_ci return value == "circle" ? QrcodeType::CIRCLE : QrcodeType::RECT; 39423b3eb3cSopenharmony_ci} 39523b3eb3cSopenharmony_ci 39623b3eb3cSopenharmony_ciinline AnimationCurve ConvertStrToAnimationCurve(const std::string& value) 39723b3eb3cSopenharmony_ci{ 39823b3eb3cSopenharmony_ci return value == "standard" ? AnimationCurve::STANDARD : AnimationCurve::FRICTION; 39923b3eb3cSopenharmony_ci} 40023b3eb3cSopenharmony_ci 40123b3eb3cSopenharmony_ciinline TextInputAction ConvertStrToTextInputAction(const std::string& action) 40223b3eb3cSopenharmony_ci{ 40323b3eb3cSopenharmony_ci TextInputAction inputAction; 40423b3eb3cSopenharmony_ci if (action == INPUT_ACTION_NEXT) { 40523b3eb3cSopenharmony_ci inputAction = TextInputAction::NEXT; 40623b3eb3cSopenharmony_ci } else if (action == INPUT_ACTION_GO) { 40723b3eb3cSopenharmony_ci inputAction = TextInputAction::GO; 40823b3eb3cSopenharmony_ci } else if (action == INPUT_ACTION_DONE) { 40923b3eb3cSopenharmony_ci inputAction = TextInputAction::DONE; 41023b3eb3cSopenharmony_ci } else if (action == INPUT_ACTION_SEND) { 41123b3eb3cSopenharmony_ci inputAction = TextInputAction::SEND; 41223b3eb3cSopenharmony_ci } else if (action == INPUT_ACTION_SEARCH) { 41323b3eb3cSopenharmony_ci inputAction = TextInputAction::SEARCH; 41423b3eb3cSopenharmony_ci } else { 41523b3eb3cSopenharmony_ci inputAction = TextInputAction::UNSPECIFIED; 41623b3eb3cSopenharmony_ci } 41723b3eb3cSopenharmony_ci return inputAction; 41823b3eb3cSopenharmony_ci} 41923b3eb3cSopenharmony_ci 42023b3eb3cSopenharmony_ciinline TextInputType ConvertStrToTextInputType(const std::string& type) 42123b3eb3cSopenharmony_ci{ 42223b3eb3cSopenharmony_ci TextInputType inputType; 42323b3eb3cSopenharmony_ci if (type == DOM_INPUT_TYPE_NUMBER) { 42423b3eb3cSopenharmony_ci inputType = TextInputType::NUMBER; 42523b3eb3cSopenharmony_ci } else if (type == DOM_INPUT_TYPE_DATE || type == DOM_INPUT_TYPE_TIME) { 42623b3eb3cSopenharmony_ci inputType = TextInputType::DATETIME; 42723b3eb3cSopenharmony_ci } else if (type == DOM_INPUT_TYPE_EMAIL) { 42823b3eb3cSopenharmony_ci inputType = TextInputType::EMAIL_ADDRESS; 42923b3eb3cSopenharmony_ci } else if (type == DOM_INPUT_TYPE_PASSWORD) { 43023b3eb3cSopenharmony_ci inputType = TextInputType::VISIBLE_PASSWORD; 43123b3eb3cSopenharmony_ci } else { 43223b3eb3cSopenharmony_ci inputType = TextInputType::TEXT; 43323b3eb3cSopenharmony_ci } 43423b3eb3cSopenharmony_ci return inputType; 43523b3eb3cSopenharmony_ci} 43623b3eb3cSopenharmony_ci 43723b3eb3cSopenharmony_ciinline TabBarMode ConvertStrToTabBarMode(const std::string& value) 43823b3eb3cSopenharmony_ci{ 43923b3eb3cSopenharmony_ci std::string temp = value; 44023b3eb3cSopenharmony_ci transform(temp.begin(), temp.end(), temp.begin(), tolower); 44123b3eb3cSopenharmony_ci return temp == "fixed" ? TabBarMode::FIXED : TabBarMode::SCROLLABLE; 44223b3eb3cSopenharmony_ci} 44323b3eb3cSopenharmony_ci 44423b3eb3cSopenharmony_ciACE_FORCE_EXPORT RefPtr<Curve> CreateBuiltinCurve(const std::string& aniTimFunc); 44523b3eb3cSopenharmony_ci 44623b3eb3cSopenharmony_ciACE_FORCE_EXPORT RefPtr<Curve> CreateCustomCurve(const std::string& aniTimFunc); 44723b3eb3cSopenharmony_ci 44823b3eb3cSopenharmony_ciACE_FORCE_EXPORT RefPtr<Curve> CreateCurve(const std::function<float(float)>& jsFunc); 44923b3eb3cSopenharmony_ci 45023b3eb3cSopenharmony_ciACE_FORCE_EXPORT RefPtr<Curve> CreateCurve(const std::string& aniTimFunc, bool useDefault = true); 45123b3eb3cSopenharmony_ci 45223b3eb3cSopenharmony_ciACE_FORCE_EXPORT bool ParseCurveParam( 45323b3eb3cSopenharmony_ci const std::string& aniTimFunc, std::string& curveName, std::vector<std::string>& paramsVector); 45423b3eb3cSopenharmony_ci 45523b3eb3cSopenharmony_ciACE_FORCE_EXPORT RefPtr<Curve> CreateCurveExceptSpring( 45623b3eb3cSopenharmony_ci const std::string& aniTimFunc, const std::function<float(float)>& jsFunc = nullptr); 45723b3eb3cSopenharmony_ci 45823b3eb3cSopenharmony_ciACE_EXPORT TransitionType ParseTransitionType(const std::string& transitionType); 45923b3eb3cSopenharmony_ci 46023b3eb3cSopenharmony_ciACE_EXPORT RefPtr<ClipPath> CreateClipPath(const std::string& value); 46123b3eb3cSopenharmony_ci 46223b3eb3cSopenharmony_ciinline FillMode StringToFillMode(const std::string& fillMode) 46323b3eb3cSopenharmony_ci{ 46423b3eb3cSopenharmony_ci if (fillMode == DOM_ANIMATION_FILL_MODE_FORWARDS) { 46523b3eb3cSopenharmony_ci return FillMode::FORWARDS; 46623b3eb3cSopenharmony_ci } else if (fillMode == DOM_ANIMATION_FILL_MODE_BACKWARDS) { 46723b3eb3cSopenharmony_ci return FillMode::BACKWARDS; 46823b3eb3cSopenharmony_ci } else if (fillMode == DOM_ANIMATION_FILL_MODE_BOTH) { 46923b3eb3cSopenharmony_ci return FillMode::BOTH; 47023b3eb3cSopenharmony_ci } else { 47123b3eb3cSopenharmony_ci return FillMode::NONE; 47223b3eb3cSopenharmony_ci } 47323b3eb3cSopenharmony_ci} 47423b3eb3cSopenharmony_ci 47523b3eb3cSopenharmony_ciinline AnimationDirection StringToAnimationDirection(const std::string& direction) 47623b3eb3cSopenharmony_ci{ 47723b3eb3cSopenharmony_ci if (direction == DOM_ANIMATION_DIRECTION_ALTERNATE) { 47823b3eb3cSopenharmony_ci return AnimationDirection::ALTERNATE; 47923b3eb3cSopenharmony_ci } else if (direction == DOM_ANIMATION_DIRECTION_REVERSE) { 48023b3eb3cSopenharmony_ci return AnimationDirection::REVERSE; 48123b3eb3cSopenharmony_ci } else if (direction == DOM_ANIMATION_DIRECTION_ALTERNATE_REVERSE) { 48223b3eb3cSopenharmony_ci return AnimationDirection::ALTERNATE_REVERSE; 48323b3eb3cSopenharmony_ci } else { 48423b3eb3cSopenharmony_ci return AnimationDirection::NORMAL; 48523b3eb3cSopenharmony_ci } 48623b3eb3cSopenharmony_ci} 48723b3eb3cSopenharmony_ci 48823b3eb3cSopenharmony_ciinline AnimationOperation StringToAnimationOperation(const std::string& direction) 48923b3eb3cSopenharmony_ci{ 49023b3eb3cSopenharmony_ci if (direction == DOM_ANIMATION_PLAY_STATE_IDLE) { 49123b3eb3cSopenharmony_ci return AnimationOperation::CANCEL; 49223b3eb3cSopenharmony_ci } else if (direction == DOM_ANIMATION_PLAY_STATE_RUNNING) { 49323b3eb3cSopenharmony_ci return AnimationOperation::RUNNING; 49423b3eb3cSopenharmony_ci } else if (direction == DOM_ANIMATION_PLAY_STATE_PAUSED) { 49523b3eb3cSopenharmony_ci return AnimationOperation::PAUSE; 49623b3eb3cSopenharmony_ci } else if (direction == DOM_ANIMATION_PLAY_STATE_FINISHED) { 49723b3eb3cSopenharmony_ci return AnimationOperation::FINISH; 49823b3eb3cSopenharmony_ci } else { 49923b3eb3cSopenharmony_ci return AnimationOperation::NONE; 50023b3eb3cSopenharmony_ci } 50123b3eb3cSopenharmony_ci} 50223b3eb3cSopenharmony_ci 50323b3eb3cSopenharmony_ciinline void RemoveHeadTailSpace(std::string& value) 50423b3eb3cSopenharmony_ci{ 50523b3eb3cSopenharmony_ci if (!value.empty()) { 50623b3eb3cSopenharmony_ci auto start = value.find_first_not_of(' '); 50723b3eb3cSopenharmony_ci if (start == std::string::npos) { 50823b3eb3cSopenharmony_ci value.clear(); 50923b3eb3cSopenharmony_ci } else { 51023b3eb3cSopenharmony_ci value = value.substr(start, value.find_last_not_of(' ') - start + 1); 51123b3eb3cSopenharmony_ci } 51223b3eb3cSopenharmony_ci } 51323b3eb3cSopenharmony_ci} 51423b3eb3cSopenharmony_ci 51523b3eb3cSopenharmony_ciinline GradientDirection StrToGradientDirection(const std::string& direction) 51623b3eb3cSopenharmony_ci{ 51723b3eb3cSopenharmony_ci static const LinearMapNode<GradientDirection> gradientDirectionTable[] = { 51823b3eb3cSopenharmony_ci { DOM_GRADIENT_DIRECTION_LEFT, GradientDirection::LEFT }, 51923b3eb3cSopenharmony_ci { DOM_GRADIENT_DIRECTION_RIGHT, GradientDirection::RIGHT }, 52023b3eb3cSopenharmony_ci { DOM_GRADIENT_DIRECTION_TOP, GradientDirection::TOP }, 52123b3eb3cSopenharmony_ci { DOM_GRADIENT_DIRECTION_BOTTOM, GradientDirection::BOTTOM }, 52223b3eb3cSopenharmony_ci }; 52323b3eb3cSopenharmony_ci 52423b3eb3cSopenharmony_ci auto index = BinarySearchFindIndex(gradientDirectionTable, ArraySize(gradientDirectionTable), direction.c_str()); 52523b3eb3cSopenharmony_ci return index < 0 ? GradientDirection::BOTTOM : gradientDirectionTable[index].value; 52623b3eb3cSopenharmony_ci} 52723b3eb3cSopenharmony_ci 52823b3eb3cSopenharmony_cistd::string CurveIntToString(int curve); 52923b3eb3cSopenharmony_ci 53023b3eb3cSopenharmony_cibool ParseBackgroundImagePosition(const std::string& value, BackgroundImagePosition& backgroundImagePosition); 53123b3eb3cSopenharmony_ci 53223b3eb3cSopenharmony_cibool ParseBackgroundImageSize(const std::string& value, BackgroundImageSize& backgroundImageSize); 53323b3eb3cSopenharmony_ci 53423b3eb3cSopenharmony_ciImageObjectPosition ParseImageObjectPosition(const std::string& value); 53523b3eb3cSopenharmony_ci 53623b3eb3cSopenharmony_cistd::optional<RadialSizeType> ParseRadialGradientSize(const std::string& value); 53723b3eb3cSopenharmony_ci 53823b3eb3cSopenharmony_ciinline bool StartWith(const std::string& dst, const std::string& prefix) 53923b3eb3cSopenharmony_ci{ 54023b3eb3cSopenharmony_ci return dst.compare(0, prefix.size(), prefix) == 0; 54123b3eb3cSopenharmony_ci} 54223b3eb3cSopenharmony_ci 54323b3eb3cSopenharmony_ciinline bool EndWith(const std::string& dst, const std::string& suffix) 54423b3eb3cSopenharmony_ci{ 54523b3eb3cSopenharmony_ci return dst.size() >= suffix.size() && dst.compare(dst.size() - suffix.size(), suffix.size(), suffix) == 0; 54623b3eb3cSopenharmony_ci} 54723b3eb3cSopenharmony_ci 54823b3eb3cSopenharmony_ciinline double ConvertTimeStr(const std::string& str) 54923b3eb3cSopenharmony_ci{ 55023b3eb3cSopenharmony_ci std::string time(str); 55123b3eb3cSopenharmony_ci StringUtils::TrimStr(time); 55223b3eb3cSopenharmony_ci double result = 0.0; 55323b3eb3cSopenharmony_ci if (EndWith(time, "ms")) { 55423b3eb3cSopenharmony_ci // remove 2 char "ms" 55523b3eb3cSopenharmony_ci result = StringToDouble(std::string(time.begin(), time.end() - 2.0)); 55623b3eb3cSopenharmony_ci } else if (EndWith(time, "s")) { 55723b3eb3cSopenharmony_ci // transform s to ms 55823b3eb3cSopenharmony_ci result = StringToDouble(std::string(time.begin(), time.end() - 1.0)) * 1000.0; 55923b3eb3cSopenharmony_ci } else if (EndWith(time, "m")) { 56023b3eb3cSopenharmony_ci // transform m to ms 56123b3eb3cSopenharmony_ci result = StringToDouble(std::string(time.begin(), time.end() - 1.0)) * 60.0 * 1000.0; 56223b3eb3cSopenharmony_ci } else { 56323b3eb3cSopenharmony_ci result = StringToDouble(str); 56423b3eb3cSopenharmony_ci } 56523b3eb3cSopenharmony_ci return result; 56623b3eb3cSopenharmony_ci} 56723b3eb3cSopenharmony_ci 56823b3eb3cSopenharmony_ciinline WordBreak ConvertStrToWordBreak(const std::string& wordBreak) 56923b3eb3cSopenharmony_ci{ 57023b3eb3cSopenharmony_ci return StringUtils::StringToWordBreak(wordBreak); 57123b3eb3cSopenharmony_ci} 57223b3eb3cSopenharmony_ci 57323b3eb3cSopenharmony_ciinline bool CheckTransformEnum(const std::string& str) 57423b3eb3cSopenharmony_ci{ 57523b3eb3cSopenharmony_ci const static std::unordered_set<std::string> offsetKeywords = { "left", "right", "center", "top", "bottom" }; 57623b3eb3cSopenharmony_ci 57723b3eb3cSopenharmony_ci return offsetKeywords.find(str) != offsetKeywords.end(); 57823b3eb3cSopenharmony_ci} 57923b3eb3cSopenharmony_ci 58023b3eb3cSopenharmony_ciinline std::pair<bool, Dimension> ConvertStrToTransformOrigin(const std::string& str, Axis axis) 58123b3eb3cSopenharmony_ci{ 58223b3eb3cSopenharmony_ci const static std::unordered_map<std::string, Dimension> xOffsetKeywords = { 58323b3eb3cSopenharmony_ci { "left", 0.0_pct }, 58423b3eb3cSopenharmony_ci { "right", 1.0_pct }, 58523b3eb3cSopenharmony_ci { "center", 0.5_pct }, 58623b3eb3cSopenharmony_ci }; 58723b3eb3cSopenharmony_ci const static std::unordered_map<std::string, Dimension> yOffsetKeywords = { 58823b3eb3cSopenharmony_ci { "top", 0.0_pct }, 58923b3eb3cSopenharmony_ci { "bottom", 1.0_pct }, 59023b3eb3cSopenharmony_ci { "center", 0.5_pct }, 59123b3eb3cSopenharmony_ci }; 59223b3eb3cSopenharmony_ci 59323b3eb3cSopenharmony_ci if (axis == Axis::HORIZONTAL) { 59423b3eb3cSopenharmony_ci auto pos = xOffsetKeywords.find(str); 59523b3eb3cSopenharmony_ci if (pos != xOffsetKeywords.end()) { 59623b3eb3cSopenharmony_ci return std::make_pair(true, pos->second); 59723b3eb3cSopenharmony_ci } 59823b3eb3cSopenharmony_ci } else if (axis == Axis::VERTICAL) { 59923b3eb3cSopenharmony_ci auto pos = yOffsetKeywords.find(str); 60023b3eb3cSopenharmony_ci if (pos != yOffsetKeywords.end()) { 60123b3eb3cSopenharmony_ci return std::make_pair(true, pos->second); 60223b3eb3cSopenharmony_ci } 60323b3eb3cSopenharmony_ci } 60423b3eb3cSopenharmony_ci 60523b3eb3cSopenharmony_ci return std::make_pair(false, Dimension {}); 60623b3eb3cSopenharmony_ci} 60723b3eb3cSopenharmony_ci 60823b3eb3cSopenharmony_ciinline int32_t ParseUtf8TextLength(std::string& text) 60923b3eb3cSopenharmony_ci{ 61023b3eb3cSopenharmony_ci int32_t trueLength = 0; 61123b3eb3cSopenharmony_ci int32_t stringLength = 0; 61223b3eb3cSopenharmony_ci while (stringLength < static_cast<int32_t>(text.length())) { 61323b3eb3cSopenharmony_ci uint8_t stringChar = static_cast<uint8_t>(text[stringLength++]); 61423b3eb3cSopenharmony_ci if ((stringChar & UTF8_CHARATER_HEAD) != UTF8_CHARATER_BODY) { 61523b3eb3cSopenharmony_ci trueLength++; 61623b3eb3cSopenharmony_ci } 61723b3eb3cSopenharmony_ci } 61823b3eb3cSopenharmony_ci return trueLength; 61923b3eb3cSopenharmony_ci} 62023b3eb3cSopenharmony_ci 62123b3eb3cSopenharmony_ciinline int32_t ParseUtf8TextSubstrStartPos(std::string& text, int32_t targetPos) 62223b3eb3cSopenharmony_ci{ 62323b3eb3cSopenharmony_ci int32_t truePos = 0; 62423b3eb3cSopenharmony_ci int32_t stringPos = 0; 62523b3eb3cSopenharmony_ci while ((stringPos < static_cast<int32_t>(text.length())) && (truePos < targetPos)) { 62623b3eb3cSopenharmony_ci uint8_t stringChar = static_cast<uint8_t>(text[stringPos++]); 62723b3eb3cSopenharmony_ci if ((stringChar & UTF8_CHARATER_HEAD) != UTF8_CHARATER_BODY) { 62823b3eb3cSopenharmony_ci truePos++; 62923b3eb3cSopenharmony_ci } 63023b3eb3cSopenharmony_ci } 63123b3eb3cSopenharmony_ci return stringPos; 63223b3eb3cSopenharmony_ci} 63323b3eb3cSopenharmony_ci 63423b3eb3cSopenharmony_ciinline int32_t ParseUtf8TextSubstrEndPos(std::string& text, int32_t targetPos) 63523b3eb3cSopenharmony_ci{ 63623b3eb3cSopenharmony_ci auto stringPos = ParseUtf8TextSubstrStartPos(text, targetPos); 63723b3eb3cSopenharmony_ci while (stringPos < static_cast<int32_t>(text.length())) { 63823b3eb3cSopenharmony_ci uint8_t stringChar = static_cast<uint8_t>(text[stringPos]); 63923b3eb3cSopenharmony_ci if ((stringChar & UTF8_CHARATER_HEAD) != UTF8_CHARATER_BODY) { 64023b3eb3cSopenharmony_ci break; 64123b3eb3cSopenharmony_ci } 64223b3eb3cSopenharmony_ci stringPos++; 64323b3eb3cSopenharmony_ci } 64423b3eb3cSopenharmony_ci return stringPos; 64523b3eb3cSopenharmony_ci} 64623b3eb3cSopenharmony_ci 64723b3eb3cSopenharmony_ciinline void HandleEscapeCharaterInUtf8TextForJson(std::string& text) 64823b3eb3cSopenharmony_ci{ 64923b3eb3cSopenharmony_ci for (size_t pos = 0; pos < text.size(); pos++) { 65023b3eb3cSopenharmony_ci if ((text.at(pos) == DOUBLE_QUOTATION) || (text.at(pos) == BACKSLASH) || 65123b3eb3cSopenharmony_ci ((text.at(pos) >= ESCAPE_CHARATER_START) && (text.at(pos) <= ESCAPE_CHARATER_END))) { 65223b3eb3cSopenharmony_ci std::ostringstream is; 65323b3eb3cSopenharmony_ci is << UNICODE_HEAD << std::hex << std::setw(UNICODE_LENGTH) << std::setfill(UNICODE_BODY) 65423b3eb3cSopenharmony_ci << int(text.at(pos)); 65523b3eb3cSopenharmony_ci text.replace(pos, 1, is.str()); 65623b3eb3cSopenharmony_ci } 65723b3eb3cSopenharmony_ci } 65823b3eb3cSopenharmony_ci} 65923b3eb3cSopenharmony_ci 66023b3eb3cSopenharmony_ciinline int32_t ParseResourceInputNumberParam(const std::string& param) 66123b3eb3cSopenharmony_ci{ 66223b3eb3cSopenharmony_ci if (!StringUtils::IsNumber(param) || param.empty()) { 66323b3eb3cSopenharmony_ci return INVALID_PARAM; 66423b3eb3cSopenharmony_ci } else { 66523b3eb3cSopenharmony_ci errno = 0; 66623b3eb3cSopenharmony_ci char* pEnd = nullptr; 66723b3eb3cSopenharmony_ci int64_t result = std::strtol(param.c_str(), &pEnd, STRTOL_BASE); 66823b3eb3cSopenharmony_ci return ((result < INT_MIN || result > INT_MAX) || errno == ERANGE) ? INT_MAX : static_cast<int32_t>(result); 66923b3eb3cSopenharmony_ci } 67023b3eb3cSopenharmony_ci} 67123b3eb3cSopenharmony_ci 67223b3eb3cSopenharmony_ciinline void ReplacePlaceHolderArray(std::string& resultStr, const std::vector<std::string>& arrayResult) 67323b3eb3cSopenharmony_ci{ 67423b3eb3cSopenharmony_ci auto size = arrayResult.size(); 67523b3eb3cSopenharmony_ci size_t startPos = 0; 67623b3eb3cSopenharmony_ci for (size_t i = 0; i < size; i++) { 67723b3eb3cSopenharmony_ci std::string placeHolder; 67823b3eb3cSopenharmony_ci placeHolder += LEFT_CURLY_BRACES; 67923b3eb3cSopenharmony_ci placeHolder += (i + '0'); 68023b3eb3cSopenharmony_ci placeHolder += RIGHT_CURLY_BRACES; 68123b3eb3cSopenharmony_ci if (startPos < resultStr.length()) { 68223b3eb3cSopenharmony_ci auto pos = resultStr.find(placeHolder, startPos); 68323b3eb3cSopenharmony_ci if (pos != std::string::npos) { 68423b3eb3cSopenharmony_ci resultStr.replace(pos, PLACE_HOLDER_LENGTH, arrayResult[i]); 68523b3eb3cSopenharmony_ci startPos = pos + arrayResult[i].length(); 68623b3eb3cSopenharmony_ci } 68723b3eb3cSopenharmony_ci } 68823b3eb3cSopenharmony_ci } 68923b3eb3cSopenharmony_ci} 69023b3eb3cSopenharmony_ci 69123b3eb3cSopenharmony_ciinline void ReplacePlaceHolder(std::string& resultStr, const std::unique_ptr<JsonValue>& argsPtr) 69223b3eb3cSopenharmony_ci{ 69323b3eb3cSopenharmony_ci auto placeHolderKey = argsPtr->GetChild()->GetKey(); 69423b3eb3cSopenharmony_ci std::string placeHolder; 69523b3eb3cSopenharmony_ci placeHolder += LEFT_CURLY_BRACES; 69623b3eb3cSopenharmony_ci placeHolder += placeHolderKey; 69723b3eb3cSopenharmony_ci placeHolder += RIGHT_CURLY_BRACES; 69823b3eb3cSopenharmony_ci auto pos = resultStr.find(placeHolder); 69923b3eb3cSopenharmony_ci if (pos != std::string::npos) { 70023b3eb3cSopenharmony_ci resultStr.replace(pos, placeHolder.length(), argsPtr->GetChild()->GetString()); 70123b3eb3cSopenharmony_ci } 70223b3eb3cSopenharmony_ci} 70323b3eb3cSopenharmony_ci 70423b3eb3cSopenharmony_ciinline std::string ParserPluralResource( 70523b3eb3cSopenharmony_ci const std::unique_ptr<JsonValue>& argsPtr, const std::string& choice, const std::string& count) 70623b3eb3cSopenharmony_ci{ 70723b3eb3cSopenharmony_ci std::string valueStr; 70823b3eb3cSopenharmony_ci std::string defaultPluralStr(DEFAULT_PLURAL_CHOICE); 70923b3eb3cSopenharmony_ci if (argsPtr->Contains(choice)) { 71023b3eb3cSopenharmony_ci valueStr = argsPtr->GetValue(choice)->GetString(); 71123b3eb3cSopenharmony_ci } else if (argsPtr->Contains(defaultPluralStr)) { 71223b3eb3cSopenharmony_ci valueStr = argsPtr->GetValue(defaultPluralStr)->GetString(); 71323b3eb3cSopenharmony_ci } else { 71423b3eb3cSopenharmony_ci return std::string(); 71523b3eb3cSopenharmony_ci } 71623b3eb3cSopenharmony_ci 71723b3eb3cSopenharmony_ci std::string pluralStr(PLURAL_COUNT_POS); 71823b3eb3cSopenharmony_ci auto pos = valueStr.find(pluralStr); 71923b3eb3cSopenharmony_ci if (pos != std::string::npos) { 72023b3eb3cSopenharmony_ci valueStr.replace(pos, pluralStr.length(), count); 72123b3eb3cSopenharmony_ci } 72223b3eb3cSopenharmony_ci return valueStr; 72323b3eb3cSopenharmony_ci} 72423b3eb3cSopenharmony_ci 72523b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Framework 72623b3eb3cSopenharmony_ci 72723b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_UTILS_H 728