• 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 
16 #ifndef UTILITY_STR_UTIL_H
17 #define UTILITY_STR_UTIL_H
18 
19 #include <sstream>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 #include <future>
24 #include <fstream>
25 #include <iostream>
26 #include <regex>
27 #include <codecvt>
28 #include <list>
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace StringUtil {
32 /**
33  * The ReplaceStr function will replace src with dst int base.
34  */
35 std::string ReplaceStr(const std::string& str, const std::string& src, const std::string& dst);
36 
37 /**
38  * The TrimStr function will trim str by cTrim front and end.
39  */
40 std::string TrimStr(const std::string& str, const char cTrim = ' ');
41 
42 /**
43  * The SplitStr function will split str by strSep.
44  */
45 void SplitStr(const std::string& str, const std::string& sep, std::vector<std::string>& strs,
46               bool canEmpty = false, bool needTrim = true);
47 
48 /**
49  * The ToString function convert int and double and so on to str.
50  */
51 template<class T>
ToString(T iValue)52 inline std::string ToString(T iValue)
53 {
54     return std::to_string(iValue);
55 }
56 
57 /**
58  * convert string to a specific type by sstream
59  */
60 template<typename T>
ConvertStringTo(const std::string & in,T & out)61 static void ConvertStringTo(const std::string &in, T &out)
62 {
63     std::stringstream ss;
64     ss << in;
65     ss >> out;
66 }
67 
68 template<class T>
IsConvertable(const std::string & inValue)69 bool IsConvertable(const std::string &inValue)
70 {
71     std::stringstream stream;
72     stream << inValue;
73     int64_t outValue;
74     stream >> outValue;
75     T value = (T)outValue;
76     stream.clear();
77     stream.str("");
78     stream << (value + 0);
79     std::string tempValue = stream.str();
80     if (tempValue != inValue) {
81         return false;
82     }
83     return true;
84 }
85 
86 bool IsValidFloatNum(const std::string &value);
87 
88 /**
89  * The StrToInt function convert str to int.
90  */
91 bool StrToInt(const std::string& str, int& value);
92 int StrToInt(const std::string& str);
93 
94 /**
95  * Append the strings in list with specific delimiter
96  */
97 std::string ConvertVectorToStr(const std::vector<std::string> &listStr, const std::string &split);
98 
99 /**
100  * The DexToHexString function convert dex to hex string.
101  */
102 std::string DexToHexString(int value, bool upper = true);
103 
104 /**
105  * Get key-value pair separated by colon
106  */
107 using KeyValuePair = std::pair<std::string, std::pair<std::string, char>>;
108 KeyValuePair GetKeyValueByString(size_t &start, const std::string &inputString);
109 
110 
111 template<typename T>
ConvertToUTF8(const std::basic_string<T,std::char_traits<T>,std::allocator<T>> & source)112 std::string ConvertToUTF8(const std::basic_string<T, std::char_traits<T>, std::allocator<T>> &source)
113 {
114     std::wstring_convert<std::codecvt_utf8_utf16<T>, T> converter;
115     std::string result = converter.to_bytes(source);
116     return result;
117 }
118 
119 std::list<std::string> SplitStr(const std::string& str, char delimiter = ' ');
120 /**
121  * Get substring in input string between begin and end
122  */
123 std::string GetMidSubstr(const std::string& input, const std::string& begin, const std::string& end);
124 /**
125  * Gets the substring to the left of the input string
126  */
127 std::string GetLeftSubstr(const std::string& input, const std::string& split);
128 /**
129  * Gets the substring to the right of the input string
130  */
131 std::string GetRightSubstr(const std::string& input, const std::string& split);
132 /**
133  * Gets the substring to the left of the input string when finding first split from the left
134  */
135 std::string GetRleftSubstr(const std::string& input, const std::string& split);
136 /**
137  * Gets the substring to the right of the input string when finding first split from the right
138  */
139 std::string GetRrightSubstr(const std::string& input, const std::string& split);
140 /**
141  * Gets the substring when erasing all same string in the input string
142  */
143 std::string EraseString(const std::string& input, const std::string& toerase);
144 
145 std::string VectorToString(const std::vector<std::string>& src, bool reverse, const std::string& tag = "\r\n");
146 
147 uint64_t StringToUl(const std::string& flag, int base = 10); // 10 : default base
148 double StringToDouble(const std::string& input);
149 
150 std::string FindMatchSubString(const std::string& target, const std::string& begin, int offset,
151     const std::string& end);
152 
153 std::string EscapeJsonStringValue(const std::string &value);
154 std::string UnescapeJsonStringValue(const std::string &value);
155 } // namespace StringUtil
156 } // namespace HiviewDFX
157 } // namespace OHOS
158 #endif // UTILITY_STR_UTIL_H