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
16 #include "string_operation.h"
17
18 #include <cstdint>
19 #include <string>
20
21 namespace OHOS {
22 namespace PowerMgr {
23 namespace {
24 const uint32_t COMPARE_SUCCESS = 0;
25 }
SplitString(const std::string & str,std::vector<std::string> & ret,const std::string & sep)26 void StringOperation::SplitString(const std::string &str, std::vector<std::string> &ret, const std::string &sep)
27 {
28 ClearAllSpace(str);
29 if (str.empty()) {
30 return;
31 }
32
33 std::string temp;
34 std::string::size_type begin = str.find_first_not_of(sep);
35 while (begin != std::string::npos) {
36 std::string::size_type pos = str.find(sep, begin);
37 if (pos != std::string::npos) {
38 temp = str.substr(begin, pos - begin);
39 begin = pos + sep.length();
40 } else {
41 temp = str.substr(begin);
42 begin = pos;
43 }
44
45 if (!temp.empty()) {
46 ret.push_back(temp);
47 temp.clear();
48 }
49 }
50 return;
51 }
52
ClearAllSpace(std::string s)53 void StringOperation::ClearAllSpace(std::string s)
54 {
55 std::string::size_type index = 0;
56 if (!s.empty()) {
57 index = s.find(' ', index);
58 while (index != std::string::npos) {
59 s.erase(index, 1);
60 }
61 }
62 }
63
Compare(const std::string & origin,const std::string & target)64 bool StringOperation::Compare(const std::string &origin, const std::string &target)
65 {
66 if (origin.compare(target) == COMPARE_SUCCESS) {
67 return true;
68 }
69 return false;
70 }
71
Find(const std::string & origin,const std::string & target)72 bool StringOperation::Find(const std::string &origin, const std::string &target)
73 {
74 if (origin.find(target) != std::string::npos) {
75 return true;
76 }
77 return false;
78 }
79 } // namespace PowerMgr
80 } // namespace OHOS