summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libadhocutil/strUtil.h29
-rw-r--r--libadhocutil/unittests/Jamfile.jam1
-rw-r--r--libadhocutil/unittests/testStrUtil.cpp45
3 files changed, 75 insertions, 0 deletions
diff --git a/libadhocutil/strUtil.h b/libadhocutil/strUtil.h
new file mode 100644
index 0000000..b366305
--- /dev/null
+++ b/libadhocutil/strUtil.h
@@ -0,0 +1,29 @@
+#include <string_view>
+
+namespace AdHoc {
+ constexpr auto &
+ rtrim(std::string_view & in, const auto c)
+ {
+ if (const auto n = in.find_last_not_of(c); n != std::string_view::npos) {
+ in.remove_suffix(in.length() - n - 1);
+ }
+ return in;
+ }
+
+ constexpr auto &
+ ltrim(std::string_view & in, const auto c)
+ {
+ if (const auto n = in.find_first_not_of(c); n != std::string_view::npos) {
+ in.remove_prefix(n);
+ }
+ return in;
+ }
+
+ constexpr auto &
+ trim(std::string_view & in, const auto c)
+ {
+ ltrim(in, c);
+ rtrim(in, c);
+ return in;
+ }
+}
diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam
index 926c4cc..adb2645 100644
--- a/libadhocutil/unittests/Jamfile.jam
+++ b/libadhocutil/unittests/Jamfile.jam
@@ -348,3 +348,4 @@ run
<library>boost_utf
;
+obj testStrUtil : testStrUtil.cpp : <use>..//adhocutil ;
diff --git a/libadhocutil/unittests/testStrUtil.cpp b/libadhocutil/unittests/testStrUtil.cpp
new file mode 100644
index 0000000..fa642a7
--- /dev/null
+++ b/libadhocutil/unittests/testStrUtil.cpp
@@ -0,0 +1,45 @@
+#include <strUtil.h>
+
+constexpr auto
+ltrim(std::string_view s, const auto c)
+{
+ return AdHoc::ltrim(s, c);
+}
+constexpr auto
+rtrim(std::string_view s, const auto c)
+{
+ return AdHoc::rtrim(s, c);
+}
+constexpr auto
+trim(std::string_view s, const auto c)
+{
+ return AdHoc::trim(s, c);
+}
+
+static_assert(ltrim("foobar", 'r') == "foobar");
+static_assert(rtrim("foobar", 'r') == "fooba");
+static_assert(ltrim("foobar", 'f') == "oobar");
+static_assert(rtrim("foobar", 'f') == "foobar");
+
+static_assert(ltrim("foobar", "r") == "foobar");
+static_assert(rtrim("foobar", "r") == "fooba");
+static_assert(ltrim("foobar", "f") == "oobar");
+static_assert(rtrim("foobar", "f") == "foobar");
+
+static_assert(ltrim("foobar", "fo") == "bar");
+static_assert(ltrim("foobar", "of") == "bar");
+static_assert(rtrim("foobar", "fo") == "foobar");
+static_assert(rtrim("foobar", "of") == "foobar");
+
+static_assert(ltrim("foobar", "ar") == "foobar");
+static_assert(ltrim("foobar", "ra") == "foobar");
+static_assert(rtrim("foobar", "ar") == "foob");
+static_assert(rtrim("foobar", "ra") == "foob");
+
+static_assert(trim("roobar", 'r') == "ooba");
+static_assert(trim("foobar", 'r') == "fooba");
+static_assert(trim("foobar", 'f') == "oobar");
+static_assert(trim("foobar", "f") == "oobar");
+static_assert(trim("foobar", "r") == "fooba");
+static_assert(trim("foobar", "fr") == "ooba");
+static_assert(trim("foobar", "ofr") == "ba");