Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arene::base::null_terminated_string_view Class Reference

A read-only string-view that provides a view onto a null-terminated string. More...

Inheritance diagram for arene::base::null_terminated_string_view:
Inheritance graph

Public Types

using const_iterator = ::arene::base::detail::wrapped_iterator<const_pointer, passkey>
 The type of const iterator used by this view.
 
using const_pointer = value_type const*
 The const pointer type of this view.
 
using const_reference = value_type const&
 The type of a reference to an element in the view.
 
using difference_type = typename iterator::difference_type
 The type used for differences between iterators in this view.
 
using iterator = const_iterator
 The type of iterator used by this view.
 
using pointer = value_type*
 The pointer type of this view.
 
using reference = value_type&
 The type of a reference to an element in the view.
 
using size_type = std::size_t
 The type used for sizes in this view.
 
using value_type = detail::character
 The value type of this view.
 

Public Member Functions

constexpr null_terminated_string_view () noexcept
 construct an empty string-view.
 
constexpr null_terminated_string_view (detail::raw_c_string const string) noexcept
 construct a view on an existing null-terminated string.
 
constexpr null_terminated_string_view (null_terminated_string_view &&)=default
 default move constructor
 
constexpr null_terminated_string_view (null_terminated_string_view const &)=default
 default copy constructor
 
 null_terminated_string_view (std::nullptr_t)=delete
 constructing from a nullptr literal is always an error.
 
template<typename String, constraints< std::enable_if_t< std::is_same< String, std::string >::value > > = nullptr>
 null_terminated_string_view (String const &string) noexcept
 Implicit construction from a std::string (which is required to be null-terminated).
 
 ~null_terminated_string_view ()=default
 default destructor
 
ARENE_NODISCARD constexpr auto begin () const noexcept -> const_iterator
 Get an iterator to the first element of the string.
 
ARENE_NODISCARD constexpr auto c_str () const noexcept -> const_pointer
 Obtain a C-style null-terminated string pointer.
 
constexpr auto compare (null_terminated_string_view const other) const noexcept -> integral_comparison_result
 Compare this null-terminated string to another null-terminated string.
 
template<typename String, constraints< std::enable_if_t< std::is_same< String, std::string >::value > > = nullptr>
auto compare (String const &other) const noexcept -> integral_comparison_result
 Compare this null-terminated string to a std::string.
 
constexpr auto compare (string_view const &other) const noexcept -> integral_comparison_result
 Compare this null-terminated string to a string_view.
 
ARENE_NODISCARD constexpr auto data () const noexcept -> const_pointer
 Obtain a C-style null-terminated string pointer.
 
ARENE_NODISCARD constexpr auto empty () const noexcept -> bool
 Query if this string is the empty string.
 
ARENE_NODISCARD constexpr auto length () const noexcept -> size_type
 Query the length of the string.
 
constexpr operator string_view () const noexcept
 Implicit conversion to string_view.
 
constexpr auto operator= (null_terminated_string_view &&) -> null_terminated_string_view &=default
 default move assignment
 
constexpr auto operator= (null_terminated_string_view const &) -> null_terminated_string_view &=default
 default copy assignment
 
ARENE_NODISCARD constexpr auto size () const noexcept -> size_type
 Query the length of the string.
 
ARENE_NODISCARD constexpr auto substr (std::size_t offset) const noexcept -> null_terminated_string_view
 Compute the sub-string starting at the specified offset within the string.
 
ARENE_NODISCARD constexpr auto substr (std::size_t offset, std::size_t max_length) const noexcept -> string_view
 Compute the sub-string starting at the specified offset and with the specified maximum length.
 

Static Public Member Functions

static ARENE_NODISCARD constexpr auto end () noexcept -> sentinel
 Get the sentinel (end-iterator) of this string.
 
static constexpr auto three_way_compare (null_terminated_string_view const lhs, detail::raw_c_string const rhs) noexcept -> strong_ordering
 Compare two null-terminated strings.
 
static constexpr auto three_way_compare (null_terminated_string_view const lhs, null_terminated_string_view const rhs) noexcept -> strong_ordering
 Compare two null-terminated strings.
 
template<typename String, constraints< std::enable_if_t< std::is_same< String, std::string >::value > > = nullptr>
static auto three_way_compare (null_terminated_string_view const lhs, String const &rhs) noexcept -> strong_ordering
 Compare a null-terminated string to a std::string.
 
static constexpr auto three_way_compare (null_terminated_string_view const lhs, string_view const rhs) noexcept -> strong_ordering
 Compare a null-terminated string to a string_view.
 

Friends

ARENE_NODISCARD friend constexpr auto operator!= (iterator const iter, sentinel) noexcept -> bool
 Equality comparison for a null terminated string view's iterator and the end-of-string sentinel.
 
ARENE_NODISCARD friend constexpr auto operator== (iterator const iter, sentinel) noexcept -> bool
 Equality comparison for a null terminated string view's iterator and the end-of-string sentinel.
 

Detailed Description

A read-only string-view that provides a view onto a null-terminated string.

This string type can be used to pass around null-terminated strings more safely and explicitly compared to passing a 'const char*', making it easier to query the length of the string, compare strings, compute sub-strings, etc.

This class just holds a reference to a string stored elsewhere. The null_terminated_string_view does not own the elements of the string.

You should prefer to use string_view over this class unless the extra storage of the size_t required by string_view is prohibitively expensive compared to the cost of repeatedly calculating the string length every time it is used.

Member Typedef Documentation

◆ const_iterator

using arene::base::null_terminated_string_view::const_iterator = ::arene::base::detail::wrapped_iterator<const_pointer, passkey>

The type of const iterator used by this view.

◆ const_pointer

The const pointer type of this view.

◆ const_reference

The type of a reference to an element in the view.

◆ difference_type

using arene::base::null_terminated_string_view::difference_type = typename iterator::difference_type

The type used for differences between iterators in this view.

◆ iterator

The type of iterator used by this view.

◆ pointer

The pointer type of this view.

◆ reference

The type of a reference to an element in the view.

◆ size_type

The type used for sizes in this view.

◆ value_type

The value type of this view.

Constructor & Destructor Documentation

◆ null_terminated_string_view() [1/6]

arene::base::null_terminated_string_view::null_terminated_string_view ( )
inlineconstexprnoexcept

construct an empty string-view.

◆ null_terminated_string_view() [2/6]

arene::base::null_terminated_string_view::null_terminated_string_view ( null_terminated_string_view const & )
constexprdefault

default copy constructor

◆ null_terminated_string_view() [3/6]

arene::base::null_terminated_string_view::null_terminated_string_view ( null_terminated_string_view && )
constexprdefault

default move constructor

◆ ~null_terminated_string_view()

arene::base::null_terminated_string_view::~null_terminated_string_view ( )
default

default destructor

◆ null_terminated_string_view() [4/6]

arene::base::null_terminated_string_view::null_terminated_string_view ( std::nullptr_t )
explicitdelete

constructing from a nullptr literal is always an error.

◆ null_terminated_string_view() [5/6]

arene::base::null_terminated_string_view::null_terminated_string_view ( detail::raw_c_string const string)
inlineexplicitconstexprnoexcept

construct a view on an existing null-terminated string.

Parameters
stringA pointer to the first character of the null-terminated string. Caller must ensure that the string is properly null-terminated. Passing a nullptr has undefined behaviour.

◆ null_terminated_string_view() [6/6]

template<typename String, constraints< std::enable_if_t< std::is_same< String, std::string >::value > > = nullptr>
arene::base::null_terminated_string_view::null_terminated_string_view ( String const & string)
inlinenoexcept

Implicit construction from a std::string (which is required to be null-terminated).

The resulting string view is invalidated by any operation that modifies the string.

Parameters
stringThe source string
Note
This constructor is a template rather than a non-template taking a const std::string& to avoid the possibility of unknowingly constructing a temporary std::string implicitly by passing something that is convertible to std::string. e.g. a std::initializer_list<char>

Member Function Documentation

◆ begin()

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::begin ( ) const -> const_iterator
inlineconstexprnoexcept

Get an iterator to the first element of the string.

Returns
const_iterator An iterator to the first character in the string.

◆ c_str()

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::c_str ( ) const -> const_pointer
inlineconstexprnoexcept

Obtain a C-style null-terminated string pointer.

Returns
const_pointer A pointer to the first character in the string.

◆ compare() [1/3]

auto arene::base::null_terminated_string_view::compare ( null_terminated_string_view const other) const -> integral_comparison_result
inlineconstexprnoexcept

Compare this null-terminated string to another null-terminated string.

Parameters
otherThe other string to compare *this to.
Returns
integral_comparison_result A negative value if *this lexicographically precedes other, a positive value if other lexicographically precedes *this, or zero if the two strings are equal.

◆ compare() [2/3]

template<typename String, constraints< std::enable_if_t< std::is_same< String, std::string >::value > > = nullptr>
auto arene::base::null_terminated_string_view::compare ( String const & other) const -> integral_comparison_result
inlinenoexcept

Compare this null-terminated string to a std::string.

Parameters
otherThe other string to compare *this to.
Returns
A negative value if *this lexicographically precedes other, a positive value if other lexicographically precedes *this, or zero if the two strings are equal.
Note
This constructor is a template rather than a non-template taking a const std::string& to handle when std::string is only declared, not defined.

◆ compare() [3/3]

auto arene::base::null_terminated_string_view::compare ( string_view const & other) const -> integral_comparison_result
inlineconstexprnoexcept

Compare this null-terminated string to a string_view.

Parameters
otherThe other string to compare *this to.
Returns
A negative value if *this lexicographically precedes other, a positive value if other lexicographically precedes *this, or zero if the two strings are equal.

◆ data()

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::data ( ) const -> const_pointer
inlineconstexprnoexcept

Obtain a C-style null-terminated string pointer.

Returns
const_pointer A pointer to the first character in the string.

◆ empty()

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::empty ( ) const -> bool
inlineconstexprnoexcept

Query if this string is the empty string.

Returns
true if the string is empty, false otherwise

◆ end()

static ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::end ( ) -> sentinel
inlinestaticconstexprnoexcept

Get the sentinel (end-iterator) of this string.

Returns
sentinel The sentinel comparable to an iterator to mark the end of the string.

◆ length()

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::length ( ) const -> size_type
inlineconstexprnoexcept

Query the length of the string.

Returns
The number of 'char' elements in the null-terminate string, not including the null-terminator.
Note
This operation needs to traverse the string to determine the length and so may be expensive to call repeatedly. If you need to call length() repeatedly then either cache the value or convert this object to a string_view first and use that instead.

◆ operator string_view()

arene::base::null_terminated_string_view::operator string_view ( ) const
inlineconstexprnoexcept

Implicit conversion to string_view.

Returns
A string_view that spans the characters of *this, minus the null-terminator.

◆ operator=() [1/2]

auto arene::base::null_terminated_string_view::operator= ( null_terminated_string_view && ) -> null_terminated_string_view &=default
constexprdefault

default move assignment

◆ operator=() [2/2]

auto arene::base::null_terminated_string_view::operator= ( null_terminated_string_view const & ) -> null_terminated_string_view &=default
constexprdefault

default copy assignment

◆ size()

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::size ( ) const -> size_type
inlineconstexprnoexcept

Query the length of the string.

Returns
The number of 'char' elements in the null-terminate string, not including the null-terminator.
Note
This operation needs to traverse the string to determine the length and so may be expensive to call repeatedly. If you need to call size() repeatedly then either cache the value or convert this object to a string_view first and use that instead.

◆ substr() [1/2]

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::substr ( std::size_t offset) const -> null_terminated_string_view
inlineconstexprnoexcept

Compute the sub-string starting at the specified offset within the string.

Parameters
offsetThe index of the first character that the returned substring should start at.
Returns
null_terminated_string_view A view which starts at the character at index offset and continues to the end of the string. If offset is larger than length() then returns the empty string view.
Note
This operation has complexity of O(min(offset, length())).

◆ substr() [2/2]

ARENE_NODISCARD constexpr auto arene::base::null_terminated_string_view::substr ( std::size_t offset,
std::size_t max_length ) const -> string_view
inlineconstexprnoexcept

Compute the sub-string starting at the specified offset and with the specified maximum length.

Parameters
offsetThe index of the first charcter of the substring. If this value is greater than length() then returns the empty sub-string.
max_lengthThe maximum length of the returned substring. The returned substring may be shorter than this length if there are fewer than max_length elements after offset.
Returns
string_view A substring referencing the elements of *this starting at index offset with a length at most max_length.
Note
This operation has complexity of O(min(offset+max_length, length()))

◆ three_way_compare() [1/4]

static constexpr auto arene::base::null_terminated_string_view::three_way_compare ( null_terminated_string_view const lhs,
detail::raw_c_string const rhs ) -> strong_ordering
inlinestaticconstexprnoexcept

Compare two null-terminated strings.

Parameters
lhsThe first string to compare
rhsThe second string to compare
Returns
strong_ordering::less if lhs lexicographically precedes rhs
strong_ordering::greater if rhs lexicographically precedes lhs
strong_ordering::equal if the two strings are equal.

◆ three_way_compare() [2/4]

static constexpr auto arene::base::null_terminated_string_view::three_way_compare ( null_terminated_string_view const lhs,
null_terminated_string_view const rhs ) -> strong_ordering
inlinestaticconstexprnoexcept

Compare two null-terminated strings.

Parameters
lhsThe first string to compare
rhsThe second string to compare
Returns
strong_ordering::less if lhs lexicographically precedes rhs
strong_ordering::greater if rhs lexicographically precedes lhs
strong_ordering::equal if the two strings are equal.

◆ three_way_compare() [3/4]

template<typename String, constraints< std::enable_if_t< std::is_same< String, std::string >::value > > = nullptr>
static auto arene::base::null_terminated_string_view::three_way_compare ( null_terminated_string_view const lhs,
String const & rhs ) -> strong_ordering
inlinestaticnoexcept

Compare a null-terminated string to a std::string.

Parameters
lhsThe first string to compare
rhsThe second string to compare
Returns
strong_ordering::less if lhs lexicographically precedes rhs
strong_ordering::greater if rhs lexicographically precedes lhs
strong_ordering::equal if the two strings are equal.
Note
This constructor is a template rather than a non-template taking a const std::string& to handle when std::string is only declared, not defined.

◆ three_way_compare() [4/4]

static constexpr auto arene::base::null_terminated_string_view::three_way_compare ( null_terminated_string_view const lhs,
string_view const rhs ) -> strong_ordering
inlinestaticconstexprnoexcept

Compare a null-terminated string to a string_view.

Parameters
lhsThe first string to compare
rhsThe second string to compare
Returns
strong_ordering::less if lhs lexicographically precedes rhs
strong_ordering::greater if rhs lexicographically precedes lhs
strong_ordering::equal if the two strings are equal.

Friends And Related Symbol Documentation

◆ operator!=

ARENE_NODISCARD friend constexpr auto operator!= ( iterator const iter,
sentinel  ) -> bool
friend

Equality comparison for a null terminated string view's iterator and the end-of-string sentinel.

Parameters
iterThe iterator to compare against the sentinel.
Returns
true if the iterator points to a '\0' character.
false Otherwise.

◆ operator==

ARENE_NODISCARD friend constexpr auto operator== ( iterator const iter,
sentinel  ) -> bool
friend

Equality comparison for a null terminated string view's iterator and the end-of-string sentinel.

Parameters
iterThe iterator to compare against the sentinel.
Returns
true if the iterator points to a '\0' character.
false Otherwise.

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