forked from xbmc/inputstream.adaptive
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStringUtils.cpp
More file actions
311 lines (267 loc) · 7.93 KB
/
Copy pathStringUtils.cpp
File metadata and controls
311 lines (267 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (C) 2022 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "StringUtils.h"
#include "kodi/tools/StringUtils.h"
#include <algorithm>
#include <cctype> // isspace
#include <charconv> // from_chars
#include <cstdio>
#include <cstring> // strstr
#include <iomanip>
#include <sstream>
#include <iterator>
using namespace UTILS::STRING;
using namespace kodi::tools;
namespace
{
/*!
* \brief Converts a string to a number of a specified type, by using istringstream.
* \param str The string to convert
* \param fallback [OPT] The number to return when the conversion fails
* \return The converted number, otherwise fallback if conversion fails
*/
template<typename T>
T NumberFromSS(std::string_view str, T fallback) noexcept
{
std::istringstream iss{str.data()};
T result{fallback};
iss >> result;
return result;
}
} // namespace
bool UTILS::STRING::ReplaceFirst(std::string& inputStr,
std::string_view oldStr,
std::string_view newStr)
{
size_t start_pos = inputStr.find(oldStr);
if (start_pos == std::string::npos)
return false;
inputStr.replace(start_pos, oldStr.length(), newStr);
return true;
}
int UTILS::STRING::ReplaceAll(std::string& inputStr,
std::string_view oldStr,
std::string_view newStr)
{
if (oldStr.empty())
return 0;
int replacedChars = 0;
size_t index = 0;
while (index < inputStr.size() && (index = inputStr.find(oldStr, index)) != std::string::npos)
{
inputStr.replace(index, oldStr.size(), newStr);
index += newStr.size();
replacedChars++;
}
return replacedChars;
}
std::string UTILS::STRING::ToDecimal(const uint8_t* data, size_t dataSize)
{
std::stringstream ret;
if (dataSize)
ret << static_cast<unsigned int>(data[0]);
for (size_t i{1}; i < dataSize; ++i)
{
ret << ',' << static_cast<unsigned int>(data[i]);
}
return ret.str();
}
unsigned char UTILS::STRING::ToHexNibble(char nibble)
{
if (nibble >= '0' && nibble <= '9')
return nibble - '0';
else if (nibble >= 'a' && nibble <= 'f')
return 10 + (nibble - 'a');
else if (nibble >= 'A' && nibble <= 'F')
return 10 + (nibble - 'A');
return 0;
}
std::string UTILS::STRING::URLDecode(std::string_view strURLData)
// Taken from xbmc/URL.cpp
// modified to be more accommodating - if a non hex value follows a % take the characters directly and don't raise an error.
// However % characters should really be escaped like any other non safe character (www.rfc-editor.org/rfc/rfc1738.txt)
{
std::string strResult;
/* result will always be less than source */
strResult.reserve(strURLData.length());
for (unsigned int i = 0; i < strURLData.size(); ++i)
{
const char kar = strURLData[i];
if (kar == '+')
{
strResult += ' ';
}
else if (kar == '%')
{
if (i < strURLData.size() - 2)
{
std::string strTmp{strURLData.substr(i + 1, 2)};
int dec_num = -1;
sscanf(strTmp.c_str(), "%x", reinterpret_cast<unsigned int*>(&dec_num));
if (dec_num < 0 || dec_num > 255)
strResult += kar;
else
{
strResult += static_cast<char>(dec_num);
i += 2;
}
}
else
strResult += kar;
}
else
strResult += kar;
}
return strResult;
}
std::string UTILS::STRING::URLEncode(std::string_view strURLData)
{
std::string result;
for (auto c : strURLData)
{
// Don't URL encode "-_.!()" according to RFC1738
// Don't URL encode "-_.~" according to RFC3986
if (StringUtils::IsAsciiAlphaNum(c) || c == '-' || c == '.' || c == '_' || c == '!' ||
c == '(' || c == ')' || c == '~')
{
result.push_back(c);
}
else
{
result.append("%");
char buf[3];
sprintf(buf, "%.2X", c);
result.append(buf);
}
}
return result;
}
uint32_t UTILS::STRING::ToUint32(std::string_view str, uint32_t fallback /* = 0 */)
{
return NumberFromSS(str, fallback);
}
uint64_t UTILS::STRING::ToUint64(std::string_view str, uint64_t fallback /* = 0 */)
{
return NumberFromSS(str, fallback);
}
double UTILS::STRING::ToDouble(std::string_view str, double fallback)
{
return NumberFromSS(str, fallback);
}
float UTILS::STRING::ToFloat(std::string_view str, float fallback)
{
return NumberFromSS(str, fallback);
}
int UTILS::STRING::ToInt32(std::string_view str, int fallback /* = 0 */)
{
int result = fallback;
std::from_chars(str.data(), str.data() + str.size(), result);
return result;
}
bool UTILS::STRING::Contains(std::string_view str,
std::string_view keyword,
bool isCaseInsensitive /* = true */)
{
if (isCaseInsensitive)
{
auto itStr = std::search(str.begin(), str.end(), keyword.begin(), keyword.end(),
[](unsigned char ch1, unsigned char ch2)
{ return std::toupper(ch1) == std::toupper(ch2); });
return (itStr != str.end());
}
return std::strstr(str.data(), keyword.data()) != nullptr;
}
bool UTILS::STRING::StartsWith(std::string_view str, std::string_view startStr)
{
return str.substr(0, startStr.size()) == startStr;
}
std::set<std::string> UTILS::STRING::Split(std::string_view input,
const char delimiter,
int maxStrings /* = 0 */)
{
std::set<std::string> result;
StringUtils::SplitTo(std::inserter(result, result.end()), input.data(), delimiter, maxStrings);
return result;
}
bool UTILS::STRING::Compare(std::string_view str1, std::string_view str2)
{
return str1.compare(str2) == 0;
}
bool UTILS::STRING::CompareNoCase(std::string_view str1, std::string_view str2)
{
if (str1.size() != str2.size())
return false;
return std::equal(str1.cbegin(), str1.cend(), str2.cbegin(),
[](std::string::value_type l, std::string::value_type r)
{ return std::tolower(l) == std::tolower(r); });
}
bool UTILS::STRING::GetLine(std::stringstream& ss, std::string& line)
{
do
{
if (!std::getline(ss, line))
return false;
// Trim return chars and spaces at the end of string
size_t charPos = line.size();
while (charPos &&
(line[charPos - 1] == '\r' || line[charPos - 1] == '\n' || line[charPos - 1] == ' '))
{
charPos--;
}
line.resize(charPos);
// Skip possible empty lines
} while (line.empty());
return true;
}
std::string UTILS::STRING::ToLower(std::string str)
{
StringUtils::ToLower(str);
return str;
}
std::map<std::string_view, std::string_view> UTILS::STRING::ToMap(std::string_view str,
const char delimiter,
const char separator)
{
std::map<std::string_view, std::string_view> mapped;
size_t keyPos = 0;
size_t keyEnd;
size_t valPos;
size_t valEnd;
while ((keyEnd = str.find(delimiter, keyPos)) != std::string::npos)
{
valPos = str.find_first_not_of(delimiter, keyEnd);
if (valPos == std::string::npos)
break;
valEnd = str.find(separator, valPos);
mapped.emplace(str.substr(keyPos, keyEnd - keyPos), str.substr(valPos, valEnd - valPos));
keyPos = valEnd;
if (keyPos != std::string::npos)
++keyPos;
}
return mapped;
}
std::string_view UTILS::STRING::Trim(std::string_view str)
{
auto left = str.begin();
while (left != str.end())
{
if (!std::isspace(*left))
break;
left++;
}
if (left == str.end())
return {};
auto right = str.end() - 1;
while (right > left && std::isspace(*right))
{
right--;
}
//! @todo: when we will switch to C++20 replace return code with:
//! return {left, std::distance(left, right) + 1};
return str.substr(left - str.begin(), std::distance(left, right) + 1);
}