1 /*
2 * Copyright (c) 2024 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 "util/string_helper.h"
17
18 #include <cstdarg>
19
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace Idl {
Split(std::string sources,const std::string & limit)24 std::vector<std::string> StringHelper::Split(std::string sources, const std::string &limit)
25 {
26 std::vector<std::string> result;
27 if (sources.empty()) {
28 return result;
29 }
30
31 if (limit.empty()) {
32 result.push_back(sources);
33 return result;
34 }
35
36 size_t begin = 0;
37 size_t pos = sources.find(limit, begin);
38 while (pos != std::string::npos) {
39 std::string element = sources.substr(begin, pos - begin);
40 if (!element.empty()) {
41 result.push_back(element);
42 }
43 begin = pos + limit.size();
44 pos = sources.find(limit, begin);
45 }
46
47 if (begin < sources.size()) {
48 std::string element = sources.substr(begin);
49 result.push_back(element);
50 }
51 return result;
52 }
53
StartWith(const std::string & value,char prefix)54 bool StringHelper::StartWith(const std::string &value, char prefix)
55 {
56 return value.find(prefix) == 0;
57 }
58
StartWith(const std::string & value,const std::string & prefix)59 bool StringHelper::StartWith(const std::string &value, const std::string &prefix)
60 {
61 return value.find(prefix) == 0;
62 }
63
EndWith(const std::string & value,char suffix)64 bool StringHelper::EndWith(const std::string &value, char suffix)
65 {
66 if (value.empty()) {
67 return false;
68 }
69 return value.back() == suffix;
70 }
71
EndWith(const std::string & value,const std::string & suffix)72 bool StringHelper::EndWith(const std::string &value, const std::string &suffix)
73 {
74 size_t index = value.rfind(suffix);
75 if (index == std::string::npos) {
76 return false;
77 }
78
79 return index + suffix.size() == value.size();
80 }
81
Replace(const std::string & value,char oldChar,char newChar)82 std::string StringHelper::Replace(const std::string &value, char oldChar, char newChar)
83 {
84 if (value.empty() || oldChar == newChar) {
85 return value;
86 }
87
88 std::string result = value;
89 for (size_t i = 0; i < result.size(); i++) {
90 if (result[i] != oldChar) {
91 continue;
92 }
93 result[i] = newChar;
94 }
95 return result;
96 }
97
Replace(const std::string & value,const std::string & oldstr,const std::string & newstr)98 std::string StringHelper::Replace(const std::string &value, const std::string &oldstr, const std::string &newstr)
99 {
100 std::string result = value;
101 size_t pos = 0;
102 while ((pos = result.find(oldstr, pos)) != std::string::npos) {
103 result.replace(pos, oldstr.size(), newstr);
104 pos += newstr.size();
105 }
106 return result;
107 }
108
Replace(const std::string & value,size_t position,const std::string & substr,const std::string & newstr)109 std::string StringHelper::Replace(
110 const std::string &value, size_t position, const std::string &substr, const std::string &newstr)
111 {
112 if (position >= value.size()) {
113 return value;
114 }
115
116 std::string prefix = value.substr(0, position);
117 std::string suffix = value.substr(position);
118 return prefix + Replace(suffix, substr, newstr);
119 }
120
Replace(const std::string & value,size_t position,size_t len,const std::string & newStr)121 std::string StringHelper::Replace(const std::string &value, size_t position, size_t len, const std::string &newStr)
122 {
123 if (position >= value.size() || len == 0) {
124 return value;
125 }
126
127 std::string prefix = value.substr(0, position);
128 std::string suffix = value.substr(position);
129 return prefix + newStr + suffix;
130 }
131
SubStr(const std::string & value,size_t start,size_t end)132 std::string StringHelper::SubStr(const std::string &value, size_t start, size_t end)
133 {
134 if (value.empty() || start == std::string::npos || start >= end) {
135 return "";
136 }
137 return (end == std::string::npos) ? value.substr(start) : value.substr(start, end - start);
138 }
139
StrToLower(const std::string & value)140 std::string StringHelper::StrToLower(const std::string &value)
141 {
142 std::string result = value;
143 for (size_t i = 0; i < result.size(); i++) {
144 if (std::isupper(result[i])) {
145 result[i] = std::tolower(result[i]);
146 }
147 }
148 return result;
149 }
150
StrToUpper(const std::string & value)151 std::string StringHelper::StrToUpper(const std::string &value)
152 {
153 std::string result = value;
154 for (size_t i = 0; i < result.size(); i++) {
155 if (std::islower(result[i])) {
156 result[i] = std::toupper(result[i]);
157 }
158 }
159 return result;
160 }
161
FirstToUpper(const std::string & value)162 std::string StringHelper::FirstToUpper(const std::string &value)
163 {
164 std::string result = value;
165 if (!result.empty()) {
166 result[0] = std::toupper(result[0]);
167 }
168 return result;
169 }
170
Format(const char * format,...)171 std::string StringHelper::Format(const char *format, ...)
172 {
173 va_list args;
174 va_list argsCopy;
175
176 va_start(args, format);
177 va_copy(argsCopy, args);
178
179 char buf[lineMaxSize] = {0};
180 int len = vsnprintf_s(buf, lineMaxSize, lineMaxSize - 1, format, args);
181 if (len <= 0) {
182 va_end(args);
183 va_end(argsCopy);
184 return "";
185 }
186
187 va_end(args);
188 va_end(argsCopy);
189 return std::string(buf, len);
190 }
191
GetHashCode(const std::string & key)192 int StringHelper::GetHashCode(const std::string &key)
193 {
194 // BKDR Hash Function
195 unsigned int seed = 31; // 31 131 1313 13131 131313 etc..
196 unsigned int hash = 0;
197
198 const char* string = key.c_str();
199 if (string != nullptr) {
200 for (; *string; ++string) {
201 hash = hash * seed + (*string);
202 }
203 }
204 return (hash & 0x7FFFFFFF);
205 }
206
207 } // namespace Idl
208 } // namespace OHOS