From 166885802a6dcbc2db285c88af4aa253cf9b6688 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 30 Jun 2022 20:13:50 +0200 Subject: [PATCH] util/StringSplit: move code from StringView.hxx --- src/util/StringSplit.hxx | 78 ++++++++++++++++++++++++++++++++++++++++ src/util/StringView.hxx | 13 ++----- 2 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 src/util/StringSplit.hxx diff --git a/src/util/StringSplit.hxx b/src/util/StringSplit.hxx new file mode 100644 index 000000000..305e77914 --- /dev/null +++ b/src/util/StringSplit.hxx @@ -0,0 +1,78 @@ +/* + * Copyright 2013-2022 Max Kellermann + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +template +[[gnu::pure]] +std::pair, std::basic_string_view> +PartitionWithout(const std::basic_string_view haystack, + const typename std::basic_string_view::size_type separator) noexcept +{ + return { + haystack.substr(0, separator), + haystack.substr(separator + 1), + }; +} + +/** + * Split the string at the first 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. + */ +template +[[gnu::pure]] +std::pair, std::basic_string_view> +Split(const std::basic_string_view haystack, const T ch) noexcept +{ + const auto i = haystack.find(ch); + if (i == haystack.npos) + return {haystack, {}}; + + return PartitionWithout(haystack, i); +} + +/** + * 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. + */ +template +[[gnu::pure]] +std::pair, std::basic_string_view> +SplitLast(const std::basic_string_view haystack, const T ch) noexcept +{ + const auto i = haystack.rfind(ch); + if (i == haystack.npos) + return {haystack, {}}; + + return PartitionWithout(haystack, i); +} diff --git a/src/util/StringView.hxx b/src/util/StringView.hxx index a26fe9aee..c4d64c178 100644 --- a/src/util/StringView.hxx +++ b/src/util/StringView.hxx @@ -32,6 +32,7 @@ #include "ConstBuffer.hxx" #include "StringAPI.hxx" +#include "StringSplit.hxx" #include #include @@ -114,11 +115,7 @@ struct BasicStringView : ConstBuffer { */ [[gnu::pure]] std::pair, BasicStringView> Split(value_type ch) const noexcept { - const auto separator = Find(ch); - if (separator == nullptr) - return {*this, nullptr}; - - return {{begin(), separator}, {separator + 1, end()}}; + return ::Split(static_cast>(*this), ch); } /** @@ -128,11 +125,7 @@ struct BasicStringView : ConstBuffer { */ [[gnu::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()}}; + return ::SplitLast(static_cast>(*this), ch); } [[gnu::pure]]