• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "util/string_helper.h"
10 
11 #include <cstdarg>
12 
13 #include "securec.h"
14 
15 namespace OHOS {
16 namespace HDI {
Split(std::string sources,const std::string & limit)17 std::vector<std::string> StringHelper::Split(std::string sources, const std::string &limit)
18 {
19     std::vector<std::string> result;
20     if (sources.empty()) {
21         return result;
22     }
23 
24     if (limit.empty()) {
25         result.push_back(sources);
26         return result;
27     }
28 
29     size_t begin = 0;
30     size_t pos = sources.find(limit, begin);
31     while (pos != std::string::npos) {
32         std::string element = sources.substr(begin, pos - begin);
33         if (!element.empty()) {
34             result.push_back(element);
35         }
36         begin = pos + limit.size();
37         pos = sources.find(limit, begin);
38     }
39 
40     if (begin < sources.size()) {
41         std::string element = sources.substr(begin);
42         result.push_back(element);
43     }
44     return result;
45 }
46 
StartWith(const std::string & value,char prefix)47 bool StringHelper::StartWith(const std::string &value, char prefix)
48 {
49     return value.find(prefix) == 0;
50 }
51 
StartWith(const std::string & value,const std::string & prefix)52 bool StringHelper::StartWith(const std::string &value, const std::string &prefix)
53 {
54     return value.find(prefix) == 0;
55 }
56 
EndWith(const std::string & value,char suffix)57 bool StringHelper::EndWith(const std::string &value, char suffix)
58 {
59     if (value.empty()) {
60         return false;
61     }
62     return value.back() == suffix;
63 }
64 
EndWith(const std::string & value,const std::string & suffix)65 bool StringHelper::EndWith(const std::string &value, const std::string &suffix)
66 {
67     size_t index = value.rfind(suffix);
68     if (index == std::string::npos) {
69         return false;
70     }
71 
72     return index + suffix.size() == value.size();
73 }
74 
Replace(const std::string & value,char oldChar,char newChar)75 std::string StringHelper::Replace(const std::string &value, char oldChar, char newChar)
76 {
77     if (value.empty() || oldChar == newChar) {
78         return value;
79     }
80 
81     std::string result = value;
82     for (size_t i = 0; i < result.size(); i++) {
83         if (result[i] != oldChar) {
84             continue;
85         }
86         result[i] = newChar;
87     }
88     return result;
89 }
90 
Replace(const std::string & value,const std::string & oldstr,const std::string & newstr)91 std::string StringHelper::Replace(const std::string &value, const std::string &oldstr, const std::string &newstr)
92 {
93     std::string result = value;
94     size_t pos = 0;
95     while ((pos = result.find(oldstr, pos)) != std::string::npos) {
96         result.replace(pos, oldstr.size(), newstr);
97         pos += newstr.size();
98     }
99     return result;
100 }
101 
Replace(const std::string & value,size_t position,const std::string & substr,const std::string & newstr)102 std::string StringHelper::Replace(
103     const std::string &value, size_t position, const std::string &substr, const std::string &newstr)
104 {
105     if (position >= value.size()) {
106         return value;
107     }
108 
109     std::string prefix = value.substr(0, position);
110     std::string suffix = value.substr(position);
111     return prefix + Replace(suffix, substr, newstr);
112 }
113 
Replace(const std::string & value,size_t position,size_t len,const std::string & newStr)114 std::string StringHelper::Replace(const std::string &value, size_t position, size_t len, const std::string &newStr)
115 {
116     if (position >= value.size() || len == 0) {
117         return value;
118     }
119 
120     std::string prefix = value.substr(0, position);
121     std::string suffix = value.substr(position);
122     return prefix + newStr + suffix;
123 }
124 
SubStr(const std::string & value,size_t start,size_t end)125 std::string StringHelper::SubStr(const std::string &value, size_t start, size_t end)
126 {
127     if (value.empty() || start == std::string::npos || start >= end) {
128         return "";
129     }
130     return (end == std::string::npos) ? value.substr(start) : value.substr(start, end - start);
131 }
132 
StrToLower(const std::string & value)133 std::string StringHelper::StrToLower(const std::string &value)
134 {
135     std::string result = value;
136     for (size_t i = 0; i < result.size(); i++) {
137         if (std::isupper(result[i])) {
138             result[i] = std::tolower(result[i]);
139         }
140     }
141     return result;
142 }
143 
StrToUpper(const std::string & value)144 std::string StringHelper::StrToUpper(const std::string &value)
145 {
146     std::string result = value;
147     for (size_t i = 0; i < result.size(); i++) {
148         if (std::islower(result[i])) {
149             result[i] = std::toupper(result[i]);
150         }
151     }
152     return result;
153 }
154 
Format(const char * format,...)155 std::string StringHelper::Format(const char *format, ...)
156 {
157     va_list args;
158     va_list argsCopy;
159 
160     va_start(args, format);
161     va_copy(argsCopy, args);
162 
163     char buf[lineMaxSize] = {0};
164     int len = vsnprintf_s(buf, lineMaxSize, lineMaxSize - 1, format, args);
165     if (len <= 0) {
166         va_end(args);
167         va_end(argsCopy);
168         return "";
169     }
170 
171     va_end(args);
172     va_end(argsCopy);
173     return std::string(buf, len);
174 }
175 } // namespace HDI
176 } // namespace OHOS