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