Add util/string.hpp
This commit is contained in:
parent
68d8e4b18a
commit
0fecd6375b
3 changed files with 61 additions and 14 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include <psemek/gfx/array.hpp>
|
||||
#include <psemek/gfx/gl.hpp>
|
||||
|
||||
#include <psemek/util/string.hpp>
|
||||
#include <psemek/util/to_string.hpp>
|
||||
|
||||
#include <iomanip>
|
||||
|
|
@ -67,17 +68,6 @@ void main()
|
|||
}
|
||||
)";
|
||||
|
||||
static void 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();
|
||||
}
|
||||
}
|
||||
|
||||
static std::string generate_blur_fs_source(int size, float sigma, bool horizontal)
|
||||
{
|
||||
std::string size_str = util::to_string(size);
|
||||
|
|
@ -105,9 +95,9 @@ void main()
|
|||
}
|
||||
|
||||
std::string result = blur_fs_template;
|
||||
replace_all(result, "@BLUR_SIZE@", size_str);
|
||||
replace_all(result, "@BLUR_COEFFS@", coeffs_ss.str());
|
||||
replace_all(result, "@BLUR_DIRECTION@", direction_str);
|
||||
result = util::replace_all(std::move(result), "@BLUR_SIZE@", size_str);
|
||||
result = util::replace_all(std::move(result), "@BLUR_COEFFS@", coeffs_ss.str());
|
||||
result = util::replace_all(std::move(result), "@BLUR_DIRECTION@", direction_str);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
15
libs/util/include/psemek/util/string.hpp
Normal file
15
libs/util/include/psemek/util/string.hpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace psemek::util
|
||||
{
|
||||
|
||||
std::string replace_all(std::string str, std::string_view old_str, std::string_view new_str);
|
||||
|
||||
std::string trim_front(std::string str);
|
||||
std::string trim_back(std::string str);
|
||||
std::string trim(std::string str);
|
||||
|
||||
}
|
||||
42
libs/util/source/string.cpp
Normal file
42
libs/util/source/string.cpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#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)));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue