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