• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef MAPLE_UTIL_INCLUDE_STRING_UTILS_H
17 #define MAPLE_UTIL_INCLUDE_STRING_UTILS_H
18 #include <map>
19 #include <string>
20 #include <vector>
21 #include <sstream>
22 #include <unordered_set>
23 #include <regex>
24 #include <queue>
25 
26 namespace maple {
27 class StringUtils {
28 public:
29     template <class Container>
Split(const std::string & src,Container & container,char delim)30     static void Split(const std::string &src, Container &container, char delim)
31     {
32         if (Trim(src).empty()) {
33             return;
34         }
35 
36         std::stringstream strStream(src + delim);
37         std::string item;
38         while (std::getline(strStream, item, delim)) {
39             container.emplace_back(item);
40         }
41     }
42 
43     template <typename Container>
SplitSV(const std::string_view & src,Container & container,char delim)44     static void SplitSV(const std::string_view &src, Container &container, char delim)
45     {
46         size_t startInd = 0;
47         while (startInd < src.size()) {
48             if (src[startInd] == delim) {
49                 ++startInd;
50                 continue;
51             }
52 
53             size_t endInd = src.find_first_of(delim, startInd);
54             if (startInd != endInd) {
55                 container.emplace_back(src.substr(startInd, endInd - startInd));
56             }
57 
58             if (endInd == std::string_view::npos) {
59                 break;
60             }
61             startInd = endInd + 1;
62         }
63     }
64 
Split(const std::string & src,std::unordered_set<std::string> & container,char delim)65     static void Split(const std::string &src, std::unordered_set<std::string> &container, char delim)
66     {
67         if (Trim(src).empty()) {
68             return;
69         }
70         std::stringstream strStream(src + delim);
71         std::string item;
72         while (std::getline(strStream, item, delim)) {
73             container.insert(item);
74         }
75     }
76 
Split(const std::string & src,std::queue<std::string> & container,char delim)77     static void Split(const std::string &src, std::queue<std::string> &container, char delim)
78     {
79         if (Trim(src).empty()) {
80             return;
81         }
82         std::stringstream strStream(src + delim);
83         std::string item;
84         while (std::getline(strStream, item, delim)) {
85             container.push(item);
86         }
87     }
88 
Trim(const std::string & s)89     static std::string Trim(const std::string &s)
90     {
91         // remove space
92         return std::regex_replace(s, std::regex("\\s+"), "");
93     }
94 
HasCommandInjectionChar(const std::string & s)95     static bool HasCommandInjectionChar(const std::string &s)
96     {
97         return std::regex_search(s, kCommandInjectionRegex);
98     }
99 
EndsWith(const std::string & str,const std::string & end)100     static bool EndsWith(const std::string &str, const std::string &end)
101     {
102         if (end.size() > str.size()) {
103             return false;
104         }
105 
106         return std::equal(end.rbegin(), end.rend(), str.rbegin());
107     }
108 
StartsWith(const std::string & str,const std::string & start)109     static bool StartsWith(const std::string &str, const std::string &start)
110     {
111         if (start.size() > str.size()) {
112             return false;
113         }
114 
115         return std::equal(start.cbegin(), start.cend(), str.cbegin());
116     }
117 
118 private:
119     static std::regex kCommandInjectionRegex;
120 };
121 }  // namespace maple
122 #endif  // MAPLE_UTIL_INCLUDE_STRING_UTILS_H
123