summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2021-09-19 15:11:43 +0200
committerStephan Bergmann <sbergman@redhat.com>2021-09-19 18:14:15 +0200
commite6fe048ded34a322007547d4d31e32c598aa4993 (patch)
tree4f494edb0100a1023fa752737f67ced855bcb8a5 /include
parent33a699c8f3605c8506cdde6918bfbb4b59ab79b2 (diff)
Some more string_view use, add o3tl::starts/ends_with
...until those C++20 string_view member functions are generally available (the fallback implementations are taken directly from the C++20 spec). (In ParseMathMLAttributeLengthValue in starmath/source/mathml/mathmlattr.cxx, returning nIdx + 2 instead of nIdx + 1 for the single-character u'%' case was presumably a typo, but which was harmless as the return value was only checked for <= 0, and has now been turned into a bool.) Change-Id: Ib441e474c515f016a4d81bb39f7821dfe0356499 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122322 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include')
-rw-r--r--include/o3tl/string_view.hxx61
1 files changed, 61 insertions, 0 deletions
diff --git a/include/o3tl/string_view.hxx b/include/o3tl/string_view.hxx
index 5ebbb0b9f044..b66ba1175a57 100644
--- a/include/o3tl/string_view.hxx
+++ b/include/o3tl/string_view.hxx
@@ -13,6 +13,7 @@
#include <cassert>
#include <cstddef>
+#include <string>
#include <string_view>
#include <rtl/ustring.h>
@@ -46,6 +47,66 @@ inline std::string_view getToken(std::string_view sv, char delimiter, std::size_
}
return t;
}
+
+// Implementations of C++20 std::basic_string_view::starts_with and
+// std::basic_string_view::ends_with, until we can use those directly on all platforms:
+template <typename charT, typename traits = std::char_traits<charT>>
+constexpr bool starts_with(std::basic_string_view<charT, traits> sv,
+ std::basic_string_view<charT, traits> x) noexcept
+{
+#if defined __cpp_lib_starts_ends_with
+ return sv.starts_with(x);
+#else
+ return sv.substr(0, x.size()) == x;
+#endif
+}
+template <typename charT, typename traits = std::char_traits<charT>>
+constexpr bool starts_with(std::basic_string_view<charT, traits> sv, charT x) noexcept
+{
+#if defined __cpp_lib_starts_ends_with
+ return sv.starts_with(x);
+#else
+ return !sv.empty() && traits::eq(sv.front(), x);
+#endif
+}
+template <typename charT, typename traits = std::char_traits<charT>>
+constexpr bool starts_with(std::basic_string_view<charT, traits> sv, charT const* x)
+{
+#if defined __cpp_lib_starts_ends_with
+ return sv.starts_with(x);
+#else
+ return starts_with(sv, std::basic_string_view<charT, traits>(x));
+#endif
+}
+template <typename charT, typename traits = std::char_traits<charT>>
+constexpr bool ends_with(std::basic_string_view<charT, traits> sv,
+ std::basic_string_view<charT, traits> x) noexcept
+{
+#if defined __cpp_lib_ends_ends_with
+ return sv.ends_with(x);
+#else
+ return sv.size() >= x.size()
+ && sv.compare(sv.size() - x.size(), std::basic_string_view<charT, traits>::npos, x) == 0;
+#endif
+}
+template <typename charT, typename traits = std::char_traits<charT>>
+constexpr bool ends_with(std::basic_string_view<charT, traits> sv, charT x) noexcept
+{
+#if defined __cpp_lib_ends_ends_with
+ return sv.ends_with(x);
+#else
+ return !sv.empty() && traits::eq(sv.back(), x);
+#endif
+}
+template <typename charT, typename traits = std::char_traits<charT>>
+constexpr bool ends_with(std::basic_string_view<charT, traits> sv, charT const* x)
+{
+#if defined __cpp_lib_ends_ends_with
+ return sv.ends_with(x);
+#else
+ return ends_with(sv, std::basic_string_view<charT, traits>(x));
+#endif
+}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */