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