• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 #include "string_util.h"
17 
18 #include <regex>
19 
20 #include "utils/string_helpers.h"
21 
22 using panda::helpers::string::Format;
23 
24 namespace {
25 const std::regex ANONYMOUS_FUNCTION_NAME_REG = std::regex("\\^\\d+");
26 const std::regex ANONYMOUS_NAMESPACE_NAME_REG = std::regex("=ens\\d+");
27 
28 const char REGEX_DELIMITER = '/';
29 }  // namespace
30 
Split(const std::string & str,const std::string & delimiter)31 std::vector<std::string> panda::guard::StringUtil::Split(const std::string &str, const std::string &delimiter)
32 {
33     std::vector<std::string> tokens;
34     auto start = str.find_first_not_of(delimiter, 0);
35     auto pos = str.find_first_of(delimiter, 0);
36     while (start != std::string::npos) {
37         if (pos > start) {
38             tokens.push_back(str.substr(start, pos - start));
39         }
40         start = str.find_first_not_of(delimiter, pos);
41         pos = str.find_first_of(delimiter, start + 1);
42     }
43 
44     return tokens;
45 }
46 
StrictSplit(const std::string & str,const std::string & delimiter)47 std::vector<std::string> panda::guard::StringUtil::StrictSplit(const std::string &str, const std::string &delimiter)
48 {
49     std::vector<std::string> tokens;
50     size_t start = 0;
51     size_t end;
52     while (start <= str.size()) {
53         end = str.find_first_of(delimiter, start);
54         if (end == std::string::npos) {
55             end = str.size();
56         }
57 
58         if (start < end) {
59             tokens.push_back(str.substr(start, end - start));
60         } else {
61             tokens.emplace_back("");
62         }
63 
64         start = end + 1;
65     }
66     return tokens;
67 }
68 
RSplitOnce(const std::string & str,const std::string & delimiter)69 std::tuple<std::string, std::string> panda::guard::StringUtil::RSplitOnce(const std::string &str,
70                                                                           const std::string &delimiter)
71 {
72     auto pos = str.rfind(delimiter);
73     if (pos == std::string::npos) {
74         return std::make_tuple("", "");
75     }
76 
77     return std::make_tuple(str.substr(0, pos), str.substr(pos + delimiter.length()));
78 }
79 
IsAnonymousFunctionName(const std::string & functionName)80 bool panda::guard::StringUtil::IsAnonymousFunctionName(const std::string &functionName)
81 {
82     if (functionName.empty()) {
83         return true;
84     }
85 
86     return std::regex_search(functionName, ANONYMOUS_FUNCTION_NAME_REG);
87 }
88 
IsAnonymousNameSpaceName(const std::string & name)89 bool panda::guard::StringUtil::IsAnonymousNameSpaceName(const std::string &name)
90 {
91     if (name.empty()) {
92         return false;
93     }
94 
95     return std::regex_search(name, ANONYMOUS_NAMESPACE_NAME_REG);
96 }
97 
IsPrefixMatched(const std::string & str,const std::string & prefix,size_t start)98 bool panda::guard::StringUtil::IsPrefixMatched(const std::string &str, const std::string &prefix,
99                                                size_t start /* = 0 */)
100 {
101     if (start > str.size() || start + prefix.size() > str.size()) {
102         return false;
103     }
104     return str.compare(start, prefix.size(), prefix) == 0;
105 }
106 
IsSuffixMatched(const std::string & str,const std::string & suffix)107 bool panda::guard::StringUtil::IsSuffixMatched(const std::string &str, const std::string &suffix)
108 {
109     if (str.size() < suffix.size()) {
110         return false;
111     }
112     return StringUtil::IsPrefixMatched(str, suffix, str.size() - suffix.size());
113 }
114 
UnicodeEscape(std::string_view string)115 std::string panda::guard::StringUtil::UnicodeEscape(std::string_view string)
116 {
117     std::string out;
118     while (!string.empty()) {
119         auto iter =
120             std::find_if(string.begin(), string.end(), [](char ch) { return ch == '"' || ch == '\\' || ch < ' '; });
121         auto pos = iter - string.begin();
122         out += string.substr(0, pos);
123         if (iter == string.end()) {
124             break;
125         }
126 
127         out += '\\';
128         switch (*iter) {
129             case '"':
130             case '\\':
131                 out += *iter;
132                 break;
133             case '\b':
134                 out += 'b';
135                 break;
136             case '\f':
137                 out += 'f';
138                 break;
139             case '\n':
140                 out += 'n';
141                 break;
142             case '\r':
143                 out += 'r';
144                 break;
145             case '\t':
146                 out += 't';
147                 break;
148             default:
149                 out += Format("u%04X", *iter);  // NOLINT(cppcoreguidelines-pro-type-vararg)
150         }
151         string.remove_prefix(pos + 1);
152     }
153     return out;
154 }
155 
RemoveSlashFromBothEnds(std::string & str)156 void panda::guard::StringUtil::RemoveSlashFromBothEnds(std::string &str)
157 {
158     if (!str.empty() && str.front() == REGEX_DELIMITER && str.back() == REGEX_DELIMITER) {
159         str.erase(str.begin());
160         str.pop_back();
161     }
162 }
163 
IsNumber(const std::string & str)164 bool panda::guard::StringUtil::IsNumber(const std::string &str)
165 {
166     return std::all_of(str.begin(), str.end(), [](const char &ch) {
167         if ((ch >= '0') && (ch <= '9')) {
168             return true;
169         }
170         return false;
171     });
172 }
173