• 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 
65     static void Split(const std::string &src, std::unordered_set<std::string> &container, char delim);
66     static void Split(const std::string &src, std::queue<std::string> &container, char delim);
67 
68     static std::string Trim(const std::string &s);
69     static std::string Replace(const std::string &src, const std::string &target, const std::string &replacement);
70     static std::string Append(const std::string &src, const std::string &target, const std::string &spliter);
71     static std::string GetStrAfterLast(const std::string &src, const std::string &target, bool isReturnEmpty = false);
72     static std::string GetStrBeforeLast(const std::string &src, const std::string &target, bool isReturnEmpty = false);
HasCommandInjectionChar(const std::string & s)73     static bool HasCommandInjectionChar(const std::string &s)
74     {
75         return std::regex_search(s, kCommandInjectionRegex);
76     }
77 
EndsWith(const std::string & str,const std::string & end)78     static bool EndsWith(const std::string &str, const std::string &end)
79     {
80         if (end.size() > str.size()) {
81             return false;
82         }
83 
84         return std::equal(end.rbegin(), end.rend(), str.rbegin());
85     }
86 
StartsWith(const std::string & str,const std::string & start)87     static bool StartsWith(const std::string &str, const std::string &start)
88     {
89         if (start.size() > str.size()) {
90             return false;
91         }
92 
93         return std::equal(start.cbegin(), start.cend(), str.cbegin());
94     }
95 
96 private:
97     static std::regex kCommandInjectionRegex;
98 };
99 }  // namespace maple
100 #endif  // MAPLE_UTIL_INCLUDE_STRING_UTILS_H
101