From 07d2bc6898e9af5c343a36113154f497a7f65695 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 30 Nov 2020 22:29:32 +0100 Subject: util/StringView: add method SplitLast() --- src/util/StringView.hxx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src') 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 + * Copyright 2013-2020 Max Kellermann * * 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 +#include #if __cplusplus >= 201703L && !GCC_OLDER_THAN(7,0) #include @@ -126,6 +127,20 @@ struct BasicStringView : ConstBuffer { 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> 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 needle) const noexcept { return this->size >= needle.size && -- cgit v1.2.3