• 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 #include "string_utils.h"
17 #include <cstddef>
18 #include <iostream>
19 
20 namespace maple {
Trim(const std::string & src)21 std::string StringUtils::Trim(const std::string &src)
22 {
23     // remove space
24     return std::regex_replace(src, std::regex("\\s+"), "");
25 }
26 
Replace(const std::string & src,const std::string & target,const std::string & replacement)27 std::string StringUtils::Replace(const std::string &src, const std::string &target, const std::string &replacement)
28 {
29     std::string::size_type replaceLen = replacement.size();
30     std::string::size_type targetLen = target.size();
31     std::string temp = src;
32     std::string::size_type index = 0;
33     index = temp.find(target, index);
34     while (index != std::string::npos) {
35         temp.replace(index, targetLen, replacement);
36         index += replaceLen;
37         index = temp.find(target, index);
38     }
39     return temp;
40 }
41 
Append(const std::string & src,const std::string & target,const std::string & spliter)42 std::string StringUtils::Append(const std::string &src, const std::string &target, const std::string &spliter)
43 {
44     return src + spliter + target;
45 }
46 
GetStrAfterLast(const std::string & src,const std::string & target,bool isReturnEmpty)47 std::string StringUtils::GetStrAfterLast(const std::string &src, const std::string &target, bool isReturnEmpty)
48 {
49     size_t pos = src.find_last_of(target);
50     if (pos == std::string::npos) {
51         return isReturnEmpty ? "" : src;
52     }
53     return src.substr(pos + 1);
54 }
55 
GetStrBeforeLast(const std::string & src,const std::string & target,bool isReturnEmpty)56 std::string StringUtils::GetStrBeforeLast(const std::string &src, const std::string &target, bool isReturnEmpty)
57 {
58     size_t pos = src.find_last_of(target);
59     if (pos == std::string::npos) {
60         return isReturnEmpty ? "" : src;
61     }
62     return src.substr(0, pos);
63 }
64 
Split(const std::string & src,std::unordered_set<std::string> & container,char delim)65 void StringUtils::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 void StringUtils::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 
89 std::regex StringUtils::kCommandInjectionRegex("[;\\|\\&\\$\\>\\<`]");
90 }  // namespace maple
91