From 5dbdd36263d732d89b3401962ed07643b7763d41 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 12 Sep 2017 17:03:40 +0200 Subject: [PATCH] util/StringView: add struct BasicStringView --- src/util/StringView.cxx | 8 +++-- src/util/StringView.hxx | 65 ++++++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/util/StringView.cxx b/src/util/StringView.cxx index 7e77e5459..b663291d8 100644 --- a/src/util/StringView.cxx +++ b/src/util/StringView.cxx @@ -30,16 +30,20 @@ #include "StringView.hxx" #include "CharUtil.hxx" +template void -StringView::StripLeft() noexcept +BasicStringView::StripLeft() noexcept { while (!IsEmpty() && IsWhitespaceOrNull(front())) pop_front(); } +template void -StringView::StripRight() noexcept +BasicStringView::StripRight() noexcept { while (!IsEmpty() && IsWhitespaceOrNull(back())) pop_back(); } + +template struct BasicStringView; diff --git a/src/util/StringView.hxx b/src/util/StringView.hxx index dce209b5e..a894569c6 100644 --- a/src/util/StringView.hxx +++ b/src/util/StringView.hxx @@ -33,50 +33,65 @@ #include "ConstBuffer.hxx" #include "StringAPI.hxx" -struct StringView : ConstBuffer { - StringView() = default; +template +struct BasicStringView : ConstBuffer { + typedef typename ConstBuffer::size_type size_type; + typedef typename ConstBuffer::value_type value_type; + typedef typename ConstBuffer::pointer_type pointer_type; - constexpr StringView(pointer_type _data, size_type _size) noexcept - :ConstBuffer(_data, _size) {} + using ConstBuffer::data; + using ConstBuffer::size; - constexpr StringView(pointer_type _begin, pointer_type _end) noexcept - :ConstBuffer(_begin, _end - _begin) {} + BasicStringView() = default; - StringView(pointer_type _data) noexcept - :ConstBuffer(_data, - _data != nullptr ? StringLength(_data) : 0) {} + constexpr BasicStringView(pointer_type _data, size_type _size) noexcept + :ConstBuffer(_data, _size) {} - constexpr StringView(std::nullptr_t n) noexcept - :ConstBuffer(n) {} + constexpr BasicStringView(pointer_type _begin, + pointer_type _end) noexcept + :ConstBuffer(_begin, _end - _begin) {} + + BasicStringView(pointer_type _data) noexcept + :ConstBuffer(_data, + _data != nullptr ? StringLength(_data) : 0) {} + + constexpr BasicStringView(std::nullptr_t n) noexcept + :ConstBuffer(n) {} + + using ConstBuffer::IsEmpty; + using ConstBuffer::front; + using ConstBuffer::back; + using ConstBuffer::pop_front; + using ConstBuffer::pop_back; gcc_pure pointer_type Find(value_type ch) const noexcept { - return StringFind(data, ch, size); + return StringFind(data, ch, this->size); } gcc_pure - bool StartsWith(StringView needle) const noexcept { - return size >= needle.size && + bool StartsWith(BasicStringView needle) const noexcept { + return this->size >= needle.size && StringIsEqual(data, needle.data, needle.size); } gcc_pure - bool EndsWith(StringView needle) const noexcept { - return size >= needle.size && - StringIsEqual(data + size - needle.size, + bool EndsWith(BasicStringView needle) const noexcept { + return this->size >= needle.size && + StringIsEqual(data + this->size - needle.size, needle.data, needle.size); } gcc_pure - bool Equals(StringView other) const noexcept { - return size == other.size && - StringIsEqual(data, other.data, size); + bool Equals(BasicStringView other) const noexcept { + return this->size == other.size && + StringIsEqual(data, other.data, this->size); } gcc_pure - bool EqualsIgnoreCase(StringView other) const noexcept { - return size == other.size && - StringIsEqualIgnoreCase(data, other.data, size); + bool EqualsIgnoreCase(BasicStringView other) const noexcept { + return this->size == other.size && + StringIsEqualIgnoreCase(data, other.data, this->size); } /** @@ -95,4 +110,8 @@ struct StringView : ConstBuffer { } }; +struct StringView : BasicStringView { + using BasicStringView::BasicStringView; +}; + #endif