6 String manipulation is a very common operation, but the standard C and C++
7 string libraries have drawbacks. The C++ functions are easy-to-use and powerful,
8 but require too much flash and memory for many embedded projects. The C string
9 functions are lighter weight, but can be difficult to use correctly. Mishandling
10 of null terminators or buffer sizes can result in serious bugs.
12 The ``pw_string`` module provides the flexibility, ease-of-use, and safety of
13 C++-style string manipulation, but with no dynamic memory allocation and a much
14 smaller binary size impact. Using ``pw_string`` in place of the standard C
15 functions eliminates issues related to buffer overflow or missing null
33 The ``pw::string::Format`` and ``pw::string::FormatVaList`` functions provide
34 safer alternatives to ``std::snprintf`` and ``std::vsnprintf``. The snprintf
35 return value is awkward to interpret, and misinterpreting it can lead to serious
38 Size report: replacing snprintf with pw::string::Format
39 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40 The ``Format`` functions have a small, fixed code size cost. However, relative
41 to equivalent ``std::snprintf`` calls, there is no incremental code size cost to
44 .. include:: format_size_report
48 StringBuilder facilitates building formatted strings in a fixed-size buffer. It
49 is designed to give the flexibility of ``std::string`` and
50 ``std::ostringstream``, but with a small footprint. However, applications
51 sensitive to code size should use StringBuilder with care.
53 Size report: replacing snprintf with pw::StringBuilder
54 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55 The fixed code size cost of StringBuilder is significant, though smaller than
56 ``std::snprintf``. Using StringBuilder's << and append methods exclusively in
57 place of ``snprintf`` reduces code size, but ``snprintf`` may be difficult to
60 The incremental code size cost of StringBuilder is comparable to ``snprintf`` if
61 errors are handled. Each argument to StringBuilder's << expands to a function
62 call, but one or two StringBuilder appends may have a smaller code size impact
63 than a single ``snprintf`` call.
65 .. include:: string_builder_size_report
69 * StringBuilder's fixed size cost can be dramatically reduced by limiting
70 support for 64-bit integers.
71 * Consider integrating with the tokenizer module.