• 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 #include "util/string_utils.h"
16 #include <cmath>
17 #include <sstream>
18 #include <iomanip>
19 #include <regex>
20 #include "string_ex.h"
21 using namespace std;
22 namespace OHOS {
23 namespace HiviewDFX {
StringUtils()24 StringUtils::StringUtils()
25 {
26 }
27 
~StringUtils()28 StringUtils::~StringUtils()
29 {
30 }
31 
StringSplit(const string & content,const string & split,vector<string> & result)32 void StringUtils::StringSplit(const string &content, const string &split, vector<string> &result)
33 {
34     SplitStr(content, split, result, false, false);
35 }
36 
StringRegex(const string & str,const string & pattern,const int & idx,string & result)37 void StringUtils::StringRegex(const string &str, const string &pattern, const int& idx, string &result)
38 {
39     smatch match;
40     if (regex_search(str, match, regex(pattern)) && match.size() > static_cast<size_t>(idx)) {
41         result = match[idx].str();
42     }
43 }
44 
IsBegin(const string & content,const string & begin)45 bool StringUtils::IsBegin(const string &content, const string &begin)
46 {
47     if (content.find(begin) == 0) {
48         return true;
49     }
50     return false;
51 }
52 
IsEnd(const string & content,const string & end)53 bool StringUtils::IsEnd(const string &content, const string &end)
54 {
55     bool result = false;
56     if (content.length() >= end.length()) {
57         result = (0 == content.compare(content.length() - end.length(), end.length(), end));
58     }
59     return result;
60 }
61 
IsContain(const string & content,const string & contain)62 bool StringUtils::IsContain(const string &content, const string &contain)
63 {
64     return IsSubStr(content, contain);
65 }
66 
IsSameStr(const string & first,const string & second)67 bool StringUtils::IsSameStr(const string &first, const string &second)
68 {
69     if (first == second) {
70         return true;
71     }
72     return false;
73 }
74 
ReplaceAll(string & str,const string & oldValue,const string & newValue)75 void StringUtils::ReplaceAll(string &str, const string &oldValue, const string &newValue)
76 {
77     str = ReplaceStr(str, oldValue, newValue);
78 }
79 
IsNum(string str)80 bool StringUtils::IsNum(string str)
81 {
82     return IsNumericStr(str);
83 }
84 
Compare(const string & str,const vector<string> & strs)85 bool StringUtils::Compare(const string &str, const vector<string> &strs)
86 {
87     bool success = false;
88     for (const string &tempStr : strs) {
89         if (str == tempStr) {
90             success = true;
91         }
92     }
93     return success;
94 }
95 
HexToDec(const string & str,uint64_t & value)96 void StringUtils::HexToDec(const string &str, uint64_t &value)
97 {
98     size_t l = str.length();
99     for (size_t i = 0; i < l; i++) {
100         if (str[i] >= '0' && str[i] <= '9')
101             value += (str[i] - '0') * pow(HEX_STR, l - 1 - i);
102         else
103             value += (str[i] - 'A' + DEC_STR) * pow(HEX_STR, l - 1 - i);
104     }
105 }
106 
GetBlank()107 char StringUtils::GetBlank()
108 {
109     char blank = ' ';
110     return blank;
111 };
112 
GetSeparator()113 char StringUtils::GetSeparator()
114 {
115     char separator = '-';
116     return separator;
117 }
118 
119 /**
120  * @description: The character length is insufficient to complement
121  * @param {string} &str-The string to be filled
122  * @param {int} &length-The length of the string to be set
123  * @param {char} &fileStr-A character to be added when the length is insufficient
124  * @param {bool} &left-true:Add characters on the left,false:Add characters to the right
125  * @return {*}
126  */
127 
SetWidth(const int & width,const char & fileStr,const bool & left,string & str)128 void StringUtils::SetWidth(const int &width, const char &fileStr, const bool &left, string &str)
129 {
130     ostringstream s;
131     s.clear();
132 
133     if (left) {
134         s << setw(width) << setfill(fileStr) << setiosflags(ios::left) << str;
135     } else {
136         s << setw(width) << setfill(fileStr) << setiosflags(ios::right) << str;
137     }
138     str = s.str();
139 }
140 } // namespace HiviewDFX
141 } // namespace OHOS
142