• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #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 }
65 
FormatString(const char * p)66 std::string FormatString(const char* p)
67 {
68     std::string str = "\"";
69     for (const char* c = p; *c != 0; c++) {
70         if (*c == '\\' || *c == '\"') {
71             str += "\\";
72         }
73         str += *c;
74     }
75     str += "\"";
76     return str;
77 }
78 
Strip(const std::string & str)79 std::string Strip(const std::string& str)
80 {
81     std::string blanks = " \f\v\t\r\n";
82 
83     auto first = str.find_first_not_of(blanks);
84     if (first == std::string::npos) {
85         return "";
86     }
87 
88     auto last = str.find_last_not_of(blanks);
89     if (last == std::string::npos) {
90         return "";
91     }
92     return str.substr(first, last - first + 1);
93 }
94 } // namespace base
95 } // namespace SysTuning
96