API Reference#
Overview#
Pigweed AI summary: This module provides two types of strings and utility functions for working with strings. The pw::StringBuilder facilitates creating formatted strings in a fixed-sized buffer or in a pw::InlineString. The pw::InlineBasicString and pw::InlineString are safer alternatives to std::basic_string and std::string. The pw::string::* functions provide safer alternatives to C++ standard library string functions.
This module provides two types of strings, and utility functions for working with strings.
pw::StringBuilder
pw::StringBuilder facilitates creating formatted strings in a fixed-sized buffer or in a pw::InlineString. It is designed to give the flexibility of std::ostringstream, but with a small footprint.
pw::InlineString
pw::InlineBasicString and pw::InlineString are safer alternatives to std::basic_string and std::string.
String utility functions
The pw::string::* functions provide safer alternatives to C++ standard library string functions.
pw::StringBuilder#
pw::StringBuilder facilitates creating formatted strings in a fixed-sized buffer or in a pw::InlineString. It is designed to give the flexibility of std::ostringstream, but with a small footprint.
-
class StringBuilder#
pw::StringBuilderinstances are always null-terminated (unless they are constructed with an empty buffer) and never overflow. Status is tracked for each operation and an overall status is maintained, which reflects the most recent error.pw::StringBuilderdoes not own the buffer it writes to. It can be used to write strings to any buffer. Thepw::StringBuffertemplate class, defined below, allocates a buffer alongside apw::StringBuilder.pw::StringBuildersupports C++-style<<output, similar tostd::ostringstream. It also supports append functions likestd::stringandprintf-style output.Support for custom types is added by overloading
operator<<in the same namespace as the custom type. For example:namespace my_project { struct MyType { int foo; const char* bar; }; pw::StringBuilder& operator<<(pw::StringBuilder& sb, const MyType& value) { return sb << "MyType(" << value.foo << ", " << value.bar << ')'; } } // namespace my_project
The
ToStringtemplate function can be specialized to support custom types withpw::StringBuilder, though overloadingoperator<<is generally preferred. For example:namespace pw { template <> StatusWithSize ToString<MyStatus>(MyStatus value, span<char> buffer) { return Copy(MyStatusString(value), buffer); } } // namespace pw
Subclassed by pw::StringBuffer< kSizeBytes >
Public Functions
-
inline explicit constexpr StringBuilder(span<char> buffer)#
Creates an empty
pw::StringBuilder.
-
StringBuilder(const StringBuilder&) = delete#
Disallow copy/assign to avoid confusion about where the string is actually stored.
pw::StringBufferinstances may be copied into one another.
-
inline const char *c_str() const#
Returns the contents of the string buffer. Always null-terminated.
-
inline std::string_view view() const#
Returns a
std::string_viewof the contents of thispw::StringBuilder. Thestd::string_viewis invalidated if thepw::StringBuildercontents change.
-
inline operator std::string_view() const#
Allow implicit conversions to
std::string_viewsopw::StringBuilderinstances can be passed into functions that take astd::string_view.
-
inline span<const std::byte> as_bytes() const#
Returns a
span<const std::byte>representation of thispw::StringBuffer.
-
inline Status status() const#
Returns the status of
pw::StringBuilder, which reflects the most recent error that occurred while updating the string. After an update fails, the status remains non-OK until it is cleared withpw::StringBuilder::clear()orpw::StringBuilder::clear_status().- Returns:
OKif no errors have occurred;RESOURCE_EXHAUSTEDif output to theStringBuilderwas truncated;INVALID_ARGUMENTifprintf-style formatting failed;OUT_OF_RANGEif an operation outside the buffer was attempted.
-
inline Status last_status() const#
The status from the last operation. May be OK while
status()is not OK.
-
inline bool empty() const#
True if the string is empty.
-
inline size_t size() const#
Returns the current length of the string, excluding the null terminator.
-
inline size_t max_size() const#
Returns the maximum length of the string, excluding the null terminator.
-
void clear()#
Clears the string and resets its error state.
-
inline void clear_status()#
Sets the statuses to
OkStatus();.
-
inline void push_back(char ch)#
Appends a single character. Sets the status to
RESOURCE_EXHAUSTEDif the character cannot be added because the buffer is full.
-
inline void pop_back() PW_NO_SANITIZE("unsigned-integer-overflow")#
Removes the last character. Sets the status to
OUT_OF_RANGEif the buffer is empty (in which case the unsigned overflow is intentional).
-
StringBuilder &append(size_t count, char ch)#
Appends the provided character
counttimes.
-
StringBuilder &append(const char *str, size_t count)#
Appends
countcharacters fromstrto the end of theStringBuilder. If count exceeds the remaining space in theStringBuffer,max_size() - size()characters are appended and the status is set toRESOURCE_EXHAUSTED.stris not considered null-terminated and may contain null characters.
-
StringBuilder &append(const char *str)#
Appends characters from the null-terminated string to the end of the
StringBuilder. If the string’s length exceeds the remaining space in the buffer,max_size() - size()characters are copied and the status is set toRESOURCE_EXHAUSTED.This function uses
string::Lengthinstead ofstd::strlento avoid unbounded reads if the string is not null-terminated.
-
StringBuilder &append(const std::string_view &str)#
Appends a
std::string_viewto the end of theStringBuilder.
-
StringBuilder &append(const std::string_view &str, size_t pos, size_t count = std::string_view::npos)#
Appends a substring from the
std::string_viewto theStringBuilder. Copies up to count characters starting fromposto the end of theStringBuilder. Ifpos > str.size(), sets the status toOUT_OF_RANGE.
-
template<typename T>
inline StringBuilder &operator<<(const T &value)# Appends to the end of the
StringBuilderusing the<<operator. This enables C++ stream-style formatted toStringBuilderinstances.
-
inline StringBuilder &operator<<(bool value)#
Provide a few additional
operator<<overloads that reduce code size.
-
StringBuilder &Format(const char *format, ...)#
Appends a
printf-style string to the end of theStringBuilder. If the formatted string does not fit, the results are truncated and the status is set toRESOURCE_EXHAUSTED.- return:
Note
Internally, calls
string::Format, which callsstd::vsnprintf.- Parameters:
format – The format string
... – Arguments for format specification
-
StringBuilder &FormatVaList(const char *format, va_list args)#
Appends a
vsnprintf-style string withva_listarguments to the end of theStringBuilder. If the formatted string does not fit, the results are truncated and the status is set toRESOURCE_EXHAUSTED.Note
Internally, calls
string::Format, which callsstd::vsnprintf.
-
void resize(size_t new_size)#
Sets the size of the
StringBuilder. This function only truncates; ifnew_size > size(), it sets status toOUT_OF_RANGEand does nothing.
-
inline explicit constexpr StringBuilder(span<char> buffer)#
pw::InlineString#
-
template<typename T, size_t kCapacity = string_impl::kGeneric>
class InlineBasicString# pw::InlineBasicStringis a fixed-capacity version ofstd::basic_string. In brief:It is C++14-compatible and null-terminated.
It stores the string contents inline and uses no dynamic memory.
It implements mostly the same API as
std::basic_string, but the capacity of the string is fixed at construction and cannot grow. Attempting to increase the size beyond the capacity triggers an assert.
pw::InlineBasicStringis efficient and compact. The current size and capacity are stored in a single word. Accessing its contents is a simple array access within the object, with no pointer indirection, even when working from a generic referencepw::InlineBasicString<T>where the capacity is not specified as a template argument. A string object can be used safely without the need to know its capacity.See also
pw::InlineString, which is an alias ofpw::InlineBasicString<char>and is equivalent tostd::string.
-
template<size_t kCapacity = string_impl::kGeneric>
using pw::InlineString = InlineBasicString<char, kCapacity># pw::InlineStringis an alias ofpw::InlineBasicString<char>and is equivalent tostd::string.
String utility functions#
pw::string::Assign()#
-
inline Status pw::string::Assign(InlineString<> &string, const std::string_view &view)#
Assigns a
std::string_viewto apw::InlineString, truncating if it does not fit. Theassign()function ofpw::InlineStringasserts if the string’s requested size exceeds its capacity;pw::string::Assign()returns aStatusinstead.- Returns:
OKif the entirestd::string_viewwas copied to the end of thepw::InlineString.RESOURCE_EXHAUSTEDif thestd::string_viewwas truncated to fit.
pw::string::Append()#
-
inline Status pw::string::Append(InlineString<> &string, const std::string_view &view)#
Appends a
std::string_viewto apw::InlineString, truncating if it does not fit. Theappend()function ofpw::InlineStringasserts if the string’s requested size exceeds its capacity;pw::string::Append()returns aStatusinstead.- Returns:
OKif the entirestd::string_viewwas assigned.RESOURCE_EXHAUSTEDif thestd::string_viewwas truncated to fit.
pw::string::ClampedCString()#
-
constexpr std::string_view pw::string::ClampedCString(span<const char> str)#
Safe alternative to the
string_viewconstructor that avoids the risk of an unbounded implicit or explicit use ofstrlen.This is strongly recommended over using something like C11’s
strnlen_sas astring_viewdoes not require null-termination.
pw::string::Copy()#
-
template<typename Span>
inline StatusWithSize pw::string::Copy(const std::string_view &source, Span &&dest)# pw::string::Copyis a safer alternative tostd::strncpyas it always null-terminates whenever the destination buffer has a non-zero size.Copies the
sourcestring to thedest, truncating if the full string does not fit. Always null terminates ifdest.size()ornumis greater than 0.- return:
the number of characters written, excluding the null terminator. If the string is truncated, the status is
RESOURCE_EXHAUSTED.
- Pre:
The destination and source shall not overlap. The source shall be a valid pointer.
It also has variants that provide a destination of pw::Vector<char>
(see pw_containers for details) that do not store the null
terminator in the vector.
pw::string::Format()#
The pw::string::Format functions are safer alternatives to std::snprintf and std::vsnprintf. The snprintf return value is awkward to interpret, and misinterpreting it can lead to serious bugs.
These functions return a pw::StatusWithSize. The pw::Status is set to reflect any errors and the return value is always the number of characters written before the null terminator.
-
StatusWithSize pw::string::Format(span<char> buffer, const char *format, ...)#
Writes a printf-style formatted string to the provided buffer, similarly to
std::snprintf().The
std::snprintf()return value is awkward to interpret, and misinterpreting it can lead to serious bugs.- Returns:
The number of characters written, excluding the null terminator. The buffer is always null-terminated unless it is empty. The status is
OkStatus()if the operation succeeded,Status::ResourceExhausted()if the buffer was too small to fit the output, orStatus::InvalidArgument()if there was a formatting error.
-
StatusWithSize pw::string::FormatVaList(span<char> buffer, const char *format, va_list args)#
Writes a printf-style formatted string with va_list-packed arguments to the provided buffer, similarly to
std::vsnprintf().- Returns:
See
pw::string::Format().
-
Status pw::string::Format(InlineString<> &string, const char *format, ...)#
Appends a printf-style formatted string to the provided
pw::InlineString, similarly tostd::snprintf().- Returns:
See
pw::string::Format().
-
Status pw::string::FormatVaList(InlineString<> &string, const char *format, va_list args)#
Appends a printf-style formatted string with va_list-packed arguments to the provided
InlineString, similarly tostd::vsnprintf().- Returns:
See
pw::string::Format().
-
Status pw::string::FormatOverwrite(InlineString<> &string, const char *format, ...)#
Writes a printf-style format string to the provided
InlineString, overwriting any contents.- Returns:
See
pw::string::Format().
-
inline Status pw::string::FormatOverwriteVaList(InlineString<> &string, const char *format, va_list args)#
Writes a printf-style formatted string with
va_list-packed arguments to the providedInlineString, overwriting any contents.- Returns:
See
pw::string::Format().
pw::string::NullTerminatedLength()#
-
constexpr Result<size_t> pw::string::NullTerminatedLength(span<const char> str)#
pw::string::NullTerminatedLengthis a safer alternative tostrlenfor calculating the null-terminated length of the string within the specified span, excluding the null terminator.Like
strnlen_sin C11, the scan for the null-terminator is bounded.- return:
the null-terminated length of the string excluding the null terminator or
OutOfRangeif the string is not null-terminated.
- Pre:
The string shall be at a valid pointer.
pw::string::PrintableCopy()#
-
inline StatusWithSize pw::string::PrintableCopy(const std::string_view &source, span<char> dest)#
Provides a safe, printable copy of a string.
Copies the
sourcestring to thedeststring with same behavior aspw::string::Copy, with the difference that any non-printable characters are changed to..