From 01d2dc62670119c262607016ab60e61befaee567 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 17 Sep 2021 19:31:28 +0100 Subject: Add utils for trimming string_views ltrim,rtrim,trim --- libadhocutil/strUtil.h | 29 ++++++++++++++++++++++ libadhocutil/unittests/Jamfile.jam | 1 + libadhocutil/unittests/testStrUtil.cpp | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 libadhocutil/strUtil.h create mode 100644 libadhocutil/unittests/testStrUtil.cpp 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 + +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 boost_utf ; +obj testStrUtil : testStrUtil.cpp : ..//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 + +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"); -- cgit v1.2.3