1 /** 2 * Copyright (c) 2024-2025 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 PANDA_GUARD_UTIL_STRING_UTIL_H 17 #define PANDA_GUARD_UTIL_STRING_UTIL_H 18 19 #include <string> 20 #include <tuple> 21 #include <vector> 22 23 namespace panda::guard { 24 25 class StringUtil { 26 public: 27 /** 28 * Split str by delimiter 29 * @param str split str 30 * @param delimiter split delimiter 31 * @return split tokens, if split failed return empty vector 32 * e.g. Split("##a#", "#"); => ["a"] 33 */ 34 static std::vector<std::string> Split(const std::string &str, const std::string &delimiter); 35 36 /** 37 * Strictly split str by delimiter, which will gives empty part 38 * @param str split str 39 * @param delimiter split delimiter 40 * @return split tokens, if split failed return empty vector 41 * e.g. Split("##a#", "#"); => ["", "", "a", ""] 42 */ 43 static std::vector<std::string> StrictSplit(const std::string &str, const std::string &delimiter); 44 45 /** 46 * Split from right by delimiter 47 * @param str split str 48 * @param delimiter split delimiter 49 * @return (left, right) if split failed return tuple is ("", "") 50 * e.g. RSplitOnce("com.test.main.js", ".") => ["com.test.main", "js"] 51 */ 52 static std::tuple<std::string, std::string> RSplitOnce(const std::string &str, const std::string &delimiter); 53 54 /** 55 * Is Anonymous Function name 56 * 1. empty name 57 * 2. ^number or lowercase letters a-f 58 * @param functionName function name 59 */ 60 static bool IsAnonymousFunctionName(const std::string &functionName); 61 62 /** 63 * Is Anonymous NameSpace name 64 * 1. =ensnumber 65 * @param name nameSpace name 66 * e.g. =ens0, =ens1... 67 */ 68 static bool IsAnonymousNameSpaceName(const std::string &name); 69 70 /** 71 * Is str has specific prefix 72 * @param start calc from specific position, default is 0 73 */ 74 static bool IsPrefixMatched(const std::string &str, const std::string &prefix, size_t start = 0); 75 76 /** 77 * Is str has specific suffix 78 */ 79 static bool IsSuffixMatched(const std::string &str, const std::string &suffix); 80 81 static std::string UnicodeEscape(std::string_view string); 82 83 /** 84 * The slash/in JavaScript is the delimiter for regular expressions, indicating the beginning and 85 * end of the expression. After converting to C++, it needs to be removed. 86 * Usage scenario: str is a regular expression in JS format 87 */ 88 static void RemoveSlashFromBothEnds(std::string &str); 89 90 /** 91 * Check string is all of numbers 92 * Usage scenario: Attribute value is a number 93 */ 94 static bool IsNumber(const std::string &str); 95 96 /** 97 * Split str by '^' 98 * @param str split str 99 * @return (left, right) if not contain '^', return tuple is (str, "") 100 * e.g. 101 * SplitAnonymousName("foo^1) => ["foo", "^1"] 102 * SplitAnonymousName("foo) => ["foo", ""] 103 */ 104 static std::tuple<std::string, std::string> SplitAnonymousName(const std::string &str); 105 }; 106 107 } // namespace panda::guard 108 109 #endif // PANDA_GUARD_UTIL_STRING_UTIL_H 110