From ff9b8bb838ecdfbfc1dc81038fcf3b2a87636982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Zrounba?= <6691770+clement-z@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:06:01 +0200 Subject: Initial release --- src/utils/strutils.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/utils/strutils.h (limited to 'src/utils/strutils.h') diff --git a/src/utils/strutils.h b/src/utils/strutils.h new file mode 100644 index 0000000..94e7ab7 --- /dev/null +++ b/src/utils/strutils.h @@ -0,0 +1,79 @@ +#pragma once + +#include // std::transform +#include + +namespace strutils { +// trim from start (in place) +inline void ltrim(std::string &s) +{ + s.erase(s.begin(), + std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); })); +} + +// trim from end (in place) +inline void rtrim(std::string &s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }) + .base(), + s.end()); +} + +// trim from both ends (in place) +inline void trim(std::string &s) +{ + ltrim(s); + rtrim(s); +} + +// trim from start (copying) +inline std::string ltrim_copy(std::string s) +{ + ltrim(s); + return s; +} + +// trim from end (copying) +inline std::string rtrim_copy(std::string s) +{ + rtrim(s); + return s; +} + +// trim from both ends (copying) +inline std::string trim_copy(std::string s) +{ + trim(s); + return s; +} +// trim from end (in place) +inline void rtrim_comments(std::string &s, const std::string &comment_tokens) +{ + size_t pos = s.find_first_of(comment_tokens); + if (pos != s.npos) + s.erase(pos); +} +inline void toupper(std::string &s) +{ + std::transform(s.begin(), s.end(), s.begin(), ::toupper); +} + +inline bool strcontains(const std::string &s, const std::string &sub) +{ + return s.find(sub) != std::string::npos; +} + +inline bool streq(const std::string &s1, const std::string &s2) +{ + return s1 == s2; +} + +inline bool iequals(const std::string& a, const std::string& b) +{ + return std::equal(a.begin(), a.end(), + b.begin(), b.end(), + [](char a, char b) { + return tolower(a) == tolower(b); + }); +} +}; -- cgit v1.2.3