• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)47 std::vector<std::string> SplitStringToVec(const std::string &str, const std::string &pat)
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     while (curPos < strSize) {
54         auto patPos = str.find(pat, curPos);
55         if (patPos == std::string::npos) {
56             break;
57         }
58         result.emplace_back(str.substr(curPos, patPos - curPos));
59         curPos = patPos + patSize;
60     }
61     if (curPos < strSize) {
62         result.emplace_back(str.substr(curPos));
63     }
64 
65     return result;
66 }
TrimInvisibleCharacters(const std::string & str)67 std::string TrimInvisibleCharacters(const std::string &str)
68 {
69     size_t start = 0;
70     size_t end = str.length() - 1;
71     while (start <= end && !std::isgraph(str[start])) {
72         start++;
73     }
74     while (end >= start && !std::isgraph(str[end])) {
75         end--;
76     }
77     return str.substr(start, end - start + 1);
78 }
79 
FormatString(const char * p)80 std::string FormatString(const char *p)
81 {
82     std::string str = "\"";
83     for (const char *c = p; *c != 0; c++) {
84         if (*c == '\\' || *c == '\"') {
85             str += "\\";
86         }
87         str += *c;
88     }
89     str += "\"";
90     return str;
91 }
92 
Strip(const std::string & str)93 std::string Strip(const std::string &str)
94 {
95     std::string blanks = " \f\v\t\r\n";
96 
97     auto first = str.find_first_not_of(blanks);
98     if (first == std::string::npos) {
99         return "";
100     }
101 
102     auto last = str.find_last_not_of(blanks);
103     if (last == std::string::npos) {
104         return "";
105     }
106     return str.substr(first, last - first + 1);
107 }
108 
StrTrim(const std::string & input)109 std::string StrTrim(const std::string &input)
110 {
111     std::string str = input;
112     if (str.empty()) {
113         return str;
114     }
115     str.erase(0, str.find_first_not_of(' '));
116     str.erase(str.find_last_not_of(' ') + 1);
117     return str;
118 }
119 
120 // in-place版本, 直接修改str
StrTrim(std::string & input)121 void StrTrim(std::string &input)
122 {
123     if (input.empty()) {
124         return;
125     }
126     input.erase(0, input.find_first_not_of(' '));
127     input.erase(input.find_last_not_of(' ') + 1);
128 }
129 
RemoveNullTerminator(std::string & str)130 void RemoveNullTerminator(std::string &str)
131 {
132     size_t pos = str.rfind('\0');
133     while (pos != std::string::npos) {
134         str.erase(pos, 1);
135         pos = str.rfind('\0');
136     }
137 }
138 
StrHash(const std::string & str,uint32_t maxValue)139 uint32_t StrHash(const std::string &str, uint32_t maxValue)
140 {
141     if (maxValue == 0) {
142         return 0;
143     }
144     static const uint32_t COLOR_A = 9876023;
145     static const uint32_t COLOR_B = 0xffffffff;
146     uint32_t hash = 0x11c9dc5;
147     for (const auto &chr : str) {
148         if (std::isdigit(chr)) {
149             continue;
150         }
151         hash ^= chr;
152         hash = (hash * COLOR_A) & COLOR_B;
153     }
154     return hash % maxValue;
155 }
156 } // namespace base
157 } // namespace SysTuning
158