1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 #include "string_help.h"
16 #include <cctype>
17 #include <cstdint>
18 namespace SysTuning {
19 namespace base {
GetDemangleSymbolIndex(const char * mangled)20 char *GetDemangleSymbolIndex(const char *mangled)
21 {
22 int status = 0;
23 auto demangle = abi::__cxa_demangle(mangled, nullptr, nullptr, &status);
24 if (status) { // status != 0 failed
25 return const_cast<char *>(mangled);
26 } else {
27 return demangle;
28 }
29 }
30
StartWith(const std::string & str,const std::string & res)31 bool StartWith(const std::string &str, const std::string &res)
32 {
33 if (res.size() > str.size()) {
34 return false;
35 }
36 return str.compare(0, res.length(), res) == 0;
37 }
38
EndWith(const std::string & str,const std::string & res)39 bool EndWith(const std::string &str, const std::string &res)
40 {
41 if (res.size() > str.size()) {
42 return false;
43 }
44 return str.compare(str.size() - res.size(), res.size(), res) == 0;
45 }
46
SplitStringToVec(const std::string & str,const std::string & pat,uint32_t expectedCount)47 std::vector<std::string> SplitStringToVec(const std::string &str, const std::string &pat, uint32_t expectedCount)
48 {
49 std::vector<std::string> result;
50 size_t curPos = 0;
51 size_t strSize = str.size();
52 size_t patSize = pat.size();
53 uint32_t endFlag = 0;
54 while (curPos < strSize) {
55 auto patPos = str.find(pat, curPos);
56 if (patPos == std::string::npos || endFlag == expectedCount) {
57 break;
58 }
59 result.emplace_back(str.substr(curPos, patPos - curPos));
60 curPos = patPos + patSize;
61 endFlag++;
62 }
63 if (curPos < strSize) {
64 result.emplace_back(str.substr(curPos));
65 }
66
67 return result;
68 }
TrimInvisibleCharacters(const std::string & str)69 std::string TrimInvisibleCharacters(const std::string &str)
70 {
71 size_t start = 0;
72 size_t end = str.length() - 1;
73 while (start <= end && !std::isgraph(str[start])) {
74 start++;
75 }
76 while (end >= start && !std::isgraph(str[end])) {
77 end--;
78 }
79 return str.substr(start, end - start + 1);
80 }
81
FormatString(const char * p)82 std::string FormatString(const char *p)
83 {
84 std::string str = "\"";
85 for (const char *c = p; *c != 0; c++) {
86 if (*c == '\\' || *c == '\"') {
87 str += "\\";
88 }
89 str += *c;
90 }
91 str += "\"";
92 return str;
93 }
94
Strip(const std::string & str)95 std::string Strip(const std::string &str)
96 {
97 std::string blanks = " \f\v\t\r\n";
98
99 auto first = str.find_first_not_of(blanks);
100 if (first == std::string::npos) {
101 return "";
102 }
103
104 auto last = str.find_last_not_of(blanks);
105 if (last == std::string::npos) {
106 return "";
107 }
108 return str.substr(first, last - first + 1);
109 }
110
StrTrim(const std::string & input)111 std::string StrTrim(const std::string &input)
112 {
113 std::string str = input;
114 if (str.empty()) {
115 return str;
116 }
117 str.erase(0, str.find_first_not_of(' '));
118 str.erase(str.find_last_not_of(' ') + 1);
119 return str;
120 }
121
122 // in-place版本, 直接修改str
StrTrim(std::string & input)123 void StrTrim(std::string &input)
124 {
125 if (input.empty()) {
126 return;
127 }
128 input.erase(0, input.find_first_not_of(' '));
129 input.erase(input.find_last_not_of(' ') + 1);
130 }
131
RemoveNullTerminator(std::string & str)132 void RemoveNullTerminator(std::string &str)
133 {
134 size_t pos = str.rfind('\0');
135 while (pos != std::string::npos) {
136 str.erase(pos, 1);
137 pos = str.rfind('\0');
138 }
139 }
140
StrHash(const std::string & str,uint32_t maxValue)141 uint32_t StrHash(const std::string &str, uint32_t maxValue)
142 {
143 if (maxValue == 0) {
144 return 0;
145 }
146 static const uint32_t COLOR_A = 9876023;
147 static const uint32_t COLOR_B = 0xffffffff;
148 uint32_t hash = 0x11c9dc5;
149 for (const auto &chr : str) {
150 if (std::isdigit(chr)) {
151 continue;
152 }
153 hash ^= chr;
154 hash = (hash * COLOR_A) & COLOR_B;
155 }
156 return hash % maxValue;
157 }
158 } // namespace base
159 } // namespace SysTuning
160