42 lines
815 B
C++
42 lines
815 B
C++
#include <psemek/util/string.hpp>
|
|
|
|
namespace psemek::util
|
|
{
|
|
|
|
std::string replace_all(std::string str, std::string_view old_str, std::string_view new_str)
|
|
{
|
|
for (size_t i = 0;;)
|
|
{
|
|
i = str.find(old_str, i);
|
|
if (i == std::string::npos) break;
|
|
str.replace(i, old_str.size(), new_str);
|
|
i += new_str.size();
|
|
}
|
|
return str;
|
|
}
|
|
|
|
static std::string const whitespace_mask = " \t\n\r\f\v";
|
|
|
|
std::string trim_front(std::string str)
|
|
{
|
|
str.erase(0, str.find_first_not_of(whitespace_mask));
|
|
return str;
|
|
}
|
|
|
|
std::string trim_back(std::string str)
|
|
{
|
|
size_t pos = str.find_last_not_of(whitespace_mask);
|
|
if (pos != std::string::npos) {
|
|
str.erase(pos + 1);
|
|
} else {
|
|
str.clear();
|
|
}
|
|
return str;
|
|
}
|
|
|
|
std::string trim(std::string str)
|
|
{
|
|
return trim_back(trim_front(std::move(str)));
|
|
}
|
|
|
|
}
|