1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#ifndef STRING_UTILS_H 17#define STRING_UTILS_H 18 19#include <algorithm> 20#include <functional> 21#include <string> 22#include <vector> 23 24#define GET_ENUM_VALUE_STRING(item) StringUtils::GetEnumValueString(#item) 25 26namespace OHOS { 27namespace UpdateEngine { 28class StringUtils { 29public: 30 // trim from start (in place) 31 static inline void LTrim(std::string &inputStr) 32 { 33 inputStr.erase(inputStr.begin(), 34 std::find_if(inputStr.begin(), inputStr.end(), [](unsigned char ch) { return !std::isspace(ch); })); 35 } 36 37 // trim from end (in place) 38 static inline void RTrim(std::string &inputStr) 39 { 40 inputStr.erase( 41 std::find_if(inputStr.rbegin(), inputStr.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), 42 inputStr.end()); 43 } 44 45 // trim from both ends (in place) 46 static inline void Trim(std::string &inputStr) 47 { 48 LTrim(inputStr); 49 RTrim(inputStr); 50 } 51 52 static inline std::vector<std::string> Split(const std::string &str, char delimiter) 53 { 54 std::vector<std::string> tokens; 55 size_t start; 56 size_t end = 0; 57 while ((start = str.find_first_not_of(delimiter, end)) != std::string::npos) { 58 end = str.find(delimiter, start); 59 tokens.push_back(str.substr(start, end - start)); 60 } 61 return tokens; 62 } 63 64 static inline std::string GetBoolStr(bool isTrue) 65 { 66 return isTrue ? "true" : "false"; 67 } 68 69 static std::string SafeSubString(const std::string &sourceStr, int beginIndex, int endIndex, 70 std::string defaultStr) 71 { 72 if (sourceStr.empty()) { 73 return defaultStr; 74 } 75 if (beginIndex < 0 || static_cast<size_t>(endIndex) > sourceStr.size() || beginIndex > endIndex) { 76 return defaultStr; 77 } 78 return sourceStr.substr(beginIndex, endIndex); 79 } 80 81 static std::string GetEnumValueString(const std::string &enumItem) 82 { 83 std::string enumSplit = "::"; 84 size_t split = enumItem.find(enumSplit); 85 if (split == std::string::npos) { 86 return ""; 87 } 88 return enumItem.substr(split + enumSplit.length(), enumItem.length()); 89 } 90 91 static std::string GetLastSplitString(const std::string &stringName, const std::string &splitStr) 92 { 93 std::size_t found = stringName.find_last_of(splitStr); 94 std::string fileString = ""; 95 if (found != std::string::npos) { 96 fileString = stringName.substr(found + 1, stringName.size()); 97 } 98 return fileString; 99 } 100 101 static void ReplaceStringAll(std::string &srcString, const std::string &subString, const std::string &newString) 102 { 103 std::string resultString = srcString; 104 for (std::string::size_type pos = 0; pos != std::string::npos; pos += newString.length()) { 105 pos = resultString.find(subString, pos); 106 if (pos != std::string::npos) { 107 resultString.replace(pos, subString.length(), newString); 108 } else { 109 break; 110 } 111 } 112 srcString = resultString; 113 } 114 115 static void StringRemove(std::string &sourceString, const std::string &startString, 116 const std::string &endString) 117 { 118 if (sourceString.empty()) { 119 return; 120 } 121 std::string::size_type indexStart = sourceString.find(startString); 122 if (indexStart != std::string::npos) { 123 std::string::size_type indexEnd = sourceString.find(endString, indexStart); 124 if (indexEnd != std::string::npos) { 125 std::string tmpString = sourceString.substr(0, indexStart) + 126 sourceString.substr(indexEnd + endString.length(), sourceString.length()); 127 sourceString = tmpString; 128 } 129 } 130 return; 131 } 132}; 133} // namespace UpdateEngine 134} // namespace OHOS 135#endif // STRING_UTILS_H