mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-15 07:35:41 +02:00
move string utils in namespace utils::string
This commit is contained in:
parent
b8f69ce58f
commit
fa3f78b08d
12 changed files with 178 additions and 133 deletions
103
common/utils/string_utils.h
Normal file
103
common/utils/string_utils.h
Normal file
|
@ -0,0 +1,103 @@
|
|||
#ifndef STRING_UTILS_H
|
||||
#define STRING_UTILS_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace utils
|
||||
{
|
||||
namespace string
|
||||
{
|
||||
|
||||
// trim from start
|
||||
static inline std::string <rim(std::string &s)
|
||||
{
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
||||
return s;
|
||||
}
|
||||
|
||||
// trim from end
|
||||
static inline std::string &rtrim(std::string &s)
|
||||
{
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
||||
return s;
|
||||
}
|
||||
|
||||
// trim from both ends
|
||||
static inline std::string &trim(std::string &s)
|
||||
{
|
||||
return ltrim(rtrim(s));
|
||||
}
|
||||
|
||||
// trim from start
|
||||
static inline std::string ltrim_copy(const std::string &s)
|
||||
{
|
||||
std::string str(s);
|
||||
return ltrim(str);
|
||||
}
|
||||
|
||||
// trim from end
|
||||
static inline std::string rtrim_copy(const std::string &s)
|
||||
{
|
||||
std::string str(s);
|
||||
return rtrim(str);
|
||||
}
|
||||
|
||||
// trim from both ends
|
||||
static inline std::string trim_copy(const std::string &s)
|
||||
{
|
||||
std::string str(s);
|
||||
return trim(str);
|
||||
}
|
||||
|
||||
// decode %xx to char
|
||||
static std::string uriDecode(const std::string& src) {
|
||||
std::string ret;
|
||||
char ch;
|
||||
for (size_t i=0; i<src.length(); i++)
|
||||
{
|
||||
if (int(src[i]) == 37)
|
||||
{
|
||||
unsigned int ii;
|
||||
sscanf(src.substr(i+1, 2).c_str(), "%x", &ii);
|
||||
ch = static_cast<char>(ii);
|
||||
ret += ch;
|
||||
i += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret += src[i];
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
|
||||
{
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, delim))
|
||||
{
|
||||
elems.push_back(item);
|
||||
}
|
||||
return elems;
|
||||
}
|
||||
|
||||
|
||||
static std::vector<std::string> split(const std::string &s, char delim)
|
||||
{
|
||||
std::vector<std::string> elems;
|
||||
split(s, delim, elems);
|
||||
return elems;
|
||||
}
|
||||
|
||||
} // namespace string
|
||||
} // namespace utils
|
||||
|
||||
#endif
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue