5 #ifndef VCCC_STRING_VIEW_HPP_
6 #define VCCC_STRING_VIEW_HPP_
19 #if __cplusplus >= 201703L
20 #include <string_view>
23 #include "vccc/__config.h"
58 contiguous_iterator<It>,
59 sized_sentinel_for<End, It>,
60 has_typename_type<iter_value<It>>,
61 negation<std::is_convertible<It, SizeType>>
64 struct string_view_iter_ctor : std::is_same<iter_value_t<It>, CharT> {};
66 template<
typename It,
typename End,
typename CharT,
typename SizeType>
67 struct string_view_iter_ctor<It, End, CharT, SizeType, false> : std::false_type {};
69 template<
typename D,
typename SV,
typename =
void>
70 struct has_operator_string_view
71 #if __cplusplus < 202002L
72 : std::is_same<D, std::string> {};
76 template<
typename D,
typename SV>
77 struct has_operator_string_view<D, SV,
void_t<decltype(
std::declval<D&>().operator SV() )>> : std::true_type {};
83 negation< std::is_same<remove_cvref_t<R>, SV> >,
84 ranges::contiguous_range<R>,
85 ranges::sized_range<R>,
86 has_typename_type<ranges::range_value<R>>
89 struct string_view_range_ctor
91 std::is_same<ranges::range_value_t<R>, typename SV::value_type>,
92 negation< std::is_convertible<R, typename SV::const_pointer> >,
93 negation< has_operator_string_view<remove_cvref_t<R>, SV> >
95 template<
typename R,
typename SV>
96 struct string_view_range_ctor<R, SV, false> : std::false_type {};
119 typename Traits = std::char_traits<CharT>>
143 : data_(
nullptr), size_(0) {}
148 : data_(s), size_(
count) {}
153 template<
typename It,
typename End, std::enable_if_t<
160 : data_(ranges::
data(r)), size_(ranges::
size(r)) {}
168 : data_(s.
data()), size_(s.
size()) {}
171 : data_(s.
data()), size_(s.
size()) {}
173 #if __cplusplus < 201703L
175 explicit operator std::basic_string<CharT, Traits>()
const {
176 return std::basic_string<CharT, Traits>(
data(),
size());
179 operator std::basic_string_view<CharT, Traits>()
const {
180 return std::basic_string_view<CharT, Traits>(
data(),
size());
257 throw std::out_of_range(
"vccc::string_view::at : out of range");
300 constexpr
bool empty() const noexcept {
return size() == 0; }
342 throw std::out_of_range(
"vccc::string_view::copy : out of range");
355 throw std::out_of_range(
"vccc::string_view::substr : out of range");
367 int r = traits_type::compare(
data(), other.data(), (
std::min)(
size(), other.size()));
369 return size() < other.size() ? -1 :
370 size() > other.size() ? 1 :
407 return !
empty() && traits_type::eq(
front(), c);
425 return !
empty() && traits_type::eq(
back(), c);
459 if (traits_type::compare(
data() + pos, sv.
data(), sv.
size()) == 0)
486 while (pos <=
size() - sv.size()) {
487 if (traits_type::compare(
data() + pos, sv.data(), sv.size()) == 0)
513 while (pos <
size()) {
542 while (pos <
size()) {
570 while (pos <
size()) {
599 while (pos <
size()) {
629 template<
typename CharT,
typename Traits>
631 return (lhs.size() == rhs.size()) && (lhs.compare(rhs) == 0);
635 template<
typename CharT,
typename Traits,
int = 1>
636 constexpr
bool operator==(basic_string_view<CharT, Traits> lhs,
type_identity_t<basic_string_view<CharT, Traits>> rhs) noexcept {
637 return (lhs.size() == rhs.size()) && (lhs.compare(rhs) == 0);
640 template<
typename CharT,
typename Traits,
int = 2>
641 constexpr
bool operator==(
type_identity_t<basic_string_view<CharT, Traits>> lhs, basic_string_view<CharT, Traits> rhs) noexcept {
642 return (lhs.size() == rhs.size()) && (lhs.compare(rhs) == 0);
647 template<
typename CharT,
typename Traits>
649 return !(lhs == rhs);
653 template<
typename CharT,
typename Traits,
int = 1>
654 constexpr
bool operator!=(
type_identity_t<basic_string_view<CharT, Traits>> lhs, basic_string_view<CharT, Traits> rhs) noexcept {
655 return !(lhs == rhs);
658 template<
typename CharT,
typename Traits,
int = 2>
659 constexpr
bool operator!=(basic_string_view<CharT, Traits> lhs,
type_identity_t<basic_string_view<CharT, Traits>> rhs) noexcept {
660 return !(lhs == rhs);
665 template<
typename CharT,
typename Traits>
667 return lhs.compare(rhs) < 0;
671 template<
typename CharT,
typename Traits,
int = 1>
672 constexpr
bool operator<(
type_identity_t<basic_string_view<CharT, Traits>> lhs, basic_string_view<CharT, Traits> rhs) noexcept {
673 return lhs.compare(rhs) < 0;
676 template<
typename CharT,
typename Traits,
int = 2>
677 constexpr
bool operator<(basic_string_view<CharT, Traits> lhs,
type_identity_t<basic_string_view<CharT, Traits>> rhs) noexcept {
678 return lhs.compare(rhs) < 0;
683 template<
typename CharT,
typename Traits>
685 return lhs.compare(rhs) <= 0;
689 template<
typename CharT,
typename Traits,
int = 1>
690 constexpr
bool operator<=(
type_identity_t<basic_string_view<CharT, Traits>> lhs, basic_string_view<CharT, Traits> rhs) noexcept {
691 return lhs.compare(rhs) <= 0;
694 template<
typename CharT,
typename Traits,
int = 2>
695 constexpr
bool operator<=(basic_string_view<CharT, Traits> lhs,
type_identity_t<basic_string_view<CharT, Traits>> rhs) noexcept {
696 return lhs.compare(rhs) <= 0;
701 template<
typename CharT,
typename Traits>
703 return lhs.compare(rhs) > 0;
707 template<
typename CharT,
typename Traits,
int = 1>
708 constexpr
bool operator>(
type_identity_t<basic_string_view<CharT, Traits>> lhs, basic_string_view<CharT, Traits> rhs) noexcept {
709 return lhs.compare(rhs) > 0;
712 template<
typename CharT,
typename Traits,
int = 2>
713 constexpr
bool operator>(basic_string_view<CharT, Traits> lhs,
type_identity_t<basic_string_view<CharT, Traits>> rhs) noexcept {
714 return lhs.compare(rhs) > 0;
719 template<
typename CharT,
typename Traits>
721 return lhs.compare(rhs) >= 0;
725 template<
typename CharT,
typename Traits,
int = 1>
726 constexpr
bool operator>=(
type_identity_t<basic_string_view<CharT, Traits>> lhs, basic_string_view<CharT, Traits> rhs) noexcept {
727 return lhs.compare(rhs) >= 0;
730 template<
typename CharT,
typename Traits,
int = 2>
731 constexpr
bool operator>=(basic_string_view<CharT, Traits> lhs,
type_identity_t<basic_string_view<CharT, Traits>> rhs) noexcept {
732 return lhs.compare(rhs) >= 0;
736 template<
typename CharT,
typename Traits>
737 std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, basic_string_view<CharT, Traits> sv) {
738 using ostream_type = std::basic_ostream<CharT, Traits>;
739 using iostate_type =
typename ostream_type::iostate;
740 using sentry_type =
typename ostream_type::sentry;
741 using size_type = std::size_t;
743 iostate_type state = ostream_type::goodbit;
744 sentry_type sentry(os);
747 state |= ostream_type::badbit;
750 if (os.width() <= 0 ||
static_cast<size_type
>(os.width()) <= sv.size()) {
753 pad =
static_cast<size_type
>(os.width()) - sv.size();
758 if ((os.flags() & ostream_type::adjustfield) != ostream_type::left) {
759 for (; 0 < pad; --pad) {
760 if (Traits::eq_int_type(Traits::eof(), os.rdbuf()->sputc(os.fill()))) {
761 state |= ostream_type::badbit;
767 std::streamsize n = (
std::max)(os.width(),
static_cast<std::streamsize
>(sv.size()));
768 if (state == ostream_type::goodbit && os.rdbuf()->sputn(sv.data(), n) != n) {
769 state |= ostream_type::badbit;
773 if (state == ostream_type::goodbit) {
774 for (; 0 < pad; --pad) {
775 if (Traits::eq_int_type(Traits::eof(), os.rdbuf()->sputc(os.fill()))) {
776 state |= ostream_type::badbit;
784 if ((os.exceptions() & ostream_type::badbit) != 0) {
785 std::rethrow_exception(std::current_exception());
796 #if __cplusplus >= 202002L && VCCC_HAS_TYPE_CHAR8_T_CXX20
802 inline namespace literals {
803 inline namespace string_view_literals {
805 constexpr
string_view operator ""_sv(
const char* str, std::size_t len) noexcept {
809 #if __cplusplus >= 202002L && VCCC_HAS_TYPE_CHAR8_T_CXX20
810 constexpr u8string_view
operator ""_sv(
const char8_t* str, std::size_t len) noexcept {
811 return u8string_view{str, len};
815 constexpr
u16string_view operator ""_sv(
const char16_t* str, std::size_t len) noexcept {
819 constexpr
u32string_view operator ""_sv(
const char32_t* str, std::size_t len) noexcept {
823 constexpr
wstring_view operator ""_sv(
const wchar_t* str, std::size_t len) noexcept {
830 template<
typename CharT,
typename Traits>
834 template<
typename CharT,
typename Traits >
837 #if __cplusplus >= 201703L
838 template<
typename It,
typename End>
870 #if __cplusplus >= 202002L && VCCC_HAS_TYPE_CHAR8_T_CXX20
872 struct hash<
vccc::u8string_view> {
873 std::size_t operator()(
const vccc::u8string_view& sv)
const noexcept {
The templated class vccc::basic_string_view provides a lightweight object that offers read-only acces...
Definition: string_view.hpp:120
constexpr const_iterator begin() const noexcept
Definition: string_view.hpp:200
constexpr size_type find_first_of(const value_type *s, size_type pos=0) const
Definition: string_view.hpp:530
constexpr const_reference front() const
Definition: string_view.hpp:266
constexpr bool contains(basic_string_view sv) const noexcept
Definition: string_view.hpp:439
constexpr size_type find_last_not_of(basic_string_view sv, size_type pos=npos) const noexcept
Definition: string_view.hpp:597
const_reverse_iterator reverse_iterator
Definition: string_view.hpp:131
constexpr size_type find_first_not_of(basic_string_view sv, size_type pos=0) const noexcept
Definition: string_view.hpp:569
constexpr size_type find_last_of(basic_string_view sv, size_type pos=npos) const noexcept
Definition: string_view.hpp:540
constexpr size_type find_last_of(value_type c, size_type pos=npos) const noexcept
Definition: string_view.hpp:551
constexpr const_reference back() const
Definition: string_view.hpp:272
constexpr size_type find_first_not_of(const value_type *s, size_type pos=0) const
Definition: string_view.hpp:587
constexpr size_type find_first_not_of(value_type c, size_type pos=0) const noexcept
Definition: string_view.hpp:579
static constexpr size_type npos
Definition: string_view.hpp:135
constexpr const_iterator end() const noexcept
Definition: string_view.hpp:210
constexpr basic_string_view(const basic_string_view &other) noexcept=default
constexpr size_type rfind(basic_string_view sv, size_type pos=npos) const noexcept
Definition: string_view.hpp:484
constexpr bool contains(const value_type *str) const
Definition: string_view.hpp:447
constexpr bool starts_with(basic_string_view prefix) const noexcept
Definition: string_view.hpp:402
constexpr size_type rfind(const value_type *str, size_type pos=npos) const
Definition: string_view.hpp:502
constexpr size_type size() const noexcept
Definition: string_view.hpp:285
constexpr const_reverse_iterator rbegin() const noexcept
Definition: string_view.hpp:220
constexpr basic_string_view substr(size_type pos=0, size_type count=npos) const
Definition: string_view.hpp:353
constexpr const_reverse_iterator crend() const noexcept
Definition: string_view.hpp:232
constexpr void remove_prefix(size_type n)
Definition: string_view.hpp:306
constexpr size_type find_last_not_of(const value_type *s, size_type pos, size_type count) const
Definition: string_view.hpp:612
constexpr int compare(size_type pos1, size_type count1, const value_type *s, size_type count2) const
Definition: string_view.hpp:392
constexpr int compare(size_type pos1, size_type count1, basic_string_view other) const
Definition: string_view.hpp:376
const CharT & const_reference
Definition: string_view.hpp:127
constexpr bool starts_with(value_type c) const noexcept
Definition: string_view.hpp:406
constexpr size_type find_last_not_of(value_type c, size_type pos=npos) const noexcept
Definition: string_view.hpp:608
constexpr basic_string_view(const CharT *s)
Definition: string_view.hpp:150
const CharT * const_pointer
Definition: string_view.hpp:125
constexpr basic_string_view(std::basic_string< CharT, Traits > &&s)
Definition: string_view.hpp:170
constexpr size_type find_last_of(const value_type *s, size_type pos, size_type count) const
Definition: string_view.hpp:555
constexpr size_type find_first_of(const value_type *s, size_type pos, size_type count) const
Definition: string_view.hpp:526
constexpr int compare(size_type pos1, size_type count1, basic_string_view other, size_type pos2, size_type count2) const
Definition: string_view.hpp:380
constexpr basic_string_view(std::nullptr_t)=delete
Traits traits_type
Definition: string_view.hpp:122
constexpr size_type find(const value_type *str, size_type pos=0) const
Definition: string_view.hpp:474
constexpr size_type rfind(value_type c, size_type pos=npos) const noexcept
Definition: string_view.hpp:494
constexpr size_type copy(value_type *dest, size_type count, size_type pos=0) const
Definition: string_view.hpp:340
constexpr size_type find_first_not_of(const value_type *s, size_type pos, size_type count) const
Definition: string_view.hpp:583
const_pointer const_iterator
Definition: string_view.hpp:128
constexpr basic_string_view(R &&r)
Definition: string_view.hpp:159
constexpr basic_string_view(It first, End last)
Definition: string_view.hpp:155
constexpr const_iterator cbegin() const noexcept
Definition: string_view.hpp:202
constexpr bool ends_with(value_type c) const noexcept
Definition: string_view.hpp:424
constexpr basic_string_view(const CharT *s, size_type count)
Definition: string_view.hpp:147
constexpr bool ends_with(basic_string_view sv) const noexcept
Definition: string_view.hpp:420
constexpr const_reverse_iterator rend() const noexcept
Definition: string_view.hpp:230
constexpr const_reference operator[](size_type pos) const
Definition: string_view.hpp:247
constexpr const_iterator cend() const noexcept
Definition: string_view.hpp:212
constexpr int compare(const CharT *s) const
Definition: string_view.hpp:384
constexpr const_reverse_iterator crbegin() const noexcept
Definition: string_view.hpp:222
constexpr int compare(basic_string_view other) const noexcept
Definition: string_view.hpp:366
constexpr basic_string_view() noexcept
Definition: string_view.hpp:142
constexpr size_type max_size() const noexcept
Definition: string_view.hpp:294
constexpr bool contains(value_type c) const noexcept
Definition: string_view.hpp:443
constexpr basic_string_view & operator=(const basic_string_view &other) noexcept=default
constexpr int compare(size_type pos1, size_type count1, const value_type *s) const
Definition: string_view.hpp:388
const_iterator iterator
Definition: string_view.hpp:129
constexpr size_type find(const value_type *str, size_type pos, size_type count) const
Definition: string_view.hpp:470
constexpr size_type find(basic_string_view sv, size_type pos=0) const noexcept
Definition: string_view.hpp:457
constexpr size_type length() const noexcept
Definition: string_view.hpp:287
constexpr size_type find_first_of(value_type c, size_type pos=0) const noexcept
Definition: string_view.hpp:522
std::ptrdiff_t difference_type
Definition: string_view.hpp:133
constexpr size_type find_last_not_of(const value_type *s, size_type pos=npos) const
Definition: string_view.hpp:616
constexpr void remove_suffix(size_type n)
Definition: string_view.hpp:315
constexpr void swap(basic_string_view &other) noexcept
Definition: string_view.hpp:324
constexpr bool ends_with(const value_type *s) const
Definition: string_view.hpp:428
constexpr size_type find(value_type c, size_type pos=0) const noexcept
Definition: string_view.hpp:466
constexpr size_type find_last_of(const value_type *s, size_type pos=npos) const
Definition: string_view.hpp:559
constexpr bool starts_with(const value_type *s) const
Definition: string_view.hpp:410
constexpr basic_string_view(const std::basic_string< CharT, Traits > &s)
Definition: string_view.hpp:167
constexpr const_pointer data() const noexcept
Definition: string_view.hpp:278
constexpr size_type rfind(const value_type *str, size_type pos, size_type count) const
Definition: string_view.hpp:498
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_view.hpp:130
constexpr size_type find_first_of(basic_string_view sv, size_type pos=0) const noexcept
Definition: string_view.hpp:512
CharT & reference
Definition: string_view.hpp:126
constexpr bool empty() const noexcept
Definition: string_view.hpp:300
std::size_t size_type
Definition: string_view.hpp:132
CharT value_type
Definition: string_view.hpp:123
CharT * pointer
Definition: string_view.hpp:124
constexpr const_reference at(size_type pos) const
Definition: string_view.hpp:255
constexpr VCCC_INLINE_OR_STATIC detail::max_niebloid max
Definition: max.hpp:95
constexpr VCCC_INLINE_OR_STATIC detail::find_niebloid find
Definition: find.hpp:54
constexpr VCCC_INLINE_OR_STATIC detail::min_niebloid min
Definition: min.hpp:90
constexpr VCCC_INLINE_OR_STATIC detail::copy_niebloid copy
Definition: copy.hpp:69
constexpr VCCC_INLINE_OR_STATIC detail::count_niebloid count
Definition: count.hpp:58
std::size_t hash_array(const T *bytes, std::size_t size)
Definition: hash_array.hpp:91
constexpr T * to_address(T *p) noexcept
Definition: to_address.hpp:37
std::ostream & operator<<(std::ostream &os, const MatrixBase< E > &mat_expr)
Definition: matrix_ostream.hpp:17
std::enable_if_t< std::is_object< T >::value, T * > addressof(T &t) noexcept
Definition: addressof.hpp:33
constexpr bool operator>(const optional< T > &lhs, const optional< U > &rhs)
Definition: optional.h:522
constexpr bool operator<(const optional< T > &lhs, const optional< U > &rhs)
Definition: optional.h:504
constexpr bool operator<=(const optional< T > &lhs, const optional< U > &rhs)
Definition: optional.h:513
constexpr bool operator>=(const optional< T > &lhs, const optional< U > &rhs)
Definition: optional.h:531
basic_string_view< char > string_view
Definition: string_view.hpp:794
basic_string_view< wchar_t > wstring_view
Definition: string_view.hpp:795
basic_string_view< char16_t > u16string_view
Definition: string_view.hpp:799
basic_string_view< char32_t > u32string_view
Definition: string_view.hpp:800
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr bool operator!=(const MatrixBase< E1 > &lhs, const MatrixBase< E2 > &rhs)
Definition: mat_expr_operations.hpp:23
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 0 > first
Definition: key_value.hpp:34
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
constexpr bool operator==(const MatrixBase< E1 > &lhs, const MatrixBase< E2 > &rhs)
Definition: mat_expr_operations.hpp:15
std::size_t operator()(const vccc::string_view &sv) const noexcept
Definition: string_view.hpp:858
std::size_t operator()(const vccc::u16string_view &sv) const noexcept
Definition: string_view.hpp:881
std::size_t operator()(const vccc::u32string_view &sv) const noexcept
Definition: string_view.hpp:888
std::size_t operator()(const vccc::wstring_view &sv) const noexcept
Definition: string_view.hpp:865
Definition: enable_borrowed_range.hpp:17
The enable_view variable template is used to indicate whether a range is a view.
Definition: enable_view.hpp:36