Class String

Class Documentation

class String
[source]

Minimal string class used throughout the mutiny framework.

Provides just enough string functionality for assertion messages and internal bookkeeping without depending on the C++ standard library. The interface deliberately mirrors the subset of std::string that the framework uses so that switching to std::string via MUTINY_USE_STD_STRING requires no call-site changes.

Public Functions

String(const char *value = "")

Construct from a NUL-terminated C string (default: empty).

String(const char *value, size_t len)

Construct from a pointer and an explicit byte count.

explicit String(StringView value)

Construct a copy from a StringView (explicit; allocates).

String(size_t count, char ch)

Construct a string of count copies of character ch.

Parameters:
  • count – Number of characters.

  • ch – Character to fill with.

String(const String &other)

Copy constructor.

String(String &&other) noexcept

Move constructor.

~String()
[source]
String &operator=(const String &other)

Copy-assignment operator.

String &operator=(String &&other) noexcept

Move-assignment operator.

String operator+(const String &rhs) const

Return the concatenation of this string and rhs.

String &operator+=(const String &rhs)

Append rhs and return a reference to this string.

String &operator+=(const char *rhs)

Append a C string and return a reference to this string.

String &operator+=(char ch)

Append a single character and return a reference to this string.

String &operator+=(StringView sv)

Append the contents of a StringView.

inline char &operator[](size_t pos)
Returns:

Reference to the character at position pos.

inline const char &operator[](size_t pos) const
Returns:

Const reference to the character at position pos.

size_t find(char ch, size_t pos = 0) const

Find the first occurrence of ch at or after pos.

Parameters:
  • ch – Character to search for.

  • pos – Starting position for the search.

Returns:

Index of the first match, or npos if not found.

size_t find(const char *s, size_t pos = 0) const

Find the first occurrence of the substring s at or after pos.

Parameters:
  • s – Substring to search for.

  • pos – Starting position for the search.

Returns:

Index of the first match, or npos if not found.

String substr(size_t begin_pos) const
Returns:

Substring from begin_pos to end-of-string.

String substr(size_t begin_pos, size_t amount) const
Returns:

Substring of at most amount characters starting at begin_pos.

const char *c_str() const
Returns:

NUL-terminated pointer to the string’s character data.

const char *data() const
Returns:

Read-only pointer to the character data.

char *data()
[source]
Returns:

Writable pointer to the character data.

size_t size() const
Returns:

Number of characters (not counting the NUL terminator).

inline size_t length() const
Returns:

Number of characters; synonym for size().

inline size_t capacity() const
Returns:

Number of characters that fit without reallocation.

bool empty() const
Returns:

true if the string has zero characters.

void clear()
[source]

Reset the string to empty without releasing the buffer.

void reserve(size_t new_capacity)

Ensure at least new_capacity characters fit without reallocation.

void resize(size_t new_size)

Resize the string to exactly new_size characters, padding with NUL bytes if growing.

Public Static Attributes

static size_t npos = static_cast<size_t>(-1)

Sentinel value returned by find() when no match is found.

Friends

friend bool operator<(const String &left, const String &right)