• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 using namespace std;
18 
19 namespace OHOS {
IsNumericStr(const string & str)20 bool IsNumericStr(const string& str)
21 {
22     if (str.empty()) {
23         return false;
24     }
25 
26     for (const auto& c : str) {
27         if (!isdigit(c)) {
28             return false;
29         }
30     }
31     return true;
32 }
33 
TrimStr(const string & str,const char cTrim)34 string TrimStr(const string& str, const char cTrim)
35 {
36     string strTmp = str;
37     strTmp.erase(0, strTmp.find_first_not_of(cTrim));
38     strTmp.erase(strTmp.find_last_not_of(cTrim) + sizeof(char));
39     return strTmp;
40 }
41 
UpperStr(const string & str)42 string UpperStr(const string& str)
43 {
44     string upperString = str;
45     transform(upperString.begin(), upperString.end(), upperString.begin(), ::toupper);
46     return upperString;
47 }
48 
IsSameTextStr(const string & first,const string & second)49 bool IsSameTextStr(const string& first, const string& second)
50 {
51     return UpperStr(first) == UpperStr(second);
52 }
53 
SplitStr(const string & str,const string & sep,vector<string> & strs,bool canEmpty,bool needTrim)54 void SplitStr(const string& str, const string& sep, vector<string>& strs, bool canEmpty, bool needTrim)
55 {
56     strs.clear();
57     string strTmp = needTrim ? TrimStr(str) : str;
58     string strPart;
59     while (true) {
60         string::size_type pos = strTmp.find(sep);
61         if (string::npos == pos || sep.empty()) {
62             strPart = needTrim ? TrimStr(strTmp) : strTmp;
63             if (!strPart.empty() || canEmpty) {
64                 strs.push_back(strPart);
65             }
66             break;
67         } else {
68             strPart = needTrim ? TrimStr(strTmp.substr(0, pos)) : strTmp.substr(0, pos);
69             if (!strPart.empty() || canEmpty) {
70                 strs.push_back(strPart);
71             }
72             strTmp = strTmp.substr(sep.size() + pos, strTmp.size() - sep.size() - pos);
73         }
74     }
75 }
76 }