• 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 
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 const uint32_t COMPARE_SUCCESS = 0;
27 }
SplitString(const std::string & str,std::vector<std::string> & ret,const std::string & sep)28 void StringOperation::SplitString(const std::string& str, std::vector<std::string>& ret, const std::string& sep)
29 {
30     ClearAllSpace(str);
31     if (str.empty()) {
32         return;
33     }
34 
35     std::string temp;
36     std::string::size_type begin = str.find_first_not_of(sep);
37     while (begin != std::string::npos) {
38         std::string::size_type pos = str.find(sep, begin);
39         if (pos != std::string::npos) {
40             temp = str.substr(begin, pos - begin);
41             begin = pos + sep.length();
42         } else {
43             temp = str.substr(begin);
44             begin = pos;
45         }
46 
47         if (!temp.empty()) {
48             ret.push_back(temp);
49             temp.clear();
50         }
51     }
52     return;
53 }
54 
ClearAllSpace(std::string s)55 void StringOperation::ClearAllSpace(std::string s)
56 {
57     std::string::size_type index = 0;
58     if (!s.empty()) {
59         index = s.find(' ', index);
60         while (index != std::string::npos) {
61             s.erase(index, 1);
62         }
63     }
64 }
65 
Compare(const std::string & origin,const std::string & target)66 bool StringOperation::Compare(const std::string& origin, const std::string& target)
67 {
68     if (origin.compare(target) == COMPARE_SUCCESS) {
69         return true;
70     }
71     return false;
72 }
73 
Find(const std::string & origin,const std::string & target)74 bool StringOperation::Find(const std::string& origin, const std::string& target)
75 {
76     if (origin.find(target) != std::string::npos) {
77         return true;
78     }
79     return false;
80 }
81 
StrToUint(const std::string & str,uint32_t & value)82 bool StringOperation::StrToUint(const std::string& str, uint32_t& value)
83 {
84     if (str.empty() || (!isdigit(str.front()) && (str.front() != '-'))) {
85         return false;
86     }
87 
88     char* end = nullptr;
89     errno = 0;
90     auto addr = str.c_str();
91     auto result = strtoul(addr, &end, 10); // 10 means decimal
92     if ((end == addr) || (end[0] != '\0') || (errno == ERANGE) || (result >= ULONG_MAX)) {
93         return false;
94     }
95     value = static_cast<uint32_t>(result);
96     return true;
97 }
98 
StrToDouble(const std::string & str,double & value)99 bool StringOperation::StrToDouble(const std::string& str, double& value)
100 {
101     if (str.empty()) {
102         return false;
103     }
104 
105     char* end = nullptr;
106     errno = 0;
107     auto addr = str.c_str();
108     auto result = strtod(addr, &end);
109     if ((end == addr) || (end[0] != '\0') || (errno == ERANGE) || (result == HUGE_VAL)) {
110         return false;
111     }
112     value = result;
113     return true;
114 }
115 } // namespace PowerMgr
116 } // namespace OHOS
117