• 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 #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         UTILS_LOGE("call StrToInt func false,  input str is: %{public}s!", str.c_str());
115         return false;
116     }
117 
118     value = static_cast<int>(result);
119     return true;
120 }
121 
IsNumericStr(const string & str)122 bool IsNumericStr(const string& str)
123 {
124     if (str.empty()) {
125         UTILS_LOGE("call IsNumericStr func,  input str is empty!");
126         return false;
127     }
128 
129     for (const auto& c : str) {
130         if (!isdigit(c)) {
131             UTILS_LOGE("call IsNumericStr func false,  input str is: %{public}s!", str.c_str());
132             return false;
133         }
134     }
135 
136     return true;
137 }
138 
IsAlphaStr(const string & str)139 bool IsAlphaStr(const string& str)
140 {
141     if (str.empty()) {
142         UTILS_LOGE("call IsAlphaStr func, input str is empty!");
143         return false;
144     }
145 
146     for (const auto& c : str) {
147         if (!isalpha(c)) {
148             UTILS_LOGE("call IsAlphaStr func false,  input str is: %{public}s!", str.c_str());
149             return false;
150         }
151     }
152 
153     return true;
154 }
155 
IsUpperStr(const string & str)156 bool IsUpperStr(const string& str)
157 {
158     if (str.empty()) {
159         UTILS_LOGE("call IsUpperStr func, input str is empty!");
160         return false;
161     }
162 
163     for (const auto& c : str) {
164         if (!isupper(c)) {
165             UTILS_LOGE("call IsUpperStr func false,  input str is: %{public}s!", str.c_str());
166             return false;
167         }
168     }
169 
170     return true;
171 }
172 
IsLowerStr(const string & str)173 bool IsLowerStr(const string& str)
174 {
175     if (str.empty()) {
176         UTILS_LOGE("call IsLowerStr func, input str is empty!");
177         return false;
178     }
179 
180     for (const auto& c : str) {
181         if (!islower(c)) {
182             UTILS_LOGE("call IsLowerStr func false,  input str is: %{public}s!", str.c_str());
183             return false;
184         }
185     }
186 
187     return true;
188 }
189 
IsSubStr(const string & str,const string & sub)190 bool IsSubStr(const string& str, const string& sub)
191 {
192     if (sub.empty() || str.empty()) {
193         return false;
194     }
195 
196     return str.find(sub) != string::npos;
197 }
198 
GetFirstSubStrBetween(const string & str,const string & left,const string & right,string & sub)199 string::size_type GetFirstSubStrBetween(const string& str, const string& left,
200     const string& right, string& sub)
201 {
202     string::size_type leftPos = str.find(left);
203     if (leftPos == string::npos) {
204         return string::npos;
205     }
206 
207     string::size_type rightPos = str.find(right, leftPos + left.length());
208     if (rightPos == string::npos) {
209         return string::npos;
210     }
211 
212     sub = str.substr((leftPos + left.length()), (rightPos - (leftPos + left.length())));
213     return rightPos;
214 }
215 
GetSubStrBetween(const string & str,const string & left,const string & right,vector<string> & sub)216 void GetSubStrBetween(const string& str, const string& left, const string& right, vector<string>& sub)
217 {
218     string subString;
219     string strTmp = str;
220     string::size_type pos = GetFirstSubStrBetween(strTmp, left, right, subString);
221     while (pos != string::npos) {
222         sub.push_back(subString);
223         strTmp = strTmp.substr(pos);
224         pos = GetFirstSubStrBetween(strTmp, left, right, subString);
225     }
226 }
227 
IsSameTextStr(const string & first,const string & second)228 bool IsSameTextStr(const string& first, const string& second)
229 {
230     return UpperStr(first) == UpperStr(second);
231 }
232 
233 /*
234  * utf8 rule:
235  * 0000 ~ 007F        : 0xxxxxxx
236  * 0080 ~ 07FF        : 110xxxxx 10xxxxxx
237  * 0800 ~ FFFF        : 1110xxxx 10xxxxxx 10xxxxxx
238  * 10000 ~ 1FFFFF     : 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
239  */
IsAsciiString(const string & str)240 bool IsAsciiString(const string& str)
241 {
242     size_t strLen = str.length();
243     for (size_t i = 0; i < strLen; ++i) {
244         if ((str[i] & 0x80) != 0) {
245             return false;
246         }
247     }
248 
249     return true;
250 }
251 
Str8ToStr16(const string & str)252 u16string Str8ToStr16(const string& str)
253 {
254     u16string str16Value;
255     if (!String8ToString16(str, str16Value)) {
256         return u16string();
257     }
258 
259     return str16Value;
260 }
261 
Str16ToStr8(const u16string & str16)262 string Str16ToStr8(const u16string& str16)
263 {
264     string str8Value;
265     if (!String16ToString8(str16, str8Value)) {
266         return string();
267     }
268 
269     return str8Value;
270 }
271 } // namespace OHOS
272