diff options
author | Max Kellermann <max@musicpd.org> | 2020-11-30 22:29:32 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2021-01-21 13:20:01 +0100 |
commit | 07d2bc6898e9af5c343a36113154f497a7f65695 (patch) | |
tree | 633c9bf2ba90457032444c0e369c4c8f6df90dd4 | |
parent | 9551166f2795238cda1089dc7de2ff02ec1dcce4 (diff) |
util/StringView: add method SplitLast()
-rw-r--r-- | src/util/StringView.hxx | 17 |
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 && |