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 #include "string_ex.h"
17 #include "unicode_ex.h"
18 #include "utils_log.h"
19 #include "securec.h"
20 #include <iostream>
21 #include <iomanip>
22 #include <sstream>
23 using namespace std;
24
25 namespace OHOS {
26
UpperStr(const string & str)27 string UpperStr(const string& str)
28 {
29 string upperString = str;
30 transform(upperString.begin(), upperString.end(), upperString.begin(), ::toupper);
31 return upperString;
32 }
33
LowerStr(const string & str)34 string LowerStr(const string& str)
35 {
36 string lowerString = str;
37 transform(lowerString.begin(), lowerString.end(), lowerString.begin(), ::tolower);
38 return lowerString;
39 }
40
ReplaceStr(const string & str,const string & src,const string & dst)41 string ReplaceStr(const string& str, const string& src, const string& dst)
42 {
43 if (src.empty()) {
44 return str;
45 }
46
47 string::size_type pos = 0;
48 string strTmp = str;
49 while ((pos = strTmp.find(src, pos)) != string::npos) {
50 strTmp.replace(pos, src.length(), dst);
51 pos += dst.length();
52 }
53
54 return strTmp;
55 }
56
TrimStr(const string & str,const char cTrim)57 string TrimStr(const string& str, const char cTrim /*= ' '*/)
58 {
59 string strTmp = str;
60 strTmp.erase(0, strTmp.find_first_not_of(cTrim));
61 strTmp.erase(strTmp.find_last_not_of(cTrim) + sizeof(char));
62 return strTmp;
63 }
64
DexToHexString(int value,bool upper)65 string DexToHexString(int value, bool upper /*= true*/)
66 {
67 stringstream ioss;
68 string hexString;
69 if (upper) {
70 ioss << setiosflags(ios::uppercase) << hex << value;
71 } else {
72 ioss << hex << value;
73 }
74
75 ioss >> hexString;
76 return hexString;
77 }
78
SplitStr(const string & str,const string & sep,vector<string> & strs,bool canEmpty,bool needTrim)79 void SplitStr(const string& str, const string& sep, vector<string>& strs, bool canEmpty, bool needTrim)
80 {
81 strs.clear();
82 string strTmp = needTrim ? TrimStr(str) : str;
83 string strPart;
84 while (true) {
85 string::size_type pos = strTmp.find(sep);
86 if (string::npos == pos || sep.empty()) {
87 strPart = needTrim ? TrimStr(strTmp) : strTmp;
88 if (!strPart.empty() || canEmpty) {
89 strs.push_back(strPart);
90 }
91 break;
92 } else {
93 strPart = needTrim ? TrimStr(strTmp.substr(0, pos)) : strTmp.substr(0, pos);
94 if (!strPart.empty() || canEmpty) {
95 strs.push_back(strPart);
96 }
97 strTmp = strTmp.substr(sep.size() + pos, strTmp.size() - sep.size() - pos);
98 }
99 }
100 }
101
StrToInt(const string & str,int & value)102 bool StrToInt(const string& str, int& value)
103 {
104 if (str.empty() || (!isdigit(str.front()) && (str.front() != '-'))) {
105 return false;
106 }
107
108 char* end = nullptr;
109 errno = 0;
110 auto addr = str.c_str();
111 auto result = strtol(addr, &end, 10); /* 10 means decimal */
112 if ((end == addr) || (end[0] != '\0') || (errno == ERANGE) ||
113 (result > INT_MAX) || (result < INT_MIN)) {
114 return false;
115 }
116
117 value = static_cast<int>(result);
118 return true;
119 }
120
IsNumericStr(const string & str)121 bool IsNumericStr(const string& str)
122 {
123 if (str.empty()) {
124 return false;
125 }
126
127 for (const auto& c : str) {
128 if (!isdigit(c)) {
129 return false;
130 }
131 }
132
133 return true;
134 }
135
IsAlphaStr(const string & str)136 bool IsAlphaStr(const string& str)
137 {
138 if (str.empty()) {
139 return false;
140 }
141
142 for (const auto& c : str) {
143 if (!isalpha(c)) {
144 return false;
145 }
146 }
147
148 return true;
149 }
150
IsUpperStr(const string & str)151 bool IsUpperStr(const string& str)
152 {
153 if (str.empty()) {
154 return false;
155 }
156
157 for (const auto& c : str) {
158 if (!isupper(c)) {
159 return false;
160 }
161 }
162
163 return true;
164 }
165
IsLowerStr(const string & str)166 bool IsLowerStr(const string& str)
167 {
168 if (str.empty()) {
169 return false;
170 }
171
172 for (const auto& c : str) {
173 if (!islower(c)) {
174 return false;
175 }
176 }
177
178 return true;
179 }
180
IsSubStr(const string & str,const string & sub)181 bool IsSubStr(const string& str, const string& sub)
182 {
183 if (sub.empty() || str.empty()) {
184 return false;
185 }
186
187 return str.find(sub) != string::npos;
188 }
189
GetFirstSubStrBetween(const string & str,const string & left,const string & right,string & sub)190 string::size_type GetFirstSubStrBetween(const string& str, const string& left,
191 const string& right, string& sub)
192 {
193 string::size_type leftPos = str.find(left);
194 if (leftPos == string::npos) {
195 return string::npos;
196 }
197
198 string::size_type rightPos = str.find(right, leftPos + left.length());
199 if (rightPos == string::npos) {
200 return string::npos;
201 }
202
203 sub = str.substr((leftPos + left.length()), (rightPos - (leftPos + left.length())));
204 return rightPos;
205 }
206
GetSubStrBetween(const string & str,const string & left,const string & right,vector<string> & sub)207 void GetSubStrBetween(const string& str, const string& left, const string& right, vector<string>& sub)
208 {
209 string subString;
210 string strTmp = str;
211 string::size_type pos = GetFirstSubStrBetween(strTmp, left, right, subString);
212 while (pos != string::npos) {
213 sub.push_back(subString);
214 strTmp = strTmp.substr(pos);
215 pos = GetFirstSubStrBetween(strTmp, left, right, subString);
216 }
217 }
218
IsSameTextStr(const string & first,const string & second)219 bool IsSameTextStr(const string& first, const string& second)
220 {
221 return UpperStr(first) == UpperStr(second);
222 }
223
224 /*
225 * utf8 rule:
226 * 0000 ~ 007F : 0xxxxxxx
227 * 0080 ~ 07FF : 110xxxxx 10xxxxxx
228 * 0800 ~ FFFF : 1110xxxx 10xxxxxx 10xxxxxx
229 * 10000 ~ 1FFFFF : 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
230 */
IsAsciiString(const string & str)231 bool IsAsciiString(const string& str)
232 {
233 size_t strLen = str.length();
234 for (size_t i = 0; i < strLen; ++i) {
235 if ((str[i] & 0x80) != 0) {
236 return false;
237 }
238 }
239
240 return true;
241 }
242
Str8ToStr16(const string & str)243 u16string Str8ToStr16(const string& str)
244 {
245 u16string str16Value;
246 if (!String8ToString16(str, str16Value)) {
247 return u16string();
248 }
249
250 return str16Value;
251 }
252
Str16ToStr8(const u16string & str16)253 string Str16ToStr8(const u16string& str16)
254 {
255 string str8Value;
256 if (!String16ToString8(str16, str8Value)) {
257 return string();
258 }
259
260 return str8Value;
261 }
262 } // namespace OHOS
263