29#include "pw_preprocessor/compiler.h"
30#include "pw_span/span.h"
31#include "pw_status/status.h"
32#include "pw_status/status_with_size.h"
34#include "pw_string/to_string.h"
91 : buffer_(buffer), size_(&inline_size_), inline_size_(0) {
97 {
reinterpret_cast<char*
>(buffer.data()), buffer.size_bytes()}) {}
100 : buffer_(string.data(), string.
max_size() + 1),
101 size_(&string.length_),
114 const char* data()
const {
return buffer_.data(); }
115 const char*
c_str()
const {
return data(); }
120 std::string_view
view()
const {
return std::string_view(data(),
size()); }
124 operator std::string_view()
const {
return view(); }
129 return span(
reinterpret_cast<const std::byte*
>(buffer_.data()),
size());
167 size_t size()
const {
return *size_; }
170 size_t max_size()
const {
return buffer_.empty() ? 0u : buffer_.size() - 1; }
177 status_ =
static_cast<unsigned char>(
OkStatus().
code());
178 last_status_ =
static_cast<unsigned char>(
OkStatus().
code());
219 size_t count = std::string_view::npos);
223 template <
typename T>
227 if constexpr (std::is_convertible_v<T, std::string_view>) {
229 }
else if constexpr (std::is_convertible_v<T, span<const std::byte>>) {
232 HandleStatusWithSize(ToString(value, buffer_.subspan(
size())));
239 return append(value ?
"true" :
"false");
248 return append(string::kNullPointerString);
282 size_(&inline_size_),
283 inline_size_(*other.size_),
284 status_(other.status_),
285 last_status_(other.last_status_) {}
291 static constexpr unsigned char StatusCode(
Status status) {
292 return static_cast<unsigned char>(
status.
code());
295 void WriteBytes(span<const std::byte> data);
297 size_t ResizeAndTerminate(
size_t chars_to_append);
299 void HandleStatusWithSize(StatusWithSize written);
301 constexpr void NullTerminate() {
302 if (!buffer_.empty()) {
303 buffer_[
size()] =
'\0';
307 void SetErrorStatus(Status
status);
309 const span<char> buffer_;
311 InlineString<>::size_type* size_;
316 InlineString<>::size_type inline_size_;
317 unsigned char status_ = StatusCode(
OkStatus());
318 unsigned char last_status_ = StatusCode(
OkStatus());
330template <
size_t kSizeBytes>
341 template <
size_t kOtherSizeBytes>
345 "A StringBuffer cannot be copied into a smaller buffer");
349 template <
size_t kOtherSizeBytes>
351 assign<kOtherSizeBytes>(other);
356 assign<kSizeBytes>(other);
360 template <
size_t kOtherSizeBytes>
363 "A StringBuffer cannot be copied into a smaller buffer");
364 CopySizeAndStatus(other);
376 static constexpr size_t max_size() {
return kSizeBytes - 1; }
380 template <
typename... Args>
386 template <
typename T>
387 StringBuffer& operator<<(T&& value) {
388 static_cast<StringBuilder&
>(*this) << std::forward<T>(value);
393 template <
size_t kOtherSize>
394 void CopyContents(
const StringBuffer<kOtherSize>& other) {
395 std::memcpy(buffer_, other.data(), other.size() + 1);
398 static_assert(kSizeBytes >= 1u,
"StringBuffers must be at least 1 byte long");
399 char buffer_[kSizeBytes];
402namespace string_internal {
410inline constexpr size_t kDefaultMinimumStringBufferSize = 24;
415inline constexpr size_t kDefaultArgumentSize = 4;
419constexpr size_t ArgLength() {
420 using Arg = std::remove_reference_t<T>;
423 if constexpr (std::is_array_v<Arg>) {
424 using Element = std::remove_reference_t<decltype(std::declval<Arg>()[0])>;
426 if constexpr (std::is_same_v<Element, const char>) {
427 return std::extent_v<Arg> > 0u ? std::extent_v<Arg> - 1 : size_t(0);
431 return kDefaultArgumentSize;
435template <
typename... Args>
436constexpr size_t DefaultStringBufferSize() {
437 return std::max((
size_t(1) + ... + ArgLength<Args>()),
438 kDefaultMinimumStringBufferSize);
444template <
size_t kBufferSize,
typename... Args>
445auto InitializeStringBuffer(
const Args&... args) {
446 return (StringBuffer<kBufferSize>() << ... << args);
472template <
size_t kBufferSize = 0u,
typename... Args>
473auto MakeString(Args&&... args) {
474 constexpr size_t kSize =
475 kBufferSize == 0u ? string_internal::DefaultStringBufferSize<Args...>()
477 return string_internal::InitializeStringBuffer<kSize>(args...);
constexpr Code code() const
Returns the Status::Code (pw_Status) for this Status.
Definition: status.h:152
constexpr bool ok() const
Definition: status.h:157
const char * str() const
Returns a null-terminated string representation of the Status.
Definition: status.h:225
Definition: status_with_size.h:49
Definition: string_builder.h:331
StringBuffer(StringBuffer &&other)=delete
StringBuffers are not movable: the underlying data must be copied.
StringBuffer & operator=(StringBuffer &&other)=delete
StringBuffers are not movable: the underlying data must be copied.
Definition: string_builder.h:87
StringBuilder & Format(const char *format,...)
bool empty() const
True if the string is empty.
Definition: string_builder.h:164
size_t size() const
Returns the current length of the string, excluding the null terminator.
Definition: string_builder.h:167
void push_back(char ch)
Definition: string_builder.h:183
StringBuilder & operator<<(const T &value)
Definition: string_builder.h:224
Status last_status() const
The status from the last operation. May be OK while status() is not OK.
Definition: string_builder.h:158
void clear_status()
Sets the statuses to OkStatus();.
Definition: string_builder.h:176
void resize(size_t new_size)
Status status() const
Definition: string_builder.h:150
span< const std::byte > as_bytes() const
Definition: string_builder.h:128
StringBuilder & append(size_t count, char ch)
Appends the provided character count times.
void clear()
Clears the string and resets its error state.
StatusWithSize status_with_size() const
Returns status() and size() as a StatusWithSize.
Definition: string_builder.h:153
size_t max_size() const
Returns the maximum length of the string, excluding the null terminator.
Definition: string_builder.h:170
StringBuilder & append(std::string_view str, size_t pos, size_t count=std::string_view::npos)
StringBuilder & FormatVaList(const char *format, va_list args)
void pop_back()
Definition: string_builder.h:187
StringBuilder & operator<<(bool value)
Provide a few additional operator<< overloads that reduce code size.
Definition: string_builder.h:238
constexpr StringBuilder(span< char > buffer)
Creates an empty pw::StringBuilder.
Definition: string_builder.h:90
const char * c_str() const
Definition: string_builder.h:115
bool ok() const
True if status() is OkStatus().
Definition: string_builder.h:161
StringBuilder & append(const char *str, size_t count)
StringBuilder(const StringBuilder &)=delete
StringBuilder & append(const char *str)
StringBuilder & append(std::string_view str)
Appends a std::string_view to the end of the StringBuilder.
std::string_view view() const
Definition: string_builder.h:120
#define PW_PRINTF_FORMAT(format_index, parameter_index)
Definition: compiler.h:86
#define PW_NO_SANITIZE(check)
Definition: compiler.h:155
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27
constexpr Status OkStatus()
Definition: status.h:234
pw::InlineBasicString and pw::InlineString are safer alternatives to std::basic_string and std::strin...