summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/util/StringView.hxx17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/util/StringView.hxx b/src/util/StringView.hxx
index 990a6f27a..a56cd361e 100644
--- a/src/util/StringView.hxx
+++ b/src/util/StringView.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright 2013-2019 Max Kellermann <max.kellermann@gmail.com>
+ * Copyright 2013-2020 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -35,6 +35,7 @@
#include "Compiler.h"
#include <utility>
+#include <cstddef>
#if __cplusplus >= 201703L && !GCC_OLDER_THAN(7,0)
#include <string_view>
@@ -126,6 +127,20 @@ struct BasicStringView : ConstBuffer<T> {
return {{begin(), separator}, {separator + 1, end()}};
}
+ /**
+ * Split the string at the last occurrence of the given
+ * character. If the character is not found, then the first
+ * value is the whole string and the second value is nullptr.
+ */
+ gcc_pure
+ std::pair<BasicStringView<T>, BasicStringView<T>> SplitLast(value_type ch) const noexcept {
+ const auto separator = FindLast(ch);
+ if (separator == nullptr)
+ return {*this, nullptr};
+
+ return {{begin(), separator}, {separator + 1, end()}};
+ }
+
gcc_pure
bool StartsWith(BasicStringView<T> needle) const noexcept {
return this->size >= needle.size &&