24#include "pw_assert/assert.h"
25#include "pw_polyfill/language_feature_macros.h"
26#include "pw_result/result.h"
27#include "pw_span/span.h"
28#include "pw_status/status.h"
29#include "pw_status/status_with_size.h"
30#include "pw_string/internal/length.h"
40 return StatusWithSize::ResourceExhausted();
43 const size_t copied = source.copy(dest.data(), dest.size() - 1);
46 return StatusWithSize(
61 return std::string_view(str.data(),
62 internal::ClampedLength(str.data(), str.size()));
65constexpr std::string_view
ClampedCString(
const char* str,
size_t max_len) {
82 PW_DASSERT(str.data() !=
nullptr);
84 const size_t length = internal::ClampedLength(str.data(), str.size());
85 if (length == str.size()) {
109template <
typename Span>
113 !std::is_base_of_v<InlineString<>, std::decay_t<Span>>,
114 "Do not use pw::string::Copy() with pw::InlineString<>. Instead, use "
115 "pw::InlineString<>'s assignment operator or assign() function, or "
116 "pw::string::Append().");
117 return internal::CopyToSpan(source, std::forward<Span>(dest));
120template <
typename Span>
122 PW_DASSERT(source !=
nullptr);
124 std::forward<Span>(dest));
130 return Copy(source, span<char>(dest, num));
143 const size_t chars_copied =
144 std::min(view.size(),
static_cast<size_t>(
string.capacity()));
145 string.assign(view, 0,
static_cast<string_impl::size_type
>(chars_copied));
150 PW_DASSERT(c_string !=
nullptr);
164 const size_t chars_copied = std::min(
165 view.size(),
static_cast<size_t>(
string.capacity() -
string.size()));
166 string.append(view, 0,
static_cast<string_impl::size_type
>(chars_copied));
171 PW_DASSERT(c_string !=
nullptr);
184 for (
size_t i = 0; i < copy_result.
size(); i++) {
185 dest[i] = std::isprint(dest[i]) ? dest[i] :
'.';
pw::InlineBasicString is a fixed-capacity version of std::basic_string. In brief:
Definition: string.h:68
static constexpr Status OutOfRange()
Definition: status.h:267
static constexpr Status ResourceExhausted()
Definition: status.h:230
Definition: status_with_size.h:51
constexpr size_t size() const
Definition: status_with_size.h:148
Definition: span_impl.h:235
#define PW_CONSTEXPR_CPP20
Definition: language_feature_macros.h:27
constexpr Status OkStatus()
Definition: status.h:450
Status Append(InlineString<> &string, std::string_view view)
Definition: util.h:163
StatusWithSize Copy(std::string_view source, Span &&dest)
pw::string::Copy is a safer alternative to std::strncpy as it always null-terminates whenever the des...
Definition: util.h:110
Status Assign(InlineString<> &string, std::string_view view)
Definition: util.h:142
constexpr std::string_view ClampedCString(span< const char > str)
Safe alternative to the string_view constructor that avoids the risk of an unbounded implicit or expl...
Definition: util.h:60
StatusWithSize PrintableCopy(std::string_view source, span< char > dest)
Provides a safe, printable copy of a string.
Definition: util.h:181
constexpr Result< size_t > NullTerminatedLength(span< const char > str)
pw::string::NullTerminatedLength is a safer alternative to strlen for calculating the null-terminated...
Definition: util.h:81
The Pigweed namespace.
Definition: alignment.h:27
pw::InlineBasicString and pw::InlineString are safer alternatives to std::basic_string and std::strin...