1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 "string_utils.h"
16 #include <sstream>
17 #include <iomanip>
18 
EndsWith(const std::string& str, const std::string& postfix)19 bool StringUtils::EndsWith(const std::string& str, const std::string& postfix)
20 {
21     if (str.size() < postfix.size()) {
22         return false;
23     }
24     return str.compare(str.size() - postfix.size(), postfix.size(), postfix) == 0;
25 }
26 
StartsWith(const std::string& str, const std::string& prefix)27 bool StringUtils::StartsWith(const std::string& str, const std::string& prefix)
28 {
29     if (str.size() < prefix.size()) {
30         return false;
31     }
32     return str.compare(0, prefix.size(), prefix) == 0;
33 }
34 
ConvertToHex(uint64_t num)35 std::string StringUtils::ConvertToHex(uint64_t num)
36 {
37     std::stringstream ss;
38     ss << std::hex << num;
39     return ss.str();
40 }
41 
Contains(const std::string& str, const std::string& target)42 bool StringUtils::Contains(const std::string& str, const std::string& target)
43 {
44     return str.find(target) != std::string::npos;
45 }
46 
Strip(const std::string& str)47 std::string StringUtils::Strip(const std::string& str)
48 {
49     std::string blanks = " \f\v\t\r\n";
50 
51     auto first = str.find_first_not_of(blanks);
52     if (first == std::string::npos) {
53         return "";
54     }
55 
56     auto last = str.find_last_not_of(blanks);
57     if (last == std::string::npos) {
58         return "";
59     }
60     return str.substr(first, last - first + 1);
61 }
62 
Join(const std::vector<std::string>& strs, const std::string& sep)63 std::string StringUtils::Join(const std::vector<std::string>& strs, const std::string& sep)
64 {
65     std::string result;
66     for (auto& s : strs) {
67         result += s;
68         result += sep;
69     }
70     if (result.size() > sep.size()) {
71         result.resize(result.size() - sep.size());
72     }
73     return result;
74 }
75 
Split(const std::string& str, const std::string& sep)76 std::vector<std::string> StringUtils::Split(const std::string& str, const std::string& sep)
77 {
78     std::vector<std::string> result;
79     if (str.empty() || sep.empty() || str.size() < sep.size()) {
80         return result;
81     }
82     size_t start = 0; // start index
83     size_t pos = 0;
84     while (start < str.size()) {
85         pos = str.find(sep, start);
86         if (pos != std::string::npos) {
87             result.push_back(str.substr(start, pos - start));
88             start = pos + sep.size(); // next start index
89         } else {
90             // the last part
91             result.push_back(str.substr(start));
92             break;
93         }
94     }
95     return result;
96 }