libostd
Classes | Public Types | Public Member Functions | List of all members
ostd::basic_char_range< T > Struct Template Reference

A string slice type. More...

Inheritance diagram for ostd::basic_char_range< T >:
ostd::input_range< basic_char_range< T > >

Public Types

using range_category = contiguous_range_tag
 
using value_type = T
 
using reference = T &
 
using size_type = std::size_t
 
- Public Types inherited from ostd::input_range< basic_char_range< T > >
using full_iterator = detail::full_range_iterator< basic_char_range< T > >
 A complete input iterator type for the range. More...
 

Public Member Functions

 basic_char_range () noexcept
 Constructs an empty slice.
 
 basic_char_range (value_type *beg, value_type *end) noexcept
 Constructs a slice from two pointers. More...
 
 basic_char_range (std::nullptr_t) noexcept
 Constructs an empty slice.
 
 basic_char_range (basic_char_range const &v) noexcept
 Slices are arbitrarily copy constructible.
 
template<typename U >
 basic_char_range (U &&beg, std::enable_if_t< std::is_convertible_v< U, value_type *>, nat >=nat{}) noexcept
 Constructs a slice from a pointer or a static array. More...
 
template<typename STR , typename A >
 basic_char_range (std::basic_string< std::remove_const_t< value_type >, STR, A > const &s) noexcept
 Constructs a slice from an std::basic_string. More...
 
template<typename U , typename = std::enable_if_t< std::is_convertible_v<U *, value_type *> >>
 basic_char_range (basic_char_range< U > const &v) noexcept
 Constructs a slice from a different but compatible slice. More...
 
basic_char_rangeoperator= (basic_char_range const &v) noexcept
 Slices are arbitrarily copy constructible.
 
template<typename STR , typename A >
basic_char_rangeoperator= (std::basic_string< value_type, STR, A > const &s) noexcept
 Assigns the slice's data from a matching std::basic_string.
 
basic_char_rangeoperator= (value_type *s) noexcept
 Assigns the slice's data from a pointer. More...
 
bool empty () const noexcept
 Checks if the slice is empty.
 
void pop_front ()
 Pops the first character out of the slice. More...
 
reference front () const noexcept
 Gets a reference to the first character. More...
 
void pop_back ()
 Pops the last character out of the slice. More...
 
reference back () const noexcept
 Gets a reference to the last character. More...
 
size_type size () const noexcept
 Gets the number of value_type in the slice.
 
size_type length () const noexcept
 Gets the number of code points in the slice. More...
 
size_type length (basic_char_range &cont) const noexcept
 Gets the number of code points in the slice. More...
 
basic_char_range slice (size_type start, size_type end) const noexcept
 Creates a sub-slice of the slice. More...
 
basic_char_range slice (size_type start) const noexcept
 Creates a sub-slice of the slice until the end. More...
 
reference operator[] (size_type i) const noexcept
 Gets a reference to a character within the slice. More...
 
void put (value_type v)
 Writes a character at the beginning and pops it out. More...
 
value_type * data () noexcept
 Gets the pointer to the beginning.
 
value_type const * data () const noexcept
 Gets the pointer to the beginning.
 
int compare (basic_char_range< value_type const > s) const noexcept
 Compares two slices. More...
 
int case_compare (basic_char_range< value_type const > s) const noexcept
 Compares two slices in a case insensitive manner. More...
 
template<typename C >
auto iter_u () const
 Iterate over the Unicode units of the given type. More...
 
template<std::size_t N>
auto iter_u () const
 Iterate over the Unicode units of the size in bits. More...
 
 operator std::basic_string_view< std::remove_cv_t< value_type >> () const noexcept
 Implicitly converts a string slice to std::basic_string_view. More...
 
- Public Member Functions inherited from ostd::input_range< basic_char_range< T > >
auto begin () const
 Creates a very simple iterator for range-based for loop. More...
 
std::nullptr_t end () const
 Gets a sentinel for begin().
 
full_iterator iter_begin () const
 Constructs a full_iterator for the range.
 
full_iterator iter_end () const
 Constructs a full_iterator end.
 
basic_char_range< T > iter () const
 Creates a copy of the range. More...
 
auto reverse () const
 Gets a reverse range to the current range. More...
 
auto movable () const
 Gets a wrapper range that moves all the elements. More...
 
auto enumerate () const
 Gets an enumerated range for the range. More...
 
auto take (std::size_t n) const
 Gets a range representing several elements of the range. More...
 
auto chunks (std::size_t n) const
 Splits the range into range of chunks. More...
 
auto join (R1 r1, RR ...rr) const
 Joins multiple ranges together. More...
 
auto zip (R1 r1, RR ...rr) const
 Zips multiple ranges together. More...
 
auto operator* () const
 Simpler syntax for accessing the front element.
 
basic_char_range< T > & operator++ ()
 Simpler syntax for popping out the front element.
 
basic_char_range< T > operator++ (int)
 Simpler syntax for popping out the front element. More...
 
auto operator| (F &&func)
 A necessary overload for pipeable algorithm support.
 
auto operator| (F &&func) const
 A necessary overload for pipeable algorithm support.
 
 operator bool () const
 Checks if the range is not empty.
 

Detailed Description

template<typename T>
struct ostd::basic_char_range< T >

A string slice type.

This is a contiguous range over a character type. The character type can be any of the standard character types, of any size - for example you would use char32_t to represent UTF-32 slices.

The range is mutable, i.e. it implements the output range interface.

Constructor & Destructor Documentation

◆ basic_char_range() [1/4]

template<typename T>
ostd::basic_char_range< T >::basic_char_range ( value_type *  beg,
value_type *  end 
)
inlinenoexcept

Constructs a slice from two pointers.

The first pointer is the beginning of the slice and the second pointer is just past the end.

◆ basic_char_range() [2/4]

template<typename T>
template<typename U >
ostd::basic_char_range< T >::basic_char_range ( U &&  beg,
std::enable_if_t< std::is_convertible_v< U, value_type *>, nat >  = nat{} 
)
inlinenoexcept

Constructs a slice from a pointer or a static array.

This constructor handles two cases. The input must be convertible to T *, if it's not, this constructor is not enabled. Effectively, if the input is a static array of T, the entire array is used to create the slice, minus the potential zero at the end. If there is no zero at the end, nothing is removed and the array is used whole. If the input is not an array, the size is checked at runtime.

◆ basic_char_range() [3/4]

template<typename T>
template<typename STR , typename A >
ostd::basic_char_range< T >::basic_char_range ( std::basic_string< std::remove_const_t< value_type >, STR, A > const &  s)
inlinenoexcept

Constructs a slice from an std::basic_string.

This uses the string's data to construct a matching slice.

◆ basic_char_range() [4/4]

template<typename T>
template<typename U , typename = std::enable_if_t< std::is_convertible_v<U *, value_type *> >>
ostd::basic_char_range< T >::basic_char_range ( basic_char_range< U > const &  v)
inlinenoexcept

Constructs a slice from a different but compatible slice.

A pointer to the other slice's value type must be convertible to a pointer to the new slice's value type, otherwise the constructor will not be enabled.

Member Function Documentation

◆ back()

template<typename T>
reference ostd::basic_char_range< T >::back ( ) const
inlinenoexcept

Gets a reference to the last character.

The behavior is undefined when the slice is empty.

See also
front(), pop_back()

◆ compare()

template<typename T>
int ostd::basic_char_range< T >::compare ( basic_char_range< value_type const >  s) const
inlinenoexcept

Compares two slices.

This works similarly to the C function strcmp or the compare method of std::char_traits, but does not depend on the strings to be terminated.

It performs an ordinary lexicographical comparison, the values are compared and the first string to have a lesser value is considered lexicographically less. If they are equal up to a point but one of them terminates early, it's also less.

If the this slice is the lesser one, a negative value is returned. If they are equal (if they're both zero length, it counts as equal) then 0 is returned. Otherwise, a positive value is returned.

This works with the slice's native unit values, i.e. bytes for UTF-8, char16_t for UTF-16 and char32_t for UTF-32. These units are compared by getting the difference between them (i.e. this[index] - other[index]).

It is not a part of the range interface, just the string slice interface.

See also
case_compare()

◆ front()

template<typename T>
reference ostd::basic_char_range< T >::front ( ) const
inlinenoexcept

Gets a reference to the first character.

The behavior is undefined when the slice is empty.

See also
back(), pop_front()

◆ iter_u()

template<typename T>
template<std::size_t N>
auto ostd::basic_char_range< T >::iter_u ( ) const
inline

Iterate over the Unicode units of the size in bits.

The type maps to char for 8, char16_t for 16 and char32_t for 32, or UTF-8, UTF-16 and UTF-32.

Like utf::iter_u().

◆ operator std::basic_string_view< std::remove_cv_t< value_type >>()

template<typename T>
ostd::basic_char_range< T >::operator std::basic_string_view< std::remove_cv_t< value_type >> ( ) const
inlinenoexcept

Implicitly converts a string slice to std::basic_string_view.

String views represent more or less the same thing but they're always immutable. This simple conversion allows usage of string slices on any API that uses either strings or string view, as well as construct strings and string views out of slices.

◆ operator=()

template<typename T>
basic_char_range& ostd::basic_char_range< T >::operator= ( value_type *  s)
inlinenoexcept

Assigns the slice's data from a pointer.

The data pointed to by the argument must be zero terminated.

◆ operator[]()

template<typename T>
reference ostd::basic_char_range< T >::operator[] ( size_type  i) const
inlinenoexcept

Gets a reference to a character within the slice.

The behavior is undefined if the index is not within the bounds.

◆ pop_back()

template<typename T>
void ostd::basic_char_range< T >::pop_back ( )
inline

Pops the last character out of the slice.

This is bounds checked, std::out_of_range is thrown when slice was already empty before popping out the character. No changes are done to the slice if it throws.

Exceptions
std::out_of_rangewhen empty.
See also
back(), pop_front()

◆ pop_front()

template<typename T>
void ostd::basic_char_range< T >::pop_front ( )
inline

Pops the first character out of the slice.

This is bounds checked, std::out_of_range is thrown when slice was already empty before popping out the character. No changes are done to the slice if it throws.

Exceptions
std::out_of_rangewhen empty.
See also
front(), pop_back()

◆ put()

template<typename T>
void ostd::basic_char_range< T >::put ( value_type  v)
inline

Writes a character at the beginning and pops it out.

Exceptions
std::out_of_rangewhen empty.

◆ slice() [1/2]

template<typename T>
basic_char_range ostd::basic_char_range< T >::slice ( size_type  start,
size_type  end 
) const
inlinenoexcept

Creates a sub-slice of the slice.

Behavior is undefined if start and end are not within the slice's bounds. There is no bound checking done in this call. It's also undefined if the first argument is larger than the second argument.

◆ slice() [2/2]

template<typename T>
basic_char_range ostd::basic_char_range< T >::slice ( size_type  start) const
inlinenoexcept

Creates a sub-slice of the slice until the end.

Equivalent to slice(size_type, size_type) with size() as the second argument. The first argument must be within the slice's boundaries otherwis the behavior is undefined.


The documentation for this struct was generated from the following file: