1#ifndef NEFORCE_CORE_STRING_BASIC_STRING_VIEW_HPP__
2#define NEFORCE_CORE_STRING_BASIC_STRING_VIEW_HPP__
16NEFORCE_BEGIN_NAMESPACE__
25template <
typename CharT,
typename Traits =
char_traits<CharT>>
38template <
typename Traits>
39struct basic_string_view_iterator :
iiterator<basic_string_view_iterator<Traits>> {
55 constexpr basic_string_view_iterator() noexcept = default;
56 NEFORCE_CONSTEXPR20 ~basic_string_view_iterator() = default;
58 constexpr basic_string_view_iterator(const basic_string_view_iterator&) noexcept = default;
59 constexpr basic_string_view_iterator& operator=(const basic_string_view_iterator&) noexcept = default;
60 constexpr basic_string_view_iterator(basic_string_view_iterator&&) noexcept = default;
61 constexpr basic_string_view_iterator& operator=(basic_string_view_iterator&&) noexcept = default;
69 constexpr basic_string_view_iterator(const
pointer data, const
size_t size, const
size_t off) noexcept :
79 NEFORCE_DEBUG_VERIFY(data_,
"Attempting to dereference on a null pointer");
80 NEFORCE_DEBUG_VERIFY(idx_ < size_,
"Attempting to dereference out of boundary");
88 NEFORCE_DEBUG_VERIFY(data_,
"Attempting to increment a null pointer");
89 NEFORCE_DEBUG_VERIFY(idx_ < size_,
"Attempting to increment out of boundary");
97 NEFORCE_DEBUG_VERIFY(data_,
"Attempting to decrement a null pointer");
98 NEFORCE_DEBUG_VERIFY(idx_ != 0,
"Attempting to decrement out of boundary");
107 NEFORCE_DEBUG_VERIFY(data_ || off == 0,
"Attempting to advance a null pointer");
108 NEFORCE_DEBUG_VERIFY((off < 0 ? idx_ >= -off : size_ - idx_ >= off),
"Attempting to advance out of boundary");
118 NEFORCE_DEBUG_VERIFY(data_ == other.data_ && size_ == other.size_,
119 "Attempting to distance to a different container");
135 NEFORCE_NODISCARD
constexpr bool equal_to(
const basic_string_view_iterator& rhs)
const noexcept {
136 NEFORCE_DEBUG_VERIFY(data_ == rhs.data_ && size_ == rhs.size_,
"Attempting to equal to a different container");
137 return idx_ == rhs.idx_;
145 NEFORCE_NODISCARD
constexpr bool less_than(
const basic_string_view_iterator& rhs)
const noexcept {
146 NEFORCE_DEBUG_VERIFY(data_ == rhs.data_ && size_ == rhs.size_,
"Attempting to less than a different container");
147 return idx_ < rhs.idx_;
154 NEFORCE_NODISCARD
constexpr pointer base() const noexcept {
return data_ + idx_; }
168template <
typename CharT,
typename Traits>
169class basic_string_view :
public icommon<basic_string_view<CharT, Traits>> {
171 "char type of basic string view should be same with char traits.");
173 "basic string view only contains non-array trivial standard-layout types.");
202 NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE
constexpr size_type clamp_size(
const size_type position,
204 return _NEFORCE
min(
size, size_ - position);
220 size_(Traits::
length(str)) {}
241 template <
typename Iterator, enable_if_t<is_same_v<iter_value_t<Iterator>, value_type>,
int> = 0>
244 size_(_NEFORCE
distance(start, finish)) {}
303 NEFORCE_NODISCARD
constexpr size_type size() const noexcept {
return size_; }
323 NEFORCE_NODISCARD
constexpr bool empty() const noexcept {
return size_ == 0; }
338 NEFORCE_DEBUG_VERIFY(!
empty(),
"cannot call front on empty string_view");
347 NEFORCE_DEBUG_VERIFY(!
empty(),
"cannot call back on empty string_view");
348 return data_[size_ - 1];
357 NEFORCE_DEBUG_VERIFY(n <= size_,
"basic string view index out of ranges.");
367 NEFORCE_DEBUG_VERIFY(n <= size_,
"basic string view index out of ranges.");
378 NEFORCE_DEBUG_VERIFY(size_ >= n,
"cannot remove prefix longer than total size");
390 NEFORCE_DEBUG_VERIFY(size_ >= n,
"cannot remove suffix longer than total size");
402 NEFORCE_DEBUG_VERIFY(off <= size_,
"basic string view index out of ranges.");
404 Traits::copy(str, data_ + off,
count);
415 NEFORCE_DEBUG_VERIFY(off <= size_,
"basic string view index out of ranges.");
417 return basic_string_view(data_ + off,
clamp);
449 NEFORCE_NODISCARD
constexpr int compare(
const basic_string_view
view)
const noexcept {
461 const basic_string_view
view)
const {
484 NEFORCE_NODISCARD
constexpr int compare(
const CharT*
const str)
const noexcept {
485 return compare(basic_string_view(str));
519 for (
size_type i = 0; i < min_len; ++i) {
523 return (lc < rc) ? -1 : 1;
526 if (size_ <
view.size_) {
529 if (size_ >
view.size_) {
636 const size_type off = 0) const noexcept {
669 const size_type off = 0) const noexcept {
724 const size_type off = 0) const noexcept {
757 const size_type off = 0) const noexcept {
813 for (
size_type idx = position; idx < size_; ++idx) {
814 if (*(
data() + idx) == chr) {
826 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
bool starts_with(
const basic_string_view
view)
const noexcept {
827 return view.size() <= size_ && traits_type::compare(
data(),
view.data(),
view.size()) == 0;
836 return !
empty() && traits_type::eq(
front(), chr);
853 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
bool ends_with(
const basic_string_view
view)
const noexcept {
855 return view_size <= size_ && traits_type::compare(data_ + size_ - view_size,
view.data(), view_size) == 0;
864 return !
empty() && traits_type::eq(
back(), chr);
873 return this->
ends_with(basic_string_view(str));
881 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
bool contains(
const basic_string_view
view)
const noexcept {
907 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 basic_string_view
trim_left() const noexcept {
915 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 basic_string_view
trim_right() const noexcept {
923 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 basic_string_view
trim() const noexcept {
933 template <
typename Predicate>
934 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 basic_string_view
trim_left_if(Predicate pred)
const
935 noexcept(
noexcept(pred(*
cbegin()))) {
941 while (it !=
cend() && pred(*it)) {
946 return basic_string_view(data_ + (it -
cbegin()), size_ - (it -
cbegin()));
958 template <
typename Predicate>
959 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 basic_string_view
trim_right_if(Predicate pred)
const
960 noexcept(
noexcept(pred(*
crbegin()))) {
966 while (rit !=
crend() && pred(*rit)) {
971 return basic_string_view(data_, size_ - (rit -
crbegin()));
983 template <
typename Predicate>
984 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 basic_string_view
trim_if(Predicate pred)
const
994 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
bool equal_to(
const basic_string_view str)
const noexcept {
1003 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
bool equal_to(
const CharT* str)
const noexcept {
1004 return equal_to(basic_string_view(str));
1011 constexpr void swap(basic_string_view&
view)
noexcept {
1012 const basic_string_view tmp(
view);
1022 NEFORCE_NODISCARD
constexpr bool less_than(
const basic_string_view& rhs)
const noexcept {
1023 return this->
compare(rhs) < 0;
1030 NEFORCE_NODISCARD
constexpr size_t to_hash() const noexcept {
1035#ifndef NEFORCE_STANDARD_17
1036template <
typename CharT,
typename Traits>
1040#ifndef NEFORCE_COMPILER_CLANG_CL
1043# ifdef NEFORCE_STANDARD_20
1052NEFORCE_END_NAMESPACE__
constexpr bool less_than(const basic_string_view &rhs) const noexcept
小于比较操作符
constexpr size_type length() const noexcept
constexpr size_type rfind(const CharT *const str, const size_type off=npos) const noexcept
从后向前查找C风格字符串
constexpr int compare_ignore_case(const_pointer str) const noexcept
忽略大小写与C风格字符串三路比较
constexpr size_type find_last_of(const CharT *const str, const size_type off, const size_type n) const noexcept
查找最后一个出现在指定字符集合中的字符
constexpr size_type size() const noexcept
constexpr basic_string_view trim_right() const noexcept
去除右侧空白字符
constexpr basic_string_view view(const size_type off, const size_type count=npos) const
获取子视图
constexpr const_reference front() const noexcept
访问第一个字符
constexpr size_type find_last_of(const CharT *const str, const size_type off=npos) const noexcept
查找最后一个出现在C风格字符串集合中的字符
constexpr size_type find_last_not_of(const CharT *const str, const size_type off=npos) const noexcept
查找最后一个不在C风格字符串集合中的字符
constexpr basic_string_view trim_left() const noexcept
去除左侧空白字符
constexpr int compare(const size_type off, const size_type n, const CharT *const str) const
比较子串与C风格字符串
constexpr int compare(const size_type off, const size_type n, const basic_string_view view) const
比较子串与字符串视图
_NEFORCE reverse_iterator< const_iterator > const_reverse_iterator
constexpr size_t to_hash() const noexcept
计算哈希值
constexpr bool starts_with(const_pointer str) const noexcept
检查是否以C风格字符串开头
constexpr const_pointer data() const noexcept
获取底层数据指针
constexpr size_type find_first_not_of(const CharT *const str, const size_type off, const size_type n) const noexcept
查找第一个不在指定字符集合中的字符
constexpr int compare(const size_type off, const size_type n, const CharT *const str, const size_type count) const
比较子串与指定长度的字符数组
constexpr bool equal_to(const CharT *str) const noexcept
与C风格字符串相等比较
static constexpr auto npos
typename Traits::char_type value_type
constexpr size_type find_last_not_of(const basic_string_view view, const size_type off=npos) const noexcept
查找最后一个不在字符集合中的字符
const_reverse_iterator reverse_iterator
constexpr size_type count(value_type chr, const size_type position=0) const noexcept
constexpr size_type find_first_not_of(const CharT chr, const size_type off=0) const noexcept
查找第一个不等于指定字符的位置
constexpr bool contains(const basic_string_view view) const noexcept
检查是否包含指定视图
constexpr bool ends_with(value_type chr) const noexcept
检查是否以指定字符结尾
constexpr bool ends_with(const_pointer str) const noexcept
检查是否以C风格字符串结尾
constexpr basic_string_view tail(const size_type off=0) const
获取尾部子串
constexpr size_type find_last_not_of(const CharT chr, const size_type off=npos) const noexcept
查找最后一个不等于指定字符的位置
constexpr basic_string_view trim_right_if(Predicate pred) const noexcept(noexcept(pred(*crbegin())))
根据谓词去除右侧字符
constexpr size_type rfind(const CharT *const str, const size_type off, const size_type n) const noexcept
从后向前查找指定长度的子串
constexpr size_type max_size() const noexcept
获取最大可能长度
constexpr const_iterator cend() const noexcept
获取常量结束迭代器
constexpr bool empty() const noexcept
检查是否为空
constexpr const_iterator end() 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() const noexcept
去除两侧空白字符
constexpr basic_string_view substr(const size_type off=0, const size_type count=npos) const
获取子视图
constexpr const_reverse_iterator crbegin() const noexcept
获取常量反向起始迭代器
const typename Traits::char_type & const_reference
constexpr const_reverse_iterator crend() const noexcept
获取常量反向结束迭代器
constexpr size_type find(const CharT *const str, const size_type off=0) const noexcept
查找C风格字符串
const typename Traits::char_type * pointer
constexpr size_type copy(CharT *const str, size_type count, const size_type off=0) const
复制字符到目标缓冲区
constexpr int compare(const basic_string_view view) const noexcept
比较字符串视图
constexpr size_type find(const CharT chr, const size_type n=0) const noexcept
查找字符
constexpr int compare(const CharT *const str) const noexcept
比较与C风格字符串
constexpr const_iterator cbegin() const noexcept
获取常量起始迭代器
constexpr basic_string_view trim_if(Predicate pred) const noexcept(noexcept(this->trim_right_if(pred)) &&noexcept(this->trim_left_if(pred)))
根据谓词去除两侧字符
constexpr const_reference operator[](const size_type n) const noexcept
下标访问操作符
constexpr basic_string_view(const_pointer str, const size_type n) noexcept
从字符数组构造(指定长度)
constexpr const_reference back() const noexcept
访问最后一个字符
constexpr size_type find(const CharT *const str, const size_type off, const size_type count) const noexcept
查找指定长度的子串
constexpr size_type find(const basic_string_view view, const size_type n=0) const noexcept
查找子串
ptrdiff_t difference_type
constexpr size_type find_first_of(const CharT chr, const size_type off=0) const noexcept
查找第一个等于指定字符的位置
constexpr const_reference at(const size_type n) const
带边界检查的访问
constexpr void remove_suffix(const size_type n) noexcept
移除后缀
constexpr const_reverse_iterator rbegin() const noexcept
获取反向起始迭代器
const typename Traits::char_type & reference
constexpr bool equal_to(const basic_string_view str) const noexcept
相等比较
constexpr const_reverse_iterator rend() const noexcept
获取反向结束迭代器
constexpr basic_string_view(Iterator start, Iterator finish)
从迭代器范围构造
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
比较子串与另一个子串
constexpr size_type find_last_not_of(const CharT *const str, const size_type off, const size_type n) const noexcept
查找最后一个不在指定字符集合中的字符
constexpr size_type find_first_not_of(const CharT *const str, const size_type off=0) const noexcept
查找第一个不在C风格字符串集合中的字符
constexpr size_type find_last_of(const CharT chr, const size_type off=npos) const noexcept
查找最后一个等于指定字符的位置
basic_string_view_iterator< Traits > const_iterator
constexpr size_type find_first_not_of(const basic_string_view view, const size_type off=0) const noexcept
查找第一个不在字符集合中的字符
constexpr bool contains(const_pointer str) const noexcept
检查是否包含C风格字符串
constexpr size_type rfind(const basic_string_view view, const size_type off=npos) const noexcept
从后向前查找子串
constexpr bool ends_with(const basic_string_view view) const noexcept
检查是否以指定视图结尾
constexpr bool contains(value_type chr) const noexcept
检查是否包含指定字符
constexpr bool starts_with(value_type chr) const noexcept
检查是否以指定字符开头
constexpr void remove_prefix(const size_type n) noexcept
移除前缀
constexpr void swap(basic_string_view &view) noexcept
交换两个字符串视图
constexpr const_iterator begin() const noexcept
constexpr size_type find_first_of(const basic_string_view view, const size_type off=0) const noexcept
查找第一个出现在字符集合中的字符
constexpr size_type find_first_of(const CharT *const str, const size_type off=0) const noexcept
查找第一个出现在C风格字符串集合中的字符
constexpr size_type find_last_of(const basic_string_view view, const size_type off=npos) const noexcept
查找最后一个出现在字符集合中的字符
constexpr ~basic_string_view() noexcept=default
析构函数
constexpr basic_string_view head(const size_type count=npos) const
获取头部子串
constexpr basic_string_view trim_left_if(Predicate pred) const noexcept(noexcept(pred(*cbegin())))
根据谓词去除左侧字符
constexpr int compare_ignore_case(const basic_string_view view) const noexcept
忽略大小写三路比较
const typename Traits::char_type * const_pointer
constexpr bool starts_with(const basic_string_view view) const noexcept
检查是否以指定视图开头
constexpr size_type rfind(const CharT chr, const size_type n=npos) const noexcept
从后向前查找字符
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_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_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_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_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 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 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 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(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 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_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 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 bool is_space(const CharT c) noexcept
检查字符是否为空白字符
constexpr T clamp(const T &value, const T &lower, const T &upper, Compare comp) noexcept(noexcept(comp(value, lower)))
将值限制在指定范围内
constexpr const T & min(const T &a, const T &b, Compare comp) noexcept(noexcept(comp(b, a)))
返回两个值中的较小者
constexpr size_t FNV_hash_string(const CharT *str, const size_t len) noexcept
字符串类型的FNV哈希
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 reference dereference() const noexcept
解引用操作
constexpr void decrement() noexcept
递减操作
constexpr pointer base() const noexcept
获取底层指针
constexpr difference_type distance_to(const basic_string_view_iterator &other) const noexcept
计算距离操作
constexpr void increment() noexcept
递增操作
typename container_type::difference_type difference_type
差值类型
constexpr void advance(difference_type off) noexcept
前进操作
typename container_type::const_pointer pointer
指针类型
typename container_type::value_type value_type
值类型
basic_string_view< typename Traits::char_type, Traits > container_type
容器类型
contiguous_iterator_tag iterator_category
迭代器类别
typename container_type::const_reference reference
引用类型
typename container_type::size_type size_type
大小类型
constexpr reference operator[](const difference_type n) const noexcept
下标访问操作符
constexpr bool equal_to(const basic_string_view_iterator &rhs) const noexcept
相等比较
constexpr bool less_than(const basic_string_view_iterator &rhs) const noexcept
小于比较