util/StringView: add struct BasicStringView

This commit is contained in:
Max Kellermann 2017-09-12 17:03:40 +02:00
parent 96b557c1f0
commit 5dbdd36263
2 changed files with 48 additions and 25 deletions

View File

@ -30,16 +30,20 @@
#include "StringView.hxx" #include "StringView.hxx"
#include "CharUtil.hxx" #include "CharUtil.hxx"
template<typename T>
void void
StringView::StripLeft() noexcept BasicStringView<T>::StripLeft() noexcept
{ {
while (!IsEmpty() && IsWhitespaceOrNull(front())) while (!IsEmpty() && IsWhitespaceOrNull(front()))
pop_front(); pop_front();
} }
template<typename T>
void void
StringView::StripRight() noexcept BasicStringView<T>::StripRight() noexcept
{ {
while (!IsEmpty() && IsWhitespaceOrNull(back())) while (!IsEmpty() && IsWhitespaceOrNull(back()))
pop_back(); pop_back();
} }
template struct BasicStringView<char>;

View File

@ -33,50 +33,65 @@
#include "ConstBuffer.hxx" #include "ConstBuffer.hxx"
#include "StringAPI.hxx" #include "StringAPI.hxx"
struct StringView : ConstBuffer<char> { template<typename T>
StringView() = default; struct BasicStringView : ConstBuffer<T> {
typedef typename ConstBuffer<T>::size_type size_type;
typedef typename ConstBuffer<T>::value_type value_type;
typedef typename ConstBuffer<T>::pointer_type pointer_type;
constexpr StringView(pointer_type _data, size_type _size) noexcept using ConstBuffer<T>::data;
:ConstBuffer(_data, _size) {} using ConstBuffer<T>::size;
constexpr StringView(pointer_type _begin, pointer_type _end) noexcept BasicStringView() = default;
:ConstBuffer(_begin, _end - _begin) {}
StringView(pointer_type _data) noexcept constexpr BasicStringView(pointer_type _data, size_type _size) noexcept
:ConstBuffer(_data, :ConstBuffer<T>(_data, _size) {}
_data != nullptr ? StringLength(_data) : 0) {}
constexpr StringView(std::nullptr_t n) noexcept constexpr BasicStringView(pointer_type _begin,
:ConstBuffer(n) {} pointer_type _end) noexcept
:ConstBuffer<T>(_begin, _end - _begin) {}
BasicStringView(pointer_type _data) noexcept
:ConstBuffer<T>(_data,
_data != nullptr ? StringLength(_data) : 0) {}
constexpr BasicStringView(std::nullptr_t n) noexcept
:ConstBuffer<T>(n) {}
using ConstBuffer<T>::IsEmpty;
using ConstBuffer<T>::front;
using ConstBuffer<T>::back;
using ConstBuffer<T>::pop_front;
using ConstBuffer<T>::pop_back;
gcc_pure gcc_pure
pointer_type Find(value_type ch) const noexcept { pointer_type Find(value_type ch) const noexcept {
return StringFind(data, ch, size); return StringFind(data, ch, this->size);
} }
gcc_pure gcc_pure
bool StartsWith(StringView needle) const noexcept { bool StartsWith(BasicStringView<T> needle) const noexcept {
return size >= needle.size && return this->size >= needle.size &&
StringIsEqual(data, needle.data, needle.size); StringIsEqual(data, needle.data, needle.size);
} }
gcc_pure gcc_pure
bool EndsWith(StringView needle) const noexcept { bool EndsWith(BasicStringView<T> needle) const noexcept {
return size >= needle.size && return this->size >= needle.size &&
StringIsEqual(data + size - needle.size, StringIsEqual(data + this->size - needle.size,
needle.data, needle.size); needle.data, needle.size);
} }
gcc_pure gcc_pure
bool Equals(StringView other) const noexcept { bool Equals(BasicStringView<T> other) const noexcept {
return size == other.size && return this->size == other.size &&
StringIsEqual(data, other.data, size); StringIsEqual(data, other.data, this->size);
} }
gcc_pure gcc_pure
bool EqualsIgnoreCase(StringView other) const noexcept { bool EqualsIgnoreCase(BasicStringView<T> other) const noexcept {
return size == other.size && return this->size == other.size &&
StringIsEqualIgnoreCase(data, other.data, size); StringIsEqualIgnoreCase(data, other.data, this->size);
} }
/** /**
@ -95,4 +110,8 @@ struct StringView : ConstBuffer<char> {
} }
}; };
struct StringView : BasicStringView<char> {
using BasicStringView::BasicStringView;
};
#endif #endif