util/IterableSplitString: use std::string_view internally

This commit is contained in:
Max Kellermann 2022-06-30 20:41:01 +02:00
parent 166885802a
commit 10197a0041

View File

@ -29,6 +29,7 @@
#pragma once #pragma once
#include "StringSplit.hxx"
#include "StringView.hxx" #include "StringView.hxx"
#include <iterator> #include <iterator>
@ -43,40 +44,41 @@
*/ */
template<typename T> template<typename T>
class BasicIterableSplitString { class BasicIterableSplitString {
typedef BasicStringView<T> StringView; using string_view = std::basic_string_view<T>;
using value_type = typename string_view::value_type;
using value_type = typename StringView::value_type; using StringView = BasicStringView<T>;
StringView s; string_view s;
value_type separator; value_type separator;
public: public:
constexpr BasicIterableSplitString(StringView _s, constexpr BasicIterableSplitString(string_view _s,
value_type _separator) noexcept value_type _separator) noexcept
:s(_s), separator(_separator) {} :s(_s), separator(_separator) {}
class Iterator final { class Iterator final {
friend class BasicIterableSplitString; friend class BasicIterableSplitString;
StringView current, rest; string_view current, rest;
value_type separator; value_type separator;
constexpr Iterator(StringView _s, constexpr Iterator(string_view _s,
value_type _separator) noexcept value_type _separator) noexcept
:rest(_s), separator(_separator) :rest(_s), separator(_separator)
{ {
Next(); Next();
} }
constexpr Iterator(std::nullptr_t n) noexcept constexpr Iterator(std::nullptr_t) noexcept
:current(n), rest(n), separator(0) {} :current(), rest(), separator() {}
constexpr void Next() noexcept { constexpr void Next() noexcept {
if (rest == nullptr) if (rest.data() == nullptr)
current = nullptr; current = {};
else { else {
const auto [a, b] = rest.Split(separator); const auto [a, b] = Split(rest, separator);
current = a; current = a;
rest = b; rest = b;
} }
@ -91,7 +93,7 @@ public:
} }
constexpr bool operator==(Iterator other) const noexcept { constexpr bool operator==(Iterator other) const noexcept {
return current.data == other.current.data; return current.data() == other.current.data();
} }
constexpr bool operator!=(Iterator other) const noexcept { constexpr bool operator!=(Iterator other) const noexcept {
@ -115,7 +117,7 @@ public:
} }
constexpr const_iterator end() const noexcept { constexpr const_iterator end() const noexcept {
return {nullptr}; return nullptr;
} }
}; };