#include <muu/fixed_string.h>
template <typename Character, size_t Length>
fixed_string class
A null-terminated string for compile-time string manipulation.
Template parameters | |
---|---|
Character | The character type of the string. |
Length | The length of the string (not including the null terminator). |
Public types
-
using const_iterator = const_
pointer - Iterator type used when iterating over the string.
- using const_pointer = const Character*
- Constant pointer to one of the string's characters.
- using const_reference = const Character&
- Constant reference to one of the string's characters.
-
using const_reverse_iterator = std::
reverse_iterator<const_ iterator> - Iterator type used when reverse-iterating over the string.
- using difference_type = ptrdiff_t
- Difference type used by iterators.
- using iterator = pointer
- Iterator type used when iterating over the string.
- using pointer = Character*
- Pointer to one of the string's characters.
- using reference = Character&
- Reference to one of the string's characters.
-
using reverse_iterator = std::
reverse_iterator<iterator> - Iterator type used when reverse-iterating over the string.
- using size_type = size_t
- Size/index type.
-
using traits_type = std::
char_traits<Character> - Traits type describing the string's character type.
- using value_type = Character
- Character type used in the string.
-
using view_type = std::
basic_string_view<Character> - The string view type corresponding to this string's character type.
Public static variables
- static size_t string_length constexpr
- The length of the string (not including the null-terminator).
Constructors, destructors, conversion operators
- fixed_string() defaulted noexcept
- Default constructor. Characters are not initialized.
-
fixed_string(const fixed_
string&) defaulted constexpr noexcept - Copy constructor.
-
template <size_t N>fixed_string(const value_
type(&arr)[N]) explicit constexpr noexcept - Constructs a string from a character array.
-
fixed_string(std::
string_view str) explicit constexpr noexcept - Constructs a string from a std::
string_view. -
template <size_t Len>fixed_string(const fixed_
string<value_ type, Len>& str) explicit constexpr noexcept - Lengthening/truncating constructor.
-
fixed_string(value_
type fill) explicit constexpr noexcept - Constructs a string with each character equal to the given value.
-
template <size_t Len>fixed_string(const value_
type* str, index_ tag<Len>) explicit constexpr noexcept - Constructs a string from a raw c-string pointer and integral length constant.
Public functions
-
auto operator=(const fixed_
string&) -> fixed_ string& defaulted constexpr noexcept - Copy-assigment operator.
Characters
- auto back() -> reference constexpr noexcept
- Returns a reference to the last character in the string.
-
auto back() const -> const_
reference constexpr noexcept - Returns a const reference to the last character in the string.
-
auto c_str() const -> const_
pointer constexpr noexcept - Returns a const pointer to the first character in the string.
- auto data() -> pointer constexpr noexcept
- Returns a pointer to the first character in the string.
-
auto data() const -> const_
pointer constexpr noexcept - Returns a const pointer to the first character in the string.
- auto front() -> reference constexpr noexcept
- Returns a reference to the first character in the string.
-
auto front() const -> const_
reference constexpr noexcept - Returns a const reference to the first character in the string.
-
template <size_t Index>auto get() -> reference constexpr noexcept
- Returns a reference to the character at the given index.
-
template <size_t Index>auto get() const -> const_
reference constexpr noexcept - Returns a const reference to the character at the given index.
-
auto operator[](size_
type idx) -> reference constexpr noexcept - Returns a reference to the character at the given index.
-
auto operator[](size_
type idx) const -> const_ reference constexpr noexcept - Returns a const reference to the character at the given index.
Concatenation
-
template <size_t Len>auto operator+(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> fixed_ string<value_ type, Length+Len> constexpr noexcept - Concatenates two static strings.
-
auto operator+(const fixed_
string& lhs, value_ type rhs) -> fixed_ string<value_ type, Length+1> constexpr noexcept - Concatenates a static string and a single character.
-
auto operator+(value_
type lhs, const fixed_ string& rhs) -> fixed_ string<value_ type, Length+1> constexpr noexcept - Concatenates a static string and a single character.
Equality and Comparison
-
template <size_t Len>static auto compare(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> int constexpr noexcept - Returns the lexicographical ordering of two strings.
-
template <size_t Len>auto compare(const fixed_
string<value_ type, Len>& rhs) -> int constexpr noexcept - Returns the lexicographical ordering of this string with respect to another.
-
template <size_t Len>auto operator!=(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> bool constexpr noexcept - Returns true if two strings do not contain the same character sequence.
-
template <size_t Len>auto operator<(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> bool constexpr noexcept - Returns true if the LHS is lexicographically ordered before the RHS.
-
template <size_t Len>auto operator<=(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> bool constexpr noexcept - Returns true if the LHS is lexicographically ordered before or equal to the RHS.
-
template <size_t Len>auto operator==(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> bool constexpr noexcept - Returns true if two strings contain the same character sequence.
-
template <size_t Len>auto operator>(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> bool constexpr noexcept - Returns true if the LHS is lexicographically ordered after than the RHS.
-
template <size_t Len>auto operator>=(const fixed_
string& lhs, const fixed_ string<value_ type, Len>& rhs) -> bool constexpr noexcept - Returns true if the LHS is lexicographically ordered after or equal to the RHS.
Iterators
- auto begin() -> iterator constexpr noexcept
- Returns an iterator to the first character in the string.
-
auto begin() const -> const_
iterator constexpr noexcept - Returns a const iterator to the first character in the string.
-
auto cbegin() const -> const_
iterator constexpr noexcept - Returns a const iterator to the first character in the string.
-
auto cend() const -> const_
iterator constexpr noexcept - Returns a const iterator to the one-past-the-last character in the string.
-
auto crbegin() const -> const_
reverse_ iterator constexpr noexcept - Returns a const reverse iterator to the last character in the string.
-
auto crend() const -> const_
reverse_ iterator constexpr noexcept - Returns a const reverse iterator to one-before-the-first character in the string.
- auto end() -> iterator constexpr noexcept
- Returns an iterator to the one-past-the-last character in the string.
-
auto end() const -> const_
iterator constexpr noexcept - Returns a const iterator to the one-past-the-last character in the string.
-
auto rbegin() -> reverse_
iterator constexpr noexcept - Returns a reverse iterator to the last character in the string.
-
auto rbegin() const -> const_
reverse_ iterator constexpr noexcept - Returns a const reverse iterator to the last character in the string.
-
auto rend() -> reverse_
iterator constexpr noexcept - Returns a reverse iterator to one-before-the-first character in the string.
-
auto rend() const -> const_
reverse_ iterator constexpr noexcept - Returns a const reverse iterator to one-before-the-first character in the string.
Iterators (ADL)
-
auto begin(fixed_
string& s) -> iterator constexpr noexcept - Returns an iterator to the first character in a string.
-
auto begin(const fixed_
string& s) -> const_ iterator constexpr noexcept - Returns a const iterator to the first character in a string.
-
auto cbegin(const fixed_
string& s) -> const_ iterator constexpr noexcept - Returns a const iterator to the first character in a string.
-
auto cend(const fixed_
string& s) -> const_ iterator constexpr noexcept - Returns a const iterator to the one-past-the-last character in a string.
-
auto crbegin(const fixed_
string& s) -> const_ reverse_ iterator constexpr noexcept - Returns a const reverse iterator to the last character in a string.
-
auto crend(const fixed_
string& s) -> const_ reverse_ iterator constexpr noexcept - Returns a const reverse iterator to one-before-the-first character in a string.
-
auto end(fixed_
string& s) -> iterator constexpr noexcept - Returns an iterator to the one-past-the-last character in a string.
-
auto end(const fixed_
string& s) -> const_ iterator constexpr noexcept - Returns a const iterator to the one-past-the-last character in a string.
-
auto rbegin(fixed_
string& s) -> reverse_ iterator constexpr noexcept - Returns a reverse iterator to the last character in a string.
-
auto rbegin(const fixed_
string& s) -> const_ reverse_ iterator constexpr noexcept - Returns a const reverse iterator to the last character in a string.
-
auto rend(fixed_
string& s) -> reverse_ iterator constexpr noexcept - Returns a reverse iterator to one-before-the-first character in a string.
-
auto rend(const fixed_
string& s) -> const_ reverse_ iterator constexpr noexcept - Returns a const reverse iterator to one-before-the-first character in a string.
Length
- auto empty() const -> bool constexpr noexcept
- Returns true if the string is empty.
-
auto length() const -> size_
type constexpr noexcept - Returns the number of characters in the string (not including the null-terminator).
- operator bool() const explicit constexpr noexcept
- Returns true if the string is not empty.
-
auto size() const -> size_
type constexpr noexcept - Returns the number of characters in the string (not including the null-terminator).
Substrings
-
template <auto Start, auto End = static_cast<size_t>(-1)>auto slice() const -> auto constexpr noexcept
- Returns a substring.
-
template <auto Start, size_t Len = static_cast<size_t>(-1)>auto substr() const -> auto constexpr noexcept
- Returns a substring.
Views
- operator view_type() const constexpr noexcept
- Returns a view of the string (not including the null terminator).
-
auto view() const -> view_
type constexpr noexcept - Returns a view of the string (not including the null terminator).
Friends
-
template <typename Char, typename Traits>auto operator<<(std::
basic_ostream<Char, Traits>& os, const fixed_ string& str) -> std:: basic_ostream<Char, Traits>& - Writes the string out to a text stream.
Function documentation
muu::fixed_string::fixed_string(const value_ type(&arr)[N]) explicit constexpr noexcept
Constructs a string from a character array.
Any extra characters not covered by the input argument are zero-initialized.
template <typename Character, size_t Length>
muu::fixed_string::fixed_string(std:: string_view str) explicit constexpr noexcept
Constructs a string from a std::
Any extra characters not covered by the input argument are zero-initialized.
muu::fixed_string::fixed_string(const fixed_ string<value_ type, Len>& str) explicit constexpr noexcept
Lengthening/truncating constructor.
Any extra characters not covered by the input argument are zero-initialized.
template <typename Character, size_t Length>
const_ reference muu::fixed_string::back() const constexpr noexcept
Returns a const reference to the last character in the string.
template <typename Character, size_t Length>
const_ reference muu::fixed_string::front() const constexpr noexcept
Returns a const reference to the first character in the string.
template <typename Character, size_t Length>
template <auto Start, auto End = static_cast<size_t>(-1)>
auto muu::fixed_string::slice() const constexpr noexcept
template <auto Start, auto End = static_cast<size_t>(-1)>
Returns a substring.
Template parameters | |
---|---|
Start | Starting index of the substring, inclusive. Negative values mean "this many characters from the end". |
End | Ending index of the substring, exclusive. Negative values mean "this many characters from the end". |
Returns | A string containing the substring from the given range. If the source string literal was empty, the range was empty, or they do not intersect, an zero-length string is returned. |
template <typename Character, size_t Length>
template <auto Start, size_t Len = static_cast<size_t>(-1)>
auto muu::fixed_string::substr() const constexpr noexcept
template <auto Start, size_t Len = static_cast<size_t>(-1)>
Returns a substring.
Template parameters | |
---|---|
Start | Starting index of the substring. Negative values mean "this many characters from the end". |
Len | Length of the substring. |
Returns | A string containing the substring from the given range. If the source string literal was empty, the range was empty, or they do not intersect, an zero-length string literal is returned. |
auto operator""_fs() consteval
Constructs a fixed_
constexpr auto str = "hello"_fs; static_assert(str == fixed_string{ "hello" })