omni::structuredlog::BasicStringView

Defined in omni/structuredlog/StringView.h

template<class CharT, class Traits = std::char_traits<CharT>>
class BasicStringView : public carb::cpp::basic_string_view<CharT, std::char_traits<CharT>>

An extension of carb::cpp::basic_string_view that can handle nullptr and std::basic_string as inputs.

Template Parameters
  • CharT – The char type pointed to by the view.

  • Traits – The type traits for CharT.

Public Types

using traits_type = Traits

Traits

using value_type = CharT

CharT

using pointer = CharT*

CharT*

using const_pointer = const CharT*

const CharT*

using reference = CharT&

CharT&

using const_reference = const CharT&

const CharT&

using const_iterator = omni::detail::PointerIterator<const_pointer, basic_string_view>

implementation defined constant LegacyRandomAccessIterator and LegacyContiguousIterator whose value_type is CharT.

using iterator = const_iterator

const_iterator

Note

iterator and const_iterator are the same type because string views are views into constant character sequences.

using const_reverse_iterator = std::reverse_iterator<const_iterator>

std::reverse_iterator<const_iterator>

using reverse_iterator = const_reverse_iterator

const_reverse_iterator

using size_type = std::size_t

std::size_t

using difference_type = std::ptrdiff_t

std::ptrdiff_t

Public Functions

inline constexpr BasicStringView()
inline constexpr BasicStringView(const CharT *s)

Create a string view from a C string.

Parameters

s[in] The C string to create a view into. This original pointer continues to be used and nothing is copied.

inline constexpr BasicStringView(const CharT *s, size_t len)

Create a string view from a char buffer.

Parameters
  • s[in] The char buffer to create a view into. This does not need to be null terminated. This original pointer continues to be used and nothing is copied.

  • len[in] The length of the view into s, in characters.

inline constexpr BasicStringView(const std::basic_string<CharT> &s)

Create a string view from a std::basic_string.

Parameters

s[in] The string to create this string view for.

inline constexpr const_iterator begin() const noexcept

Returns an iterator to the beginning.

Returns

A const_iterator to the first character of the view.

inline constexpr const_iterator cbegin() const noexcept

Returns an iterator to the beginning.

Returns

A const_iterator to the first character of the view.

inline constexpr const_iterator end() const noexcept

Returns an iterator to the end.

Returns

A const_iterator to the character following the last character of the view. This character acts as a placeholder, attempting to access it results in undefined behavior.

inline constexpr const_iterator cend() const noexcept

Returns an iterator to the end.

Returns

A const_iterator to the character following the last character of the view. This character acts as a placeholder, attempting to access it results in undefined behavior.

inline constexpr const_reverse_iterator rbegin() const noexcept

Returns a reverse iterator to the beginning.

Returns

A const_reverse_iterator to the first character of the reversed view. It corresponds to the last character of the non-reversed view.

inline constexpr const_reverse_iterator crbegin() const noexcept

Returns a reverse iterator to the beginning.

Returns

A const_reverse_iterator to the first character of the reversed view. It corresponds to the last character of the non-reversed view.

inline constexpr const_reverse_iterator rend() const noexcept

Returns a reverse iterator to the end.

Returns

a const_reverse_iterator to the character following the last character of the reversed view. It corresponds to the character preceding the first character of the non-reversed view. This character acts as a placeholder, attempting to access it results in undefined behavior.

inline constexpr const_reverse_iterator crend() const noexcept

Returns a reverse iterator to the end.

Returns

a const_reverse_iterator to the character following the last character of the reversed view. It corresponds to the character preceding the first character of the non-reversed view. This character acts as a placeholder, attempting to access it results in undefined behavior.

inline constexpr const_reference operator[](size_type pos) const noexcept

Accesses the specified character.

Returns a const reference to the character at the specified position. No bounds checking is performed: the behavior is undefined if pos >= size().

Parameters

pos – The position of the character to return.

Returns

A const_reference to the requested character.

inline constexpr const_reference at(size_type pos) const

Accesses the specified character with bounds checking.

Returns a const reference to the character at the specified position. Bounds checking is performed.

Throws

std::out_of_range – Invalid access: pos is at or after size().

Parameters

pos – The position of the character to return.

Returns

A const_reference to the requested character

inline constexpr const_reference front() const noexcept

Accesses the first character.

Returns

A const_reference to the first character in the view. The behavior is undefined if empty() is true.

inline constexpr const_reference back() const noexcept

Accesses the last character.

Returns

A const_reference to the last character in the view. The behavior is undefined if empty() is true.

inline constexpr const_pointer data() const noexcept

Returns a pointer to the first character of a view.

Returns a pointer to the underlying character array. The pointer is such that the range [data(), data() + size()) is valid and the values in it correspond to the values of the view.

Note

Unlike std::basic_string::data() and string literals, this function returns a pointer to a buffer that is not necessarily null-terminated, for example a substring view (e.g. from remove_suffix()). Therefore, it is typically a mistake to pass data() to a routine that takes just a const CharT* and expects a null- terminated string.

Returns

A const_pointer to the underlying character array.

inline constexpr size_type size() const noexcept

Returns the number of characters.

Returns the number of CharT characters in the view, i.e. std::distance(begin(), end()).

Returns

The number of CharT elements in the view.

inline constexpr size_type length() const noexcept

Returns the number of characters.

Returns the number of CharT characters in the view, i.e. std::distance(begin(), end()).

Returns

The number of CharT elements in the view.

inline constexpr size_type max_size() const noexcept

Returns the maximum number of characters.

The largest possible number of char-like objects that can be referred to by a basic_string_view.

Returns

Maximum number of characters.

inline constexpr bool empty() const noexcept

Checks whether the view is empty.

Checks if the view has no characters, i.e. whether size() is ​0​.

Returns

true if the view is empty, false otherwise.

inline constexpr void remove_prefix(size_type n) noexcept

Shrinks the view by moving its start forward.

Moves the start of the view forward by n characters. The behavior is undefined if n > size().

Parameters

n – Number of characters to remove from the start of the view.

inline constexpr void remove_suffix(size_type n) noexcept

Shrinks the view by moving its end backward.

Moves the end of the view back by n characters. The behavior is undefined if n > size().

Parameters

n – Number of characters to remove from the end of the view.

inline constexpr void swap(basic_string_view &v) noexcept

Swaps the contents.

Exchanges the view with that of v.

Parameters

v – View to swap with.

inline constexpr size_type copy(CharT *dest, size_type count, size_type pos = 0) const

Copies characters.

Copies the substring [pos, pos + rcount) to the character array pointed to by dest, where rcount is the smaller of count and size() - pos. Equivalent to Traits::copy(dest, data() + pos, rcount).

Throws

std::out_of_range – if pos > size().

Parameters
  • dest – Pointer to the destination character string.

  • count – Requested substring length.

  • pos – Position of first character.

Returns

Number of characters copied.

inline constexpr basic_string_view substr(size_t pos, size_t count = npos) const

Returns a substring.

Returns a view of the substring [pos, pos + rcount), where rcount is the smaller of count and size() - pos.

Throws

std::out_of_range – if pos > size().

Parameters
  • pos – Position of the first character.

  • count – Requested length.

Returns

View of the substring [pos, pos + count).

inline constexpr int compare(basic_string_view v) const noexcept

Compares two views.

The length rlen of the sequences to compare is the smaller of size() and v.size(). The function compares the two views by calling traits::compare(data(), v.data(), rlen), and returns as follows:

  • A value less than zero (<0) if:

    • Traits::compare(data(), v.data(), rlen) < 0, or

    • Traits::compare(data(), v.data(), rlen) == 0 and size() < v.size().

  • A value of zero (0) if:

    • Traits::compare(data(), v.data(), rlen) == 0 and size() == v.size().

  • A value greater than zero (>0) if:

    • Traits::compare(data(), v.data(), rlen) > 0, or

    • Traits::compare(data(), v.data(), rlen) == 0 and size() > v.size().

Parameters

v – View to compare

Returns

A negative value if *this is less than the other character sequence, 0 if both character sequences are equal, positive value if *this is greater than the other character sequence. See above.

inline constexpr int compare(size_type pos1, size_type count1, basic_string_view v) const

Compares two views.

Equivalent to substr(pos1, count1).compare(v).

Throws

std::out_of_range – if pos1 > size().

Parameters
  • pos1 – Position of the first character in this view to compare.

  • count1 – Number of characters of this view to compare.

  • v – View to compare.

Returns

see compare(basic_string_view) const noexcept

inline constexpr int compare(size_type pos1, size_type count1, basic_string_view v, size_type pos2, size_type count2) const

Compares two views.

Equivalent to substr(pos1, count1).compare(v.substr(pos2, count2)).

Throws

std::out_of_range – if pos1 > size() or if pos2 > v.size().

Parameters
  • pos1 – Position of the first character in this view to compare.

  • count1 – Number of characters of this view to compare.

  • v – View to compare.

  • pos2 – Position of the first character of the given view to compare.

  • count2 – Number of characters of the given view to compare.

Returns

see compare(basic_string_view) const noexcept

inline constexpr int compare(const CharT *s) const

Compares two views.

Equivalent to compare(basic_string_view(s)).

Parameters

s – Pointer to the null-terminated character string to compare to.

Returns

see compare(basic_string_view) const noexcept

inline constexpr int compare(size_type pos1, size_type count1, const CharT *s) const

Compares two views.

Equivalent to substr(pos1, count1).compare(basic_string_view(s)).

Throws

std::out_of_range – if pos1 > size().

Parameters
  • pos1 – Position of the first character in this view to compare.

  • count1 – Number of characters of this view to compare.

  • s – Pointer to the null-terminated character string to compare to.

Returns

see compare(basic_string_view) const noexcept

inline constexpr int compare(size_type pos1, size_type count1, const CharT *s, size_type count2) const

Compares two views.

Equivalent to substr(pos1, count1).compare(basic_string_view(s, count2)). Behavior is undefined if [s, s+count2) is not a valid contiguous range.

Throws

std::out_of_range – if pos1 > size().

Parameters
  • pos1 – Position of the first character in this view to compare.

  • count1 – Number of characters of this view to compare.

  • s – Pointer to the character string to compare to.

  • count2 – Number of characters of s to compare.

Returns

see compare(basic_string_view) const noexcept

inline constexpr bool starts_with(basic_string_view sv) const noexcept

Checks if the string view starts with the given prefix.

Effectively returns substr(0, sv.size()) == sv.

Parameters

sv – A string view which may be a result of implicit conversion from std::basic_string.

Returns

true if the string view begins with the provided prefix, false otherwise.

inline constexpr bool starts_with(CharT ch) const noexcept

Checks if the string view starts with the given prefix.

Effectively returns !empty() && Traits::eq(front(), ch).

Parameters

ch – A single character.

Returns

true if the string view begins with the provided prefix, false otherwise.

inline constexpr bool starts_with(const CharT *s) const

Checks if the string view starts with the given prefix.

Effectively returns starts_with(basic_string_view(s)).

Parameters

s – A null-terminated character string.

Returns

true if the string view begins with the provided prefix, false otherwise.

inline constexpr bool ends_with(basic_string_view sv) const noexcept

Checks if the string view ends with the given suffix.

Effectively returns size() >= sv.size() && compare(size() - sv.size(), npos, sv) == 0.

Parameters

sv – A string view which may be a result of implicit conversion from std::basic_string.

Returns

true if the string view ends with the provided suffix, false otherwise.

inline constexpr bool ends_with(CharT ch) const noexcept

Checks if the string view ends with the given suffix.

Effectively returns !empty() && Traits::eq(back(), ch).

Parameters

ch – A single character.

Returns

true if the string view ends with the provided suffix, false otherwise.

inline constexpr bool ends_with(const CharT *s) const

Checks if the string view ends with the given suffix.

Effectively returns ends_with(basic_string_view(s)).

Parameters

s – A null-terminated character string.

Returns

true if the string view ends with the provided suffix, false otherwise.

inline constexpr bool contains(basic_string_view sv) const noexcept

Checks if the string view contains the given substring or character.

Effectively find(sv) != npos.

Parameters

sv – A string view.

Returns

true if the string view contains the provided substring, false otherwise.

inline constexpr bool contains(CharT c) const noexcept

Checks if the string view contains the given substring or character.

Effectively find(c) != npos.

Parameters

c – A single character.

Returns

true if the string view contains the provided substring, false otherwise.

inline constexpr bool contains(const CharT *s) const

Checks if the string view contains the given substring or character.

Effectively find(s) != npos.

Parameters

s – A null-terminated character string.

Returns

true if the string view contains the provided substring, false otherwise.

inline constexpr size_type find(basic_string_view v, size_type pos = 0) const noexcept

Find characters in the view.

Finds the first substring equal to the given character sequence. Complexity is O(size() * v.size()) at worst.

Parameters
  • v – View to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type find(CharT ch, size_type pos = 0) const noexcept

Find characters in the view.

Finds the first substring equal to the given character sequence. Equivalent to find(basic_string_view(std::addressof(ch), 1)). Complexity is O(size()) at worst.

Parameters
  • ch – Character to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type find(const CharT *s, size_type pos, size_type count) const

Find characters in the view.

Finds the first substring equal to the given character sequence. Equivalent to find(basic_string_view(s, count), pos). Complexity is O(size() * count) at worst.

Parameters
  • s – Pointer to a character string to search for.

  • pos – Position at which to start the search.

  • count – Length of substring to search for.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type find(const CharT *s, size_type pos = 0) const

Find characters in the view.

Finds the first substring equal to the given character sequence. Equivalent to find(basic_string_view(s), pos). Complexity is O(size() * Traits::length(s)) at worst.

Parameters
  • s – Pointer to a character string to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type rfind(basic_string_view v, size_type pos = npos) const noexcept

Find the last occurrence of a substring.

Finds the last substring equal to the given character sequence. Search begins at pos, i.e. the found substring must not being is a position following pos. If npos or any value not smaller than size()-1 is passed as pos, the whole string will be searched. Complexity is O(size() * v.size()) at worst.

Parameters
  • v – View to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type rfind(CharT ch, size_type pos = npos) const noexcept

Find the last occurrence of a substring.

Finds the last substring equal to the given character sequence. Search begins at pos, i.e. the found substring must not being is a position following pos. If npos or any value not smaller than size()-1 is passed as pos, the whole string will be searched. Equivalent to rfind(basic_string_view(std::addressof(ch), 1), pos). Complexity is O(size()) at worst.

Parameters
  • ch – Character to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type rfind(const CharT *s, size_type pos, size_type count) const

Find the last occurrence of a substring.

Finds the last substring equal to the given character sequence. Search begins at pos, i.e. the found substring must not being is a position following pos. If npos or any value not smaller than size()-1 is passed as pos, the whole string will be searched. Equivalent to rfind(basic_string_view(s, count), pos). Complexity is O(size() * count) at worst.

Parameters
  • s – Pointer to a character string to search for.

  • pos – Position at which to start the search.

  • count – Length of substring to search for.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type rfind(const CharT *s, size_type pos = npos) const

Find the last occurrence of a substring.

Finds the last substring equal to the given character sequence. Search begins at pos, i.e. the found substring must not being is a position following pos. If npos or any value not smaller than size()-1 is passed as pos, the whole string will be searched. Equivalent to rfind(basic_string_view(s), pos). Complexity is O(size() * Traits::length(s)) at worst.

Parameters
  • s – Pointer to a null-terminated character string to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character of the found substring, or npos if no such substring is found.

inline constexpr size_type find_first_of(basic_string_view v, size_type pos = 0) const noexcept

Find first occurrence of characters.

Finds the first occurrence of any of the characters of v in this view, starting as position pos. Complexity is O(size() * v.size()) at worst.

Parameters
  • v – View to search for.

  • pos – Position at which to start the search.

Returns

Position of the first occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_first_of(CharT ch, size_type pos = 0) const noexcept

Find first occurrence of characters.

Equivalent to find_first_of(basic_string_view(std::addressof(ch), 1), pos). Complexity is O(size()) at worst.

Parameters
  • ch – Character to search for.

  • pos – Position at which to start the search.

Returns

Position of the first occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_first_of(const CharT *s, size_type pos, size_type count) const

Find first occurrence of characters.

Equivalent to find_first_of(basic_string_view(s, count), pos). Complexity is O(size() * count) at worst.

Parameters
  • s – Pointer to a string of characters to search for.

  • pos – Position at which to start the search.

  • count – Length of the string of characters to search for.

Returns

Position of the first occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_first_of(const CharT *s, size_type pos = 0) const

Find first occurrence of characters.

Equivalent to find_first_of(basic_string_view(s, Traits::length(s)), pos). Complexity is O(size() * count) at worst.

Parameters
  • s – Pointer to a null-terminated string of characters to search for.

  • pos – Position at which to start the search.

Returns

Position of the first occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_last_of(basic_string_view v, size_type pos = npos) const noexcept

Find last occurrence of characters.

Finds the last character equal to one of characters in the given character sequence. Exact search algorithm is not specified. The search considers only the interval [0, pos]. If the character is not present in the interval, npos will be returned. Complexity is O(size() * v.size()) at worst.

Parameters
  • v – View to search for.

  • pos – Position at which the search is to finish.

Returns

Position of the last occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_last_of(CharT ch, size_type pos = npos) const noexcept

Find last occurrence of characters.

Finds the last character equal to one of characters in the given character sequence. Exact search algorithm is not specified. The search considers only the interval [0, pos]. If the character is not present in the interval, npos will be returned. Equivalent to find_last_of(basic_string_view(std::addressof(ch), 1), pos). Complexity is O(size()) at worst.

Parameters
  • ch – Character to search for.

  • pos – Position at which the search is to finish.

Returns

Position of the last occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_last_of(const CharT *s, size_type pos, size_type count) const

Find last occurrence of characters.

Finds the last character equal to one of characters in the given character sequence. Exact search algorithm is not specified. The search considers only the interval [0, pos]. If the character is not present in the interval, npos will be returned. Equivalent to find_last_of(basic_string_view(s, count), pos). Complexity is O(size() * count) at worst.

Parameters
  • s – Pointer to a string of characters to search for.

  • pos – Position at which the search is to finish.

  • count – Length of the string of characters to search for.

Returns

Position of the last occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_last_of(const CharT *s, size_type pos = npos) const

Find last occurrence of characters.

Finds the last character equal to one of characters in the given character sequence. Exact search algorithm is not specified. The search considers only the interval [0, pos]. If the character is not present in the interval, npos will be returned. Equivalent to find_last_of(basic_string_view(s), pos). Complexity is O(size() * Traits::length(s)) at worst.

Parameters
  • s – Pointer to a null-terminated string of characters to search for.

  • pos – Position at which the search is to finish.

Returns

Position of the last occurrence of any character of the substring, or npos if no such character is found.

inline constexpr size_type find_first_not_of(basic_string_view v, size_type pos = 0) const noexcept

Find first absence of characters.

Finds the first character not equal to any of the characters in the given character sequence. Complexity is O(size() * v.size()) at worst.

Parameters
  • v – View to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character not equal to any of the characters in the given string, or npos if no such character is found.

inline constexpr size_type find_first_not_of(CharT ch, size_type pos = 0) const noexcept

Find first absence of characters.

Finds the first character not equal to any of the characters in the given character sequence. Equivalent to find_first_not_of(basic_string_view(std::addressof(ch), 1), pos). Complexity is O(size()) at worst.

Parameters
  • ch – Character to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character not equal to any of the characters in the given string, or npos if no such character is found.

inline constexpr size_type find_first_not_of(const CharT *s, size_type pos, size_type count) const

Find first absence of characters.

Finds the first character not equal to any of the characters in the given character sequence. Equivalent to find_first_not_of(basic_string_view(s, count), pos). Complexity is O(size() * count) at worst.

Parameters
  • s – Pointer to a string of characters to search for.

  • pos – Position at which to start the search.

  • count – Length of the string of characters to compare.

Returns

Position of the first character not equal to any of the characters in the given string, or npos if no such character is found.

inline constexpr size_type find_first_not_of(const CharT *s, size_type pos = 0) const

Find first absence of characters.

Finds the first character not equal to any of the characters in the given character sequence. Equivalent to find_first_not_of(basic_string_view(s), pos). Complexity is O(size() * Traits::length(s)) at worst.

Parameters
  • s – Pointer to a null-terminated string of characters to search for.

  • pos – Position at which to start the search.

Returns

Position of the first character not equal to any of the characters in the given string, or npos if no such character is found.

inline constexpr size_type find_last_not_of(basic_string_view v, size_type pos = 0) const noexcept

Find last absence of characters.

Finds the last character not equal to any of the characters in the given character sequence. The search considers only the interval [0, pos]. Complexity is O(size() * v.size()) at worst.

Parameters
  • v – View to search for.

  • pos – Position at which to start the search.

Returns

Position of the last character not equal to any of the characters in the given string, or npos if no such character is found.

inline constexpr size_type find_last_not_of(CharT ch, size_type pos = 0) const noexcept

Find last absence of characters.

Finds the last character not equal to any of the characters in the given character sequence. The search considers only the interval [0, pos]. Equivalent to find_last_not_of(basic_string_view(std::addressof(ch), 1), pos), Complexity is O(size()) at worst.

Parameters
  • ch – Character to search for.

  • pos – Position at which to start the search.

Returns

Position of the last character not equal to any of the characters in the given string, or npos if no such character is found.

inline constexpr size_type find_last_not_of(const CharT *s, size_type pos, size_type count) const

Find last absence of characters.

Finds the last character not equal to any of the characters in the given character sequence. The search considers only the interval [0, pos]. Equivalent to find_last_not_of(basic_string_view(s, count), pos), Complexity is O(size() * count) at worst.

Parameters
  • s – Pointer to a string of characters to compare.

  • pos – Position at which to start the search.

  • count – Length of the string of characters to compare.

Returns

Position of the last character not equal to any of the characters in the given string, or npos if no such character is found.

inline constexpr size_type find_last_not_of(const CharT *s, size_type pos = 0) const

Find last absence of characters.

Finds the last character not equal to any of the characters in the given character sequence. The search considers only the interval [0, pos]. Equivalent to find_last_not_of(basic_string_view(s), pos), Complexity is O(size() * Traits::length(s)) at worst.

Parameters
  • s – Pointer to a null-terminated string of characters to compare.

  • pos – Position at which to start the search.

Returns

Position of the last character not equal to any of the characters in the given string, or npos if no such character is found.

Public Static Attributes

static constexpr size_type npos = size_type(-1)

Special value. The exact meaning depends on the context.