/** * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PANDA_GUARD_UTIL_STRING_UTIL_H #define PANDA_GUARD_UTIL_STRING_UTIL_H #include #include #include namespace panda::guard { class StringUtil { public: /** * Split str by delimiter * @param str split str * @param delimiter split delimiter * @return split tokens, if split failed return empty vector * e.g. Split("##a#", "#"); => ["a"] */ static std::vector Split(const std::string &str, const std::string &delimiter); /** * Strictly split str by delimiter, which will gives empty part * @param str split str * @param delimiter split delimiter * @return split tokens, if split failed return empty vector * e.g. Split("##a#", "#"); => ["", "", "a", ""] */ static std::vector StrictSplit(const std::string &str, const std::string &delimiter); /** * Split from right by delimiter * @param str split str * @param delimiter split delimiter * @return (left, right) if split failed return tuple is ("", "") * e.g. RSplitOnce("com.test.main.js", ".") => ["com.test.main", "js"] */ static std::tuple RSplitOnce(const std::string &str, const std::string &delimiter); /** * Is Anonymous Function name * 1. empty name * 2. ^number * @param functionName function name */ static bool IsAnonymousFunctionName(const std::string &functionName); /** * Is Anonymous NameSpace name * 1. =ensnumber * @param name nameSpace name * e.g. =ens0, =ens1... */ static bool IsAnonymousNameSpaceName(const std::string &name); /** * Is str has specific prefix * @param start calc from specific position, default is 0 */ static bool IsPrefixMatched(const std::string &str, const std::string &prefix, size_t start = 0); /** * Is str has specific suffix */ static bool IsSuffixMatched(const std::string &str, const std::string &suffix); static std::string UnicodeEscape(std::string_view string); /** * The slash/in JavaScript is the delimiter for regular expressions, indicating the beginning and * end of the expression. After converting to C++, it needs to be removed. * Usage scenario: str is a regular expression in JS format */ static void RemoveSlashFromBothEnds(std::string &str); /** * Check string is all of numbers * Usage scenario: Attribute value is a number */ static bool IsNumber(const std::string &str); }; } // namespace panda::guard #endif // PANDA_GUARD_UTIL_STRING_UTIL_H