Skip to content

Commit 25dc50c

Browse files
committed
[StringUtils] Add Trim, ToMap methods
1 parent 27ffa26 commit 25dc50c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/utils/StringUtils.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "kodi/tools/StringUtils.h"
1212

1313
#include <algorithm>
14+
#include <cctype> // isspace
1415
#include <charconv> // from_chars
1516
#include <cstdio>
1617
#include <cstring> // strstr
@@ -249,3 +250,56 @@ bool UTILS::STRING::GetLine(std::stringstream& ss, std::string& line)
249250

250251
return true;
251252
}
253+
254+
std::map<std::string_view, std::string_view> UTILS::STRING::ToMap(std::string_view str,
255+
const char delimiter,
256+
const char separator)
257+
{
258+
std::map<std::string_view, std::string_view> mapped;
259+
260+
size_t keyPos = 0;
261+
size_t keyEnd;
262+
size_t valPos;
263+
size_t valEnd;
264+
265+
while ((keyEnd = str.find(delimiter, keyPos)) != std::string::npos)
266+
{
267+
valPos = str.find_first_not_of(delimiter, keyEnd);
268+
if (valPos == std::string::npos)
269+
break;
270+
271+
valEnd = str.find(separator, valPos);
272+
mapped.emplace(str.substr(keyPos, keyEnd - keyPos), str.substr(valPos, valEnd - valPos));
273+
274+
keyPos = valEnd;
275+
if (keyPos != std::string::npos)
276+
++keyPos;
277+
}
278+
279+
return mapped;
280+
}
281+
282+
std::string_view UTILS::STRING::Trim(std::string_view str)
283+
{
284+
auto left = str.begin();
285+
while (left != str.end())
286+
{
287+
if (!std::isspace(*left))
288+
break;
289+
290+
left++;
291+
}
292+
293+
if (left == str.end())
294+
return {};
295+
296+
auto right = str.end() - 1;
297+
while (right > left && std::isspace(*right))
298+
{
299+
right--;
300+
}
301+
302+
//! @todo: when we will switch to C++20 replace return code with:
303+
//! return {left, std::distance(left, right) + 1};
304+
return str.substr(left - str.begin(), std::distance(left, right) + 1);
305+
}

src/utils/StringUtils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
#include <cstdint>
12+
#include <map>
1213
#include <set>
1314
#include <string>
1415
#include <string_view>
@@ -157,5 +158,22 @@ bool CompareNoCase(std::string_view str1, std::string_view str2);
157158
*/
158159
bool GetLine(std::stringstream& ss, std::string& line);
159160

161+
/*!
162+
* \brief Convert a string to a map.
163+
* \param str The string to convert
164+
* \param delimiter The character separating the key from the value
165+
* \param separator The character separating more key/value pairs
166+
* \return The mapped string.
167+
*/
168+
std::map<std::string_view, std::string_view> ToMap(std::string_view str,
169+
const char delimiter,
170+
const char separator);
171+
172+
/*!
173+
* \brief Trim a string with remove of not wanted spaces at begin and end of string.
174+
* \return The changed string.
175+
*/
176+
std::string_view Trim(std::string_view str);
177+
160178
} // namespace STRING
161179
} // namespace UTILS

0 commit comments

Comments
 (0)