1#ifndef NEFORCE_CORE_STRING_BASIC_STRING_VIEW_HPP__
2#define NEFORCE_CORE_STRING_BASIC_STRING_VIEW_HPP__
17NEFORCE_BEGIN_NAMESPACE__
26template <
typename CharT,
typename Traits =
char_traits<CharT>>
27class basic_string_view;
39template <
typename Traits>
40struct basic_string_view_iterator :
iiterator<basic_string_view_iterator<Traits>> {
56 constexpr basic_string_view_iterator() noexcept = default;
57 NEFORCE_CONSTEXPR20 ~basic_string_view_iterator() = default;
59 constexpr basic_string_view_iterator(const basic_string_view_iterator&) noexcept = default;
60 constexpr basic_string_view_iterator& operator=(const basic_string_view_iterator&) noexcept = default;
61 constexpr basic_string_view_iterator(basic_string_view_iterator&&) noexcept = default;
62 constexpr basic_string_view_iterator& operator=(basic_string_view_iterator&&) noexcept = default;
70 constexpr basic_string_view_iterator(const
pointer data, const
size_t size, const
size_t off) noexcept :
80 NEFORCE_DEBUG_VERIFY(data_,
"Attempting to dereference on a null pointer");
81 NEFORCE_DEBUG_VERIFY(idx_ < size_,
"Attempting to dereference out of boundary");
89 NEFORCE_DEBUG_VERIFY(data_,
"Attempting to increment a null pointer");
90 NEFORCE_DEBUG_VERIFY(idx_ < size_,
"Attempting to increment out of boundary");
98 NEFORCE_DEBUG_VERIFY(data_,
"Attempting to decrement a null pointer");
99 NEFORCE_DEBUG_VERIFY(idx_ != 0,
"Attempting to decrement out of boundary");
108 NEFORCE_DEBUG_VERIFY(data_ || off == 0,
"Attempting to advance a null pointer");
109 NEFORCE_DEBUG_VERIFY((off < 0 ? idx_ >= -off : size_ - idx_ >= off),
"Attempting to advance out of boundary");
119 NEFORCE_DEBUG_VERIFY(data_ == other.data_ && size_ == other.size_,
120 "Attempting to distance to a different container");
136 NEFORCE_NODISCARD
constexpr bool equal_to(
const basic_string_view_iterator& rhs)
const noexcept {
137 NEFORCE_DEBUG_VERIFY(data_ == rhs.data_ && size_ == rhs.size_,
"Attempting to equal to a different container");
138 return idx_ == rhs.idx_;
146 NEFORCE_NODISCARD
constexpr bool less_than(
const basic_string_view_iterator& rhs)
const noexcept {
147 NEFORCE_DEBUG_VERIFY(data_ == rhs.data_ && size_ == rhs.size_,
"Attempting to less than a different container");
148 return idx_ < rhs.idx_;
155 NEFORCE_NODISCARD
constexpr pointer base() const noexcept {
return data_ + idx_; }
169template <
typename CharT,
typename Traits>
170class basic_string_view :
public icommon<basic_string_view<CharT, Traits>> {
172 "char type of basic string view should be same with char traits.");
174 "basic string view only contains non-array trivial standard-layout types.");
203 NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE
constexpr size_type clamp_size(
const size_type position,
205 return _NEFORCE
min(
size, size_ - position);
223 size_(Traits::
length(str)) {}
244 template <
typename Iterator, enable_if_t<is_same_v<iter_value_t<Iterator>, value_type>,
int> = 0>
247 size_(_NEFORCE
distance(start, finish)) {}
306 NEFORCE_NODISCARD
constexpr size_type size() const noexcept {
return size_; }
326 NEFORCE_NODISCARD
constexpr bool empty() const noexcept {
return size_ == 0; }
342 NEFORCE_DEBUG_VERIFY(!
empty(),
"cannot call front on empty string_view");
351 NEFORCE_DEBUG_VERIFY(!
empty(),
"cannot call back on empty string_view");
352 return data_[size_ - 1];
361 NEFORCE_DEBUG_VERIFY(n <= size_,
"basic string view index out of ranges.");
371 NEFORCE_DEBUG_VERIFY(n <= size_,
"basic string view index out of ranges.");
382 NEFORCE_DEBUG_VERIFY(size_ >= n,
"cannot remove prefix longer than total size");
394 NEFORCE_DEBUG_VERIFY(size_ >= n,
"cannot remove suffix longer than total size");
406 NEFORCE_DEBUG_VERIFY(off <= size_,
"basic string view index out of ranges.");
408 Traits::copy(str, data_ + off,
count);
419 NEFORCE_DEBUG_VERIFY(off <= size_,
"basic string view index out of ranges.");
421 return basic_string_view(data_ + off,
clamp);
453 NEFORCE_NODISCARD
constexpr int compare(
const basic_string_view
view)
const noexcept {
465 const basic_string_view
view)
const {
488 NEFORCE_NODISCARD
constexpr int compare(
const CharT*
const str)
const noexcept {
489 return compare(basic_string_view(str));
500 return substr(off, n).compare(basic_string_view(str));
513 return substr(off, n).compare(basic_string_view(str,
count));
523 for (
size_type i = 0; i < min_len; ++i) {
527 return (lc < rc) ? -1 : 1;
530 if (size_ <
view.size_) {
533 if (size_ >
view.size_) {
640 const size_type off = 0) const noexcept {
673 const size_type off = 0) const noexcept {
728 const size_type off = 0) const noexcept {
761 const size_type off = 0) const noexcept {
862 for (
size_type idx = position; idx < size_; ++idx) {
863 if (*(
data() + idx) == chr) {
875 NEFORCE_NODISCARD
constexpr bool starts_with(
const basic_string_view
view)
const noexcept {
876 return view.size() <= size_ && traits_type::compare(
data(),
view.data(),
view.size()) == 0;
885 return !
empty() && traits_type::eq(
front(), chr);
902 NEFORCE_NODISCARD
constexpr bool ends_with(
const basic_string_view
view)
const noexcept {
904 return view_size <= size_ && traits_type::compare(data_ + size_ - view_size,
view.data(), view_size) == 0;
913 return !
empty() && traits_type::eq(
back(), chr);
922 return this->
ends_with(basic_string_view(str));
930 NEFORCE_NODISCARD
constexpr bool contains(
const basic_string_view
view)
const noexcept {
952 NEFORCE_NODISCARD
constexpr basic_string_view
trim_left() const noexcept {
960 NEFORCE_NODISCARD
constexpr basic_string_view
trim_right() const noexcept {
968 NEFORCE_NODISCARD
constexpr basic_string_view
trim() const noexcept {
return this->
trim_left().trim_right(); }
976 template <
typename Predicate>
977 NEFORCE_NODISCARD
constexpr basic_string_view
trim_left_if(Predicate pred)
const
978 noexcept(
noexcept(pred(*
cbegin()))) {
984 while (it !=
cend() && pred(*it)) {
989 return basic_string_view(data_ + (it -
cbegin()), size_ - (it -
cbegin()));
1001 template <
typename Predicate>
1002 NEFORCE_NODISCARD
constexpr basic_string_view
trim_right_if(Predicate pred)
const
1003 noexcept(
noexcept(pred(*
crbegin()))) {
1009 while (rit !=
crend() && pred(*rit)) {
1014 return basic_string_view(data_, size_ - (rit -
crbegin()));
1031 while (it !=
cend() && cs.contains(*it)) {
1036 return basic_string_view(data_ + (it -
cbegin()), size_ - (it -
cbegin()));
1053 while (rit !=
crend() && cs.contains(*rit)) {
1058 return basic_string_view(data_, size_ - (rit -
crbegin()));
1069 NEFORCE_NODISCARD
constexpr basic_string_view
trim_if(
const charset& cs)
const noexcept {
1079 template <
typename Predicate>
1080 NEFORCE_NODISCARD
constexpr basic_string_view
trim_if(Predicate pred)
const
1090 NEFORCE_NODISCARD
constexpr bool equal_to(
const basic_string_view str)
const noexcept {
1099 NEFORCE_NODISCARD
constexpr bool equal_to(
const CharT* str)
const noexcept {
1100 return equal_to(basic_string_view(str));
1107 constexpr void swap(basic_string_view&
view)
noexcept {
1108 const basic_string_view tmp(
view);
1118 NEFORCE_NODISCARD
constexpr bool less_than(
const basic_string_view& rhs)
const noexcept {
1119 return this->
compare(rhs) < 0;
1126 NEFORCE_NODISCARD
constexpr size_t to_hash() const noexcept {
1127 const size_t byte_len =
length() *
sizeof(CharT);
1128#ifdef NEFORCE_ARCH_BITS_64
1129 return static_cast<size_t>(_NEFORCE
XXH64(
data(), byte_len));
1131 return static_cast<size_t>(_NEFORCE
XXH32(
data(), byte_len));
1136#ifndef NEFORCE_STANDARD_17
1137template <
typename CharT,
typename Traits>
1138constexpr size_t basic_string_view<CharT, Traits>::npos;
1141#ifndef NEFORCE_COMPILER_CLANG_CL
1142extern template class basic_string_view<char>;
1143extern template class basic_string_view<wchar_t>;
1144# ifdef NEFORCE_STANDARD_20
1145extern template class basic_string_view<char8_t>;
1147extern template class basic_string_view<char16_t>;
1148extern template class basic_string_view<char32_t>;
1153NEFORCE_END_NAMESPACE__
constexpr size_type find_first_not_of(const charset &cs, const size_type off=0) const noexcept
查找第一个不在 charset 中的字符
constexpr bool ends_with(value_type chr) const noexcept
检查是否以指定字符结尾
const typename Traits::char_type & reference
constexpr basic_string_view tail(const size_type off=0) const
获取尾部子串
constexpr const_reference at(const size_type n) const
带边界检查的访问
const typename Traits::char_type & const_reference
constexpr size_type find_last_not_of(const basic_string_view view, const size_type off=npos) const noexcept
查找最后一个不在字符集合中的字符
constexpr basic_string_view trim_right_if(const charset &cs) const noexcept
根据 charset 去除右侧字符
const typename Traits::char_type * const_pointer
constexpr const_iterator cend() const noexcept
获取常量结束迭代器
constexpr size_type find_first_not_of(const CharT *const str, const size_type off=0) const noexcept
查找第一个不在C风格字符串集合中的字符
constexpr const_reference operator[](const size_type n) const noexcept
下标访问操作符
constexpr bool starts_with(const_pointer str) const noexcept
检查是否以C风格字符串开头
constexpr bool ends_with(const basic_string_view view) const noexcept
检查是否以指定视图结尾
constexpr int compare(const basic_string_view view) const noexcept
比较字符串视图
constexpr const_iterator cbegin() const noexcept
获取常量起始迭代器
constexpr size_t to_hash() const noexcept
计算哈希值
constexpr basic_string_view trim_left() const noexcept
去除左侧空白字符
constexpr int compare(const size_type off, const size_type n, const CharT *const str, const size_type count) const
比较子串与指定长度的字符数组
constexpr basic_string_view trim_right() const noexcept
去除右侧空白字符
constexpr bool starts_with(value_type chr) const noexcept
检查是否以指定字符开头
constexpr int compare(const CharT *const str) const noexcept
比较与C风格字符串
constexpr basic_string_view(Iterator start, Iterator finish)
从迭代器范围构造
constexpr size_type count(value_type chr, const size_type position=0) const noexcept
constexpr bool equal_to(const CharT *str) const noexcept
与C风格字符串相等比较
constexpr const_reverse_iterator rend() const noexcept
获取反向结束迭代器
constexpr ~basic_string_view() noexcept=default
析构函数
constexpr const_reverse_iterator crend() const noexcept
获取常量反向结束迭代器
constexpr bool starts_with(const basic_string_view view) const noexcept
检查是否以指定视图开头
constexpr size_type find_first_of(const CharT *const str, const size_type off, const size_type n) const noexcept
查找第一个出现在指定字符集合中的字符
constexpr basic_string_view trim_left_if(const charset &cs) const noexcept
根据 charset 去除左侧字符
constexpr size_type rfind(const basic_string_view view, const size_type off=npos) const noexcept
从后向前查找子串
constexpr size_type find_first_of(const charset &cs, const size_type off=0) const noexcept
查找第一个出现在 charset 中的字符
constexpr bool less_than(const basic_string_view &rhs) const noexcept
小于比较操作符
constexpr size_type size() const noexcept
constexpr bool contains(const basic_string_view view) const noexcept
检查是否包含指定视图
constexpr int compare(const size_type off, const size_type n, const CharT *const str) const
比较子串与C风格字符串
constexpr size_type find(const basic_string_view view, const size_type n=0) const noexcept
查找子串
constexpr size_type find_last_not_of(const CharT *const str, const size_type off, const size_type n) const noexcept
查找最后一个不在指定字符集合中的字符
constexpr const_reverse_iterator crbegin() const noexcept
获取常量反向起始迭代器
constexpr size_type find(const CharT chr, const size_type n=0) const noexcept
查找字符
constexpr bool ends_with(const_pointer str) const noexcept
检查是否以C风格字符串结尾
constexpr size_type rfind(const CharT *const str, const size_type off=npos) const noexcept
从后向前查找C风格字符串
constexpr size_type length() const noexcept
constexpr size_type find_first_of(const CharT *const str, const size_type off=0) const noexcept
查找第一个出现在C风格字符串集合中的字符
const_reverse_iterator reverse_iterator
_NEFORCE reverse_iterator< const_iterator > const_reverse_iterator
constexpr const_reference back() const noexcept
访问最后一个字符
constexpr bool empty() const noexcept
检查是否为空
constexpr size_type max_size() const noexcept
获取最大可能长度
constexpr basic_string_view trim() const noexcept
去除两侧空白字符
constexpr size_type find(const CharT *const str, const size_type off, const size_type count) const noexcept
查找指定长度的子串
static constexpr auto npos
constexpr basic_string_view trim_if(const charset &cs) const noexcept
根据 charset 去除两侧字符
constexpr bool contains(value_type chr) const noexcept
检查是否包含指定字符
constexpr size_type find_first_not_of(const CharT *const str, const size_type off, const size_type n) const noexcept
查找第一个不在指定字符集合中的字符
constexpr size_type find_last_of(const basic_string_view view, const size_type off=npos) const noexcept
查找最后一个出现在字符集合中的字符
typename Traits::char_type value_type
constexpr int compare(const size_type off, const size_type n, const basic_string_view view, const size_type roff, const size_type count) const
比较子串与另一个子串
ptrdiff_t difference_type
constexpr size_type copy(CharT *const str, size_type count, const size_type off=0) const
复制字符到目标缓冲区
constexpr int compare_ignore_case(const basic_string_view view) const noexcept
忽略大小写三路比较
constexpr bool equal_to(const basic_string_view str) const noexcept
相等比较
constexpr size_type find_last_not_of(const CharT chr, const size_type off=npos) const noexcept
查找最后一个不等于指定字符的位置
constexpr int compare(const size_type off, const size_type n, const basic_string_view view) const
比较子串与字符串视图
constexpr size_type rfind(const CharT *const str, const size_type off, const size_type n) const noexcept
从后向前查找指定长度的子串
constexpr basic_string_view trim_right_if(Predicate pred) const noexcept(noexcept(pred(*crbegin())))
constexpr void remove_suffix(const size_type n) noexcept
移除后缀
constexpr basic_string_view head(const size_type count=npos) const
获取头部子串
constexpr const_reverse_iterator rbegin() const noexcept
获取反向起始迭代器
constexpr basic_string_view(const_pointer str, const size_type n) noexcept
从字符数组构造(指定长度)
const typename Traits::char_type * pointer
constexpr size_type find(const CharT *const str, const size_type off=0) const noexcept
查找C风格字符串
constexpr basic_string_view substr(const size_type off=0, const size_type count=npos) const
获取子视图
constexpr size_type find_first_of(const basic_string_view view, const size_type off=0) const noexcept
查找第一个出现在字符集合中的字符
constexpr size_type find_last_not_of(const CharT *const str, const size_type off=npos) const noexcept
查找最后一个不在C风格字符串集合中的字符
constexpr size_type find_last_not_of(const charset &cs, const size_type off=npos) const noexcept
查找最后一个不在 charset 中的字符
constexpr const_iterator end() const noexcept
获取结束迭代器
constexpr void remove_prefix(const size_type n) noexcept
移除前缀
constexpr size_type find_first_not_of(const CharT chr, const size_type off=0) const noexcept
查找第一个不等于指定字符的位置
constexpr const_pointer data() const noexcept
获取底层数据指针
basic_string_view_iterator< Traits > const_iterator
constexpr basic_string_view trim_if(Predicate pred) const noexcept(noexcept(this->trim_right_if(pred)) &&noexcept(this->trim_left_if(pred)))
根据谓词去除两侧字符
constexpr size_type find_first_not_of(const basic_string_view view, const size_type off=0) const noexcept
查找第一个不在字符集合中的字符
constexpr basic_string_view view(const size_type off, const size_type count=npos) const
获取子视图
constexpr size_type find_first_of(const CharT chr, const size_type off=0) const noexcept
查找第一个等于指定字符的位置
constexpr const_iterator begin() const noexcept
constexpr const_reference front() const noexcept
访问第一个字符
constexpr size_type find_last_of(const charset &cs, const size_type off=npos) const noexcept
查找最后一个出现在 charset 中的字符
constexpr bool contains(const_pointer str) const noexcept
检查是否包含C风格字符串
constexpr size_type find_last_of(const CharT chr, const size_type off=npos) const noexcept
查找最后一个等于指定字符的位置
constexpr size_type find_last_of(const CharT *const str, const size_type off, const size_type n) const noexcept
查找最后一个出现在指定字符集合中的字符
constexpr size_type find_last_of(const CharT *const str, const size_type off=npos) const noexcept
查找最后一个出现在C风格字符串集合中的字符
constexpr size_type rfind(const CharT chr, const size_type n=npos) const noexcept
从后向前查找字符
constexpr basic_string_view trim_left_if(Predicate pred) const noexcept(noexcept(pred(*cbegin())))
constexpr int compare_ignore_case(const_pointer str) const noexcept
忽略大小写与C风格字符串三路比较
constexpr void swap(basic_string_view &view) noexcept
交换两个字符串视图
static constexpr charset ascii_space() noexcept
ASCII 空白字符集
constexpr bool is_standard_layout_v
is_standard_layout的便捷变量模板
constexpr bool is_array_v
is_array的便捷变量模板
constexpr CharT to_lowercase(const CharT c) noexcept
将字符转换为小写
constexpr size_t char_traits_rfind(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_ptr_t< Traits > rsc, const size_t rsc_size) noexcept
从后向前查找子序列
constexpr size_t char_traits_find_not_char(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_char_t< Traits > chr) noexcept
查找第一个不等于指定字符的位置
constexpr int char_traits_compare(const char_traits_ptr_t< Traits > lhs, const size_t lh_size, const char_traits_ptr_t< Traits > rhs, const size_t rh_size) noexcept
比较两个字符序列(三路比较)
constexpr size_t char_traits_find_char(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_char_t< Traits > chr) noexcept
在字符序列中查找单个字符
constexpr size_t char_traits_find_first_not_of(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_ptr_t< Traits > rsc, const size_t rsc_size) noexcept
查找第一个不在给定集合中的字符(char_traits特化版本)
constexpr size_t char_traits_find(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_ptr_t< Traits > rsc, const size_t rsc_size) noexcept
在字符序列中查找子序列
constexpr size_t char_traits_find_last_not_of(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_ptr_t< Traits > rsc, const size_t rsc_size) noexcept
查找最后一个不在给定集合中的字符(char_traits特化版本)
constexpr bool char_traits_equal(const char_traits_ptr_t< Traits > lhs, const size_t lh_size, const char_traits_ptr_t< Traits > rhs, const size_t rh_size) noexcept
比较两个字符序列是否相等
constexpr size_t char_traits_find_first_of(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_ptr_t< Traits > rsc, const size_t rsc_size) noexcept
查找第一个出现在给定集合中的字符(char_traits特化版本)
constexpr size_t char_traits_find_last_of(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_ptr_t< Traits > rsc, const size_t rsc_size) noexcept
查找最后一个出现在给定集合中的字符(char_traits特化版本)
constexpr size_t char_traits_rfind_not_char(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_char_t< Traits > chr) noexcept
查找最后一个不等于指定字符的位置
constexpr size_t char_traits_rfind_char(const char_traits_ptr_t< Traits > dest, const size_t dest_size, const size_t start, const char_traits_char_t< Traits > chr) noexcept
从后向前查找单个字符
constexpr const T & min(const T &a, const T &b, Compare comp) noexcept(noexcept(comp(b, a)))
返回两个值中的较小者
constexpr T clamp(const T &value, const T &lower, const T &upper, Compare comp) noexcept(noexcept(comp(value, lower)))
将值限制在指定范围内
constexpr uint32_t XXH32(const void *input, size_t len, uint32_t seed=0) noexcept
XXH32 哈希算法
constexpr uint64_t XXH64(const void *input, size_t len, uint64_t seed=0) noexcept
XXH64 哈希算法
constexpr iter_difference_t< Iterator > distance(Iterator first, Iterator last)
计算两个迭代器之间的距离
constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
constexpr bool is_trivial_v
is_trivial的便捷变量模板
constexpr bool is_same_v
is_same的便捷变量模板
constexpr void increment() noexcept
递增操作
constexpr void advance(difference_type off) noexcept
前进操作
basic_string_view< typename Traits::char_type, Traits > container_type
容器类型
constexpr bool equal_to(const basic_string_view_iterator &rhs) const noexcept
相等比较
typename container_type::const_reference reference
引用类型
typename container_type::size_type size_type
大小类型
constexpr difference_type distance_to(const basic_string_view_iterator &other) const noexcept
计算距离操作
constexpr bool less_than(const basic_string_view_iterator &rhs) const noexcept
小于比较
typename container_type::value_type value_type
值类型
typename container_type::const_pointer pointer
指针类型
typename container_type::difference_type difference_type
差值类型
constexpr reference operator[](const difference_type n) const noexcept
下标访问操作符
constexpr void decrement() noexcept
递减操作
contiguous_iterator_tag iterator_category
迭代器类别
constexpr reference dereference() const noexcept
解引用操作
constexpr pointer base() const noexcept
获取底层指针