diff --git a/libs/gfx/source/effect/blur.cpp b/libs/gfx/source/effect/blur.cpp index 4ded46d1..8c1fb490 100644 --- a/libs/gfx/source/effect/blur.cpp +++ b/libs/gfx/source/effect/blur.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -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; } diff --git a/libs/util/include/psemek/util/string.hpp b/libs/util/include/psemek/util/string.hpp new file mode 100644 index 00000000..a405086d --- /dev/null +++ b/libs/util/include/psemek/util/string.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +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); + +} diff --git a/libs/util/source/string.cpp b/libs/util/source/string.cpp new file mode 100644 index 00000000..456c31bd --- /dev/null +++ b/libs/util/source/string.cpp @@ -0,0 +1,42 @@ +#include + +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))); + } + +}