5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_STRING_VIEW_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_STRING_VIEW_HPP_
20#include "arene/base/detail/raw_c_string.hpp"
25#include "arene/base/stdlib_choice/cstddef.hpp"
26#include "arene/base/stdlib_choice/enable_if.hpp"
27#include "arene/base/stdlib_choice/ignore.hpp"
28#include "arene/base/stdlib_choice/integral_constant.hpp"
29#include "arene/base/stdlib_choice/is_same.hpp"
30#include "arene/base/stdlib_choice/min_value_overload.hpp"
31#include "arene/base/stdlib_choice/numeric_limits.hpp"
32#include "arene/base/stdlib_choice/string.hpp"
33#include "arene/base/strings/detail/lexicographic_string_compare.hpp"
34#include "arene/base/strings/detail/string_length.hpp"
35#include "arene/base/strings/detail/string_view_ostream_mixin.hpp"
54 ,
private string_view_ostream_mixin<string_view> {
56 using character = detail::character;
58 using raw_c_string = detail::raw_c_string;
63 using integral_comparison_result = int;
118 constexpr
string_view(raw_c_string const str,
std::
size_t const str_length) noexcept
120 str_span_(str, str_length) {}
127 template <std::
size_t N>
129 inline constexpr string_view(character
const (&literal)[N]) noexcept
141 constexpr string_view(raw_c_string
const null_terminated_string) noexcept
142 :
string_view(null_terminated_string, detail::string_length(null_terminated_string)) {}
156 template <typename String, constraints<std::enable_if_t<std::is_same<String, std::string>::value>> =
nullptr>
220 constexpr auto size() const noexcept ->
size_type {
return str_span_.size(); }
231 constexpr auto empty() const noexcept ->
bool {
return size() == 0U; }
265 -> integral_comparison_result {
266 return substr(pos1, length1).compare(other);
285 )
const noexcept -> integral_comparison_result {
286 return substr(pos1, length1).compare(other.substr(pos2, length2));
295 constexpr auto compare(raw_c_string
const other)
const noexcept -> integral_comparison_result {
308 -> integral_comparison_result {
309 return substr(pos1, length1).compare(other);
325 raw_c_string
const other,
327 )
const noexcept -> integral_comparison_result {
387 return str_span_[pos];
396 return copy(dest, n, 0U);
407 auto const sub =
substr(pos, n);
420 return str_span_.front();
430 return str_span_.back();
460 return substr(0U, other.size()) == other;
470 constexpr auto starts_with(raw_c_string
const other)
const noexcept ->
bool {
487 return (other.size() <=
size()) &&
substr(
size() - other.size(), other.size()) == other;
503 constexpr auto ends_with(character
const chr)
const noexcept ->
bool {
return (!
empty()) && (
back() == chr); }
516 for (
size_type index{pos}; (
size() - index) >= str.size(); ++index) {
517 if (
substr(index, str.size()) == str) {
571 if (
substr(index, str.size()) == str) {
641 if (str.find(str_span_[index]) !=
npos) {
681 return find(chr, pos);
697 if (str.find(str_span_[index]) ==
npos) {
753 if (str.find(str_span_[index]) !=
npos) {
796 return rfind(chr, pos);
809 return pos <
size() ? pos :
size() - 1U;
813 if (str.find(str_span_[index]) ==
npos) {
875 return detail::lexicographic_string_compare(lhs.str_span_, rhs.str_span_);
902 const_char_span str_span_;
constexpr generic_ordering_from_three_way_compare() noexcept=default
An opt-in trait to indicate that a range type is a borrowed range.
Definition enable_borrowed_range.hpp:31
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
::arene::base::reverse_iterator< iterator > reverse_iterator
The type of an iterator into a reversed view of the data viewed by the span.
Definition span.hpp:392
detail::wrapped_iterator< const_pointer, span_detail::span_iter_passkey > const_iterator
The type of an iterator into a const view of the data viewed by the span.
Definition span.hpp:390
detail::wrapped_iterator< pointer, span_detail::span_iter_passkey > iterator
The type of an iterator into the data viewed by the span.
Definition span.hpp:388
T & reference
The type of references to elements of the span.
Definition span.hpp:384
T element_type
The type of the elements of the span.
Definition span.hpp:374
arene::base::remove_cvref_t< T > value_type
The type of the elements of the span without const or volatile qualification.
Definition span.hpp:376
::arene::base::reverse_iterator< const_iterator > const_reverse_iterator
The type of an iterator into a const and reversed view of the data viewed by the span.
Definition span.hpp:394
T * pointer
The type of pointers to elements of the span.
Definition span.hpp:380
T const & const_reference
The type of references to const elements of the span.
Definition span.hpp:386
T const * const_pointer
The type of pointers to const elements of the span.
Definition span.hpp:382
Backport of the C++17 std::string_view class.
Definition string_view.hpp:54
constexpr auto copy(character *const dest, size_type const n) const noexcept -> size_type
Copy the first min(n,size()) characters to the destination buffer.
Definition string_view.hpp:395
constexpr string_view() noexcept=default
Default constructor, creates an empty string_view.
constexpr auto operator=(string_view const &other) noexcept -> string_view &=default
Assignment operator for the string view.
constexpr auto crbegin() const noexcept -> const_reverse_iterator
Obtain a reverse iterator referring to the end of the string_view.
Definition string_view.hpp:208
constexpr auto rend() const noexcept -> reverse_iterator
Obtain a reverse iterator referring to one-before the front of the string_view.
Definition string_view.hpp:203
const_char_span::value_type value_type
Same that element_type but without any cv-qualifier.
Definition string_view.hpp:78
constexpr auto find_first_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence.
Definition string_view.hpp:670
constexpr void remove_suffix(size_type const count) noexcept
Remove the last n characters from the string.
Definition string_view.hpp:450
const_char_span::const_reverse_iterator const_reverse_iterator
Alias to the const-qualified reverse iterator.
Definition string_view.hpp:99
const_char_span::reference reference
Alias to the reference type associated to the underlying data.
Definition string_view.hpp:89
constexpr auto starts_with(character const chr) const noexcept -> bool
Check if the string starts with a specific character.
Definition string_view.hpp:479
constexpr auto find_last_not_of(raw_c_string const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:830
constexpr auto find_last_not_of(character const chr, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to the provided character.
Definition string_view.hpp:854
constexpr auto find(string_view const str, size_type const pos=0U) const noexcept -> size_type
Find the earliest position of the given substring within the string, starting at the specified positi...
Definition string_view.hpp:511
constexpr auto find_last_of(raw_c_string const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence.
Definition string_view.hpp:770
constexpr string_view(character const (&literal)[N]) noexcept
Constructor from compile time string literal or c-array.
Definition string_view.hpp:129
constexpr auto rfind(string_view const str, size_type const pos=npos) const noexcept -> size_type
Find the latest position of the given substring within the string, starting at the specified position...
Definition string_view.hpp:568
static constexpr auto three_way_compare(string_view const lhs, string_view const rhs) noexcept -> strong_ordering
Compare two string_view objects lexicographically.
Definition string_view.hpp:874
const_char_span::const_iterator const_iterator
Const qualified version of iterator.
Definition string_view.hpp:95
constexpr auto compare(size_type const pos1, size_type const length1, raw_c_string const other, size_type const length2) const noexcept -> integral_comparison_result
Compare a substring against a string specified as a pointer and length.
Definition string_view.hpp:322
constexpr auto find_last_not_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the last character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:843
constexpr auto find_first_not_of(character const chr, size_type const pos=0U) const noexcept -> size_type
Finds the first character not equal to the provided character.
Definition string_view.hpp:735
constexpr auto length() const noexcept -> size_type
Get the length of the string view.
Definition string_view.hpp:225
constexpr auto find_last_not_of(string_view const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:807
constexpr auto find(raw_c_string const str, size_type const pos=0U) const noexcept -> size_type
Find the earliest position of the given substring within the string, starting at the specified positi...
Definition string_view.hpp:532
constexpr void remove_prefix(size_type const count) noexcept
Remove the first n characters from the string.
Definition string_view.hpp:439
constexpr auto cbegin() const noexcept -> const_iterator
Begin const iterator.
Definition string_view.hpp:188
constexpr auto rfind(raw_c_string const str, size_type const pos=npos) const noexcept -> size_type
Find the latest position of the given substring within the string, starting at the specified position...
Definition string_view.hpp:587
static constexpr auto fast_inequality_check(string_view const first, string_view const second) noexcept -> inequality_heuristic
quick check that string views have the same size to quickly identify non-equal strings.
Definition string_view.hpp:893
const_char_span::const_pointer const_pointer
Const qualified version of pointer.
Definition string_view.hpp:87
constexpr auto compare(size_type const pos1, size_type const length1, string_view const other) const noexcept -> integral_comparison_result
Compare a substring against another string view.
Definition string_view.hpp:264
constexpr auto find_first_not_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:725
std::ptrdiff_t difference_type
Alias associated to the type used for indexing.
Definition string_view.hpp:83
constexpr auto find_last_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence.
Definition string_view.hpp:784
constexpr auto ends_with(character const chr) const noexcept -> bool
Check if the string ends with a specific character.
Definition string_view.hpp:503
static constexpr std::size_t npos
Special marker value for end of string.
Definition string_view.hpp:69
constexpr auto copy(character *const dest, size_type const n, size_type const pos) const noexcept -> size_type
Copy the first min(n,size()-pos) characters to the destination buffer.
Definition string_view.hpp:406
constexpr auto find_first_not_of(raw_c_string const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:713
const_char_span::element_type element_type
Alias to the type associated to a single contained character.
Definition string_view.hpp:75
constexpr string_view(string_view const &other) noexcept=default
Default copy constructor.
constexpr auto find_first_of(string_view const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence.
Definition string_view.hpp:635
constexpr auto rbegin() const noexcept -> reverse_iterator
Obtain a reverse iterator referring to the end of the string.
Definition string_view.hpp:198
constexpr auto ends_with(raw_c_string const other) const noexcept -> bool
Check if the string ends with another string.
Definition string_view.hpp:497
constexpr auto empty() const noexcept -> bool
Query if this is the empty string.
Definition string_view.hpp:231
const_char_span::const_reference const_reference
Const qualified version of reference.
Definition string_view.hpp:91
constexpr auto data() const noexcept -> const_pointer
Get a pointer to the first character in the string.
Definition string_view.hpp:366
constexpr auto find_first_of(character const chr, size_type const pos=0U) const noexcept -> size_type
Finds the first character equal to the specified character.
Definition string_view.hpp:680
constexpr auto find(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Find the earliest position of the n character string starting at str within the string,...
Definition string_view.hpp:545
constexpr string_view(string_view &&other) noexcept=default
Default move constructor.
constexpr auto find_first_not_of(string_view const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:691
constexpr auto substr(size_type const pos, size_type const count=npos) const noexcept -> string_view
Create a new string view based on a substring of the current one.
Definition string_view.hpp:346
constexpr auto size() const noexcept -> size_type
Get the length of the string view.
Definition string_view.hpp:220
constexpr string_view(raw_c_string const null_terminated_string) noexcept
Construct a string_view from a null terminated string, non-inclusive of the null terminator.
Definition string_view.hpp:141
std::size_t size_type
Alias for the type associated to the size of the string.
Definition string_view.hpp:81
constexpr auto compare(size_type const pos1, size_type const length1, string_view const other, size_type const pos2, size_type const length2) const noexcept -> integral_comparison_result
Compare a substring against a substring of another string view.
Definition string_view.hpp:279
~string_view()=default
Default destructor.
constexpr auto front() const noexcept -> const_reference
Get the first character in the string.
Definition string_view.hpp:418
constexpr auto starts_with(raw_c_string const other) const noexcept -> bool
Check if the string starts with another string.
Definition string_view.hpp:470
constexpr void swap(string_view &other) noexcept
Swap the content of two string_view objects.
Definition string_view.hpp:360
constexpr auto rfind(character const chr, size_type const pos=npos) const noexcept -> size_type
Find the latest position of the specified character within the string, starting at the specified posi...
Definition string_view.hpp:612
constexpr auto starts_with(string_view const other) const noexcept -> bool
Check if the string starts with another string.
Definition string_view.hpp:459
constexpr auto find_last_of(string_view const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence.
Definition string_view.hpp:747
constexpr auto cend() const noexcept -> const_iterator
End const iterator.
Definition string_view.hpp:193
constexpr auto compare(string_view const other) const noexcept -> integral_comparison_result
lexicographically compares two string_view, behavior similar to std::string
Definition string_view.hpp:252
constexpr auto operator[](std::size_t pos) const noexcept -> const_reference
Get a character at position pos in the string.
Definition string_view.hpp:385
constexpr auto operator=(string_view &&other) noexcept -> string_view &=default
Move assignment operator for the string view.
constexpr auto find(character const chr, size_type const pos=0U) const noexcept -> size_type
Find the earliest position of the specified character within the string, starting at the specified po...
Definition string_view.hpp:555
constexpr auto begin() const noexcept -> const_iterator
Begin const iterator.
Definition string_view.hpp:175
constexpr auto compare(raw_c_string const other) const noexcept -> integral_comparison_result
Compare a string_view against a null-terminated string.
Definition string_view.hpp:295
constexpr auto substr() const noexcept -> string_view
Create a new string view referencing the same string as *this.
Definition string_view.hpp:334
constexpr auto rfind(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Find the latest position of the n character string starting at str within the string,...
Definition string_view.hpp:601
const_char_span::pointer pointer
Alias to the pointer type associated to the underlying data.
Definition string_view.hpp:85
const_char_span::iterator iterator
Alias to the iterator associated with the string_view class.
Definition string_view.hpp:93
string_view(String const &str) noexcept
Construct a string_view from a std::string.
Definition string_view.hpp:158
const_char_span::reverse_iterator reverse_iterator
Alias to the reverse iterator.
Definition string_view.hpp:97
constexpr auto find_last_of(character const chr, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to the provided character.
Definition string_view.hpp:795
constexpr auto compare(size_type const pos1, size_type const length1, raw_c_string const other) const noexcept -> integral_comparison_result
Compare a substring against a null terminated string.
Definition string_view.hpp:307
constexpr auto find_first_of(raw_c_string const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence.
Definition string_view.hpp:658
static constexpr auto max_size() noexcept -> size_type
Get the maximum possible length of a string_view.
Definition string_view.hpp:235
constexpr auto end() const noexcept -> const_iterator
End const iterator.
Definition string_view.hpp:182
constexpr auto crend() const noexcept -> const_reverse_iterator
Obtain a reverse iterator referring to one-before the front of the string_view.
Definition string_view.hpp:213
constexpr auto back() const noexcept -> const_reference
Get the last character in the string.
Definition string_view.hpp:428
constexpr auto ends_with(string_view const other) const noexcept -> bool
Check if the string ends with another string.
Definition string_view.hpp:486
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr std::integral_constant< std::size_t, N > array< T, N >::size
Definition array.hpp:534
ARENE_NODISCARD constexpr auto next(Iterator itr, typename std::iterator_traits< Iterator >::difference_type steps=1) noexcept(noexcept(::arene::base::advance(itr, steps))) -> Iterator
Produces the Nth successor (or predecessor if steps is negative) of an iterator.
Definition next.hpp:38
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
constexpr auto copy(InputIterator first, InputIterator last, OutputIterator output) noexcept(noexcept(*(++output)) &&noexcept(*(++first)) &&noexcept(first !=last) &&std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output.
Definition copy.hpp:40
auto to_string(string_view const str) -> std::string
Construct a std::string from the string_view.
constexpr auto from_strong_ordering(strong_ordering const ordering) noexcept -> int
Convert a strong_ordering into an integer three-way-comparison result.
Definition strong_ordering.hpp:177
constexpr auto find(Iterator first, Iterator last, Value const &value_to_find) noexcept(denotes_nothrow_iterable_range_v< Iterator, Iterator > &&noexcept(*first==value_to_find) &&std::is_nothrow_copy_constructible< Iterator >::value) -> Iterator
Constexpr-compatible implementation of std::find.
Definition find.hpp:42
inequality_heuristic
An enumeration used for the return value of fast_inequality_check functions to indicate if two values...
Definition operators.hpp:696
@ may_be_equal_or_not_equal
The values checked may or may not be equal to each other; a more detailed check is required.
Definition operators.hpp:700
@ definitely_not_equal
The two values checked are definitely not equal to each other.
Definition operators.hpp:698
Definition accumulate.hpp:36
::ptrdiff_t ptrdiff_t
A type suitable for holding the difference between two pointers.
Definition cstddef.hpp:31
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76
basic_string< arene::base::detail::character > string
Alias for the common case of a string with char characters, standard traits and standard allocator.
Definition string.hpp:28
constexpr auto min(initializer_list< T > list, Compare comp) noexcept(noexcept(comp(std::declval< T & >(), std::declval< T & >())) &&std::is_nothrow_copy_constructible_v< T >) -> T
Returns the smallest value in the std::initializer_list.
Definition min_initializer_list_overload.hpp:50
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
Provides a backport of the constexpr implementation of std::reverse_iterator .