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 namespace SysTuning {
17 namespace base {
GetDemangleSymbolIndex(const char * mangled)18 char *GetDemangleSymbolIndex(const char *mangled)
19 {
20 int status = 0;
21 auto demangle = abi::__cxa_demangle(mangled, nullptr, nullptr, &status);
22 if (status) { // status != 0 failed
23 return const_cast<char *>(mangled);
24 } else {
25 return demangle;
26 }
27 }
28
StartWith(const std::string & str,const std::string & res)29 bool StartWith(const std::string &str, const std::string &res)
30 {
31 if (res.size() > str.size()) {
32 return false;
33 }
34 return str.compare(0, res.length(), res) == 0;
35 }
36
EndWith(const std::string & str,const std::string & res)37 bool EndWith(const std::string &str, const std::string &res)
38 {
39 if (res.size() > str.size()) {
40 return false;
41 }
42 return str.compare(str.size() - res.size(), res.size(), res) == 0;
43 }
44
SplitStringToVec(const std::string & str,const std::string & pat)45 std::vector<std::string> SplitStringToVec(const std::string &str, const std::string &pat)
46 {
47 std::vector<std::string> result;
48 size_t curPos = 0;
49 size_t strSize = str.size();
50 size_t patSize = pat.size();
51 while (curPos < strSize) {
52 auto patPos = str.find(pat, curPos);
53 if (patPos == std::string::npos) {
54 break;
55 }
56 result.emplace_back(str.substr(curPos, patPos - curPos));
57 curPos = patPos + patSize;
58 }
59 if (curPos < strSize) {
60 result.emplace_back(str.substr(curPos));
61 }
62
63 return result;
64 }
TrimInvisibleCharacters(const std::string & str)65 std::string TrimInvisibleCharacters(const std::string &str)
66 {
67 size_t start = 0;
68 size_t end = str.length() - 1;
69 while (start <= end && !std::isgraph(str[start])) {
70 start++;
71 }
72 while (end >= start && !std::isgraph(str[end])) {
73 end--;
74 }
75 return str.substr(start, end - start + 1);
76 }
77
FormatString(const char * p)78 std::string FormatString(const char *p)
79 {
80 std::string str = "\"";
81 for (const char *c = p; *c != 0; c++) {
82 if (*c == '\\' || *c == '\"') {
83 str += "\\";
84 }
85 str += *c;
86 }
87 str += "\"";
88 return str;
89 }
90
Strip(const std::string & str)91 std::string Strip(const std::string &str)
92 {
93 std::string blanks = " \f\v\t\r\n";
94
95 auto first = str.find_first_not_of(blanks);
96 if (first == std::string::npos) {
97 return "";
98 }
99
100 auto last = str.find_last_not_of(blanks);
101 if (last == std::string::npos) {
102 return "";
103 }
104 return str.substr(first, last - first + 1);
105 }
106
RemoveNullTerminator(std::string & str)107 void RemoveNullTerminator(std::string &str)
108 {
109 size_t pos = str.rfind('\0');
110 while (pos != std::string::npos) {
111 str.erase(pos, 1);
112 pos = str.rfind('\0');
113 }
114 }
115 } // namespace base
116 } // namespace SysTuning
117