diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-09-17 19:31:28 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-09-17 19:31:28 +0100 |
commit | 01d2dc62670119c262607016ab60e61befaee567 (patch) | |
tree | 7e6356217c5c7ff056730c54dbd26d39e8225d81 | |
parent | General tidy up (diff) | |
download | libadhocutil-01d2dc62670119c262607016ab60e61befaee567.tar.bz2 libadhocutil-01d2dc62670119c262607016ab60e61befaee567.tar.xz libadhocutil-01d2dc62670119c262607016ab60e61befaee567.zip |
Add utils for trimming string_views ltrim,rtrim,trimlibadhocutil-0.8.5
-rw-r--r-- | libadhocutil/strUtil.h | 29 | ||||
-rw-r--r-- | libadhocutil/unittests/Jamfile.jam | 1 | ||||
-rw-r--r-- | libadhocutil/unittests/testStrUtil.cpp | 45 |
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"); |