• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_operation.h"
17 
18 #include <cmath>
19 #include <cstdint>
20 #include <limits.h>
21 #include <string>
22 #include "thermal_log.h"
23 
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 const uint32_t COMPARE_SUCCESS = 0;
28 }
SplitString(const std::string & str,std::vector<std::string> & ret,const std::string & sep)29 void StringOperation::SplitString(const std::string& str, std::vector<std::string>& ret, const std::string& sep)
30 {
31     ClearAllSpace(str);
32     if (str.empty()) {
33         return;
34     }
35 
36     std::string temp;
37     std::string::size_type begin = str.find_first_not_of(sep);
38     while (begin != std::string::npos) {
39         std::string::size_type pos = str.find(sep, begin);
40         if (pos != std::string::npos) {
41             temp = str.substr(begin, pos - begin);
42             begin = pos + sep.length();
43         } else {
44             temp = str.substr(begin);
45             begin = pos;
46         }
47 
48         if (!temp.empty()) {
49             ret.push_back(temp);
50             temp.clear();
51         }
52     }
53     return;
54 }
55 
ClearAllSpace(std::string s)56 void StringOperation::ClearAllSpace(std::string s)
57 {
58     std::string::size_type index = 0;
59     if (!s.empty()) {
60         index = s.find(' ', index);
61         while (index != std::string::npos) {
62             s.erase(index, 1);
63         }
64     }
65 }
66 
Compare(const std::string & origin,const std::string & target)67 bool StringOperation::Compare(const std::string& origin, const std::string& target)
68 {
69     if (origin.compare(target) == COMPARE_SUCCESS) {
70         return true;
71     }
72     return false;
73 }
74 
Find(const std::string & origin,const std::string & target)75 bool StringOperation::Find(const std::string& origin, const std::string& target)
76 {
77     if (origin.find(target) != std::string::npos) {
78         return true;
79     }
80     return false;
81 }
82 
StrToUint(const std::string & str,uint32_t & value)83 bool StringOperation::StrToUint(const std::string& str, uint32_t& value)
84 {
85     if (str.empty() || (!isdigit(str.front()) && (str.front() != '-'))) {
86         return false;
87     }
88 
89     char* end = nullptr;
90     errno = 0;
91     auto addr = str.c_str();
92     auto result = strtoul(addr, &end, 10); // 10 means decimal
93     if ((end == addr) || (end[0] != '\0') || (errno == ERANGE) || (result >= ULONG_MAX)) {
94         return false;
95     }
96     value = static_cast<uint32_t>(result);
97     return true;
98 }
99 
StrToDouble(const std::string & str,double & value)100 bool StringOperation::StrToDouble(const std::string& str, double& value)
101 {
102     if (str.empty()) {
103         return false;
104     }
105 
106     char* end = nullptr;
107     errno = 0;
108     auto addr = str.c_str();
109     auto result = strtod(addr, &end);
110     if ((end == addr) || (end[0] != '\0') || (errno == ERANGE) || (result == HUGE_VAL)) {
111         return false;
112     }
113     value = result;
114     return true;
115 }
116 
ParseStrtollResult(const std::string & str,int64_t & result)117 bool StringOperation::ParseStrtollResult(const std::string& str, int64_t& result)
118 {
119     constexpr int PARAMETER_TEN = 10;
120     errno = 0;
121     char* endptr = nullptr;
122     result = strtoll(str.c_str(), &endptr, PARAMETER_TEN);
123     if (endptr == str.c_str()) {
124         THERMAL_HILOGE(COMP_SVC, "String have no numbers, string:%{public}s", str.c_str());
125         return false;
126     }
127     if (errno == ERANGE && (result == LLONG_MAX || result == LLONG_MIN)) {
128         THERMAL_HILOGE(COMP_SVC, "Transit result out of range, string:%{public}s", str.c_str());
129         return false;
130     }
131     if (*endptr != '\0') {
132         THERMAL_HILOGE(COMP_SVC, "String contain non-numeric characters, string:%{public}s", str.c_str());
133     }
134     return true;
135 }
136 
ParseStrtoulResult(const std::string & str,unsigned long & result)137 bool StringOperation::ParseStrtoulResult(const std::string& str, unsigned long& result)
138 {
139     constexpr unsigned long ULONG_MIN = 0;
140     constexpr int PARAMETER_TEN = 10;
141     errno = 0;
142     char* endptr = nullptr;
143     result = strtoul(str.c_str(), &endptr, PARAMETER_TEN);
144     if (endptr == str.c_str()) {
145         THERMAL_HILOGE(COMP_SVC, "String have no numbers, string:%{public}s", str.c_str());
146         return false;
147     }
148     if (errno == ERANGE && (result == ULONG_MAX || result == ULONG_MIN)) {
149         THERMAL_HILOGE(COMP_SVC, "Transit result out of range, string:%{public}s", str.c_str());
150         return false;
151     }
152     if (*endptr != '\0') {
153         THERMAL_HILOGE(COMP_SVC, "String contain non-numeric characters, string:%{public}s", str.c_str());
154     }
155     return true;
156 }
157 } // namespace PowerMgr
158 } // namespace OHOS
159