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 
IsBegin(const string & content,const string & begin)37 bool StringUtils::IsBegin(const string &content, const string &begin)
38 {
39     if (content.find(begin) == 0) {
40         return true;
41     }
42     return false;
43 }
44 
IsEnd(const string & content,const string & end)45 bool StringUtils::IsEnd(const string &content, const string &end)
46 {
47     bool result = false;
48     if (content.length() >= end.length()) {
49         result = (0 == content.compare(content.length() - end.length(), end.length(), end));
50     }
51     return result;
52 }
53 
IsContain(const string & content,const string & contain)54 bool StringUtils::IsContain(const string &content, const string &contain)
55 {
56     return IsSubStr(content, contain);
57 }
58 
IsSameStr(const string & first,const string & second)59 bool StringUtils::IsSameStr(const string &first, const string &second)
60 {
61     if (first == second) {
62         return true;
63     }
64     return false;
65 }
66 
ReplaceAll(string & str,const string & oldValue,const string & newValue)67 void StringUtils::ReplaceAll(string &str, const string &oldValue, const string &newValue)
68 {
69     str = ReplaceStr(str, oldValue, newValue);
70 }
71 
IsNum(string str)72 bool StringUtils::IsNum(string str)
73 {
74     return IsNumericStr(str);
75 }
76 
HexToDec(const string & str,uint64_t & value)77 void StringUtils::HexToDec(const string &str, uint64_t &value)
78 {
79     size_t l = str.length();
80     for (size_t i = 0; i < l; i++) {
81         if (str[i] >= '0' && str[i] <= '9')
82             value += (str[i] - '0') * pow(HEX_STR, l - 1 - i);
83         else
84             value += (str[i] - 'A' + DEC_STR) * pow(HEX_STR, l - 1 - i);
85     }
86 }
87 
GetBlank()88 char StringUtils::GetBlank()
89 {
90     char blank = ' ';
91     return blank;
92 };
93 
GetSeparator()94 char StringUtils::GetSeparator()
95 {
96     char separator = '-';
97     return separator;
98 }
99 
100 /**
101  * @description: The character length is insufficient to complement
102  * @param {string} &str-The string to be filled
103  * @param {int} &length-The length of the string to be set
104  * @param {char} &fileStr-A character to be added when the length is insufficient
105  * @param {bool} &left-true:Add characters on the left,false:Add characters to the right
106  * @return {*}
107  */
108 
SetWidth(const int & width,const char & fileStr,const bool & left,string & str)109 void StringUtils::SetWidth(const int &width, const char &fileStr, const bool &left, string &str)
110 {
111     ostringstream s;
112     s.clear();
113 
114     if (left) {
115         s << setw(width) << setfill(fileStr) << setiosflags(ios::left) << str;
116     } else {
117         s << setw(width) << setfill(fileStr) << setiosflags(ios::right) << str;
118     }
119     str = s.str();
120 }
121 } // namespace HiviewDFX
122 } // namespace OHOS
123