blob: a47b08fe9502d59dedeebc34b29e4f2ba959ca17 [file] [log] [blame]
Wyatt Hepleree3e02f2019-12-05 10:52:31 -08001.. _chapter-pw-string:
Wyatt Heplerfe85de22019-11-19 17:10:20 -08002
3.. default-domain:: cpp
4
5.. highlight:: sh
6
7---------
8pw_string
9---------
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080010The string module provides efficient utilities for safely working with strings.
11The pw_string functions and classes always null-terminate strings and never
12overrun buffers. They do not allocate their own memory.
Wyatt Heplerfe85de22019-11-19 17:10:20 -080013
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080014Compatibility
Wyatt Heplerfe85de22019-11-19 17:10:20 -080015=============
16C++17
17
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080018Dependencies
19============
Wyatt Heplerfe85de22019-11-19 17:10:20 -080020* pw_preprocessor
21* pw_status
22* pw_span
23
24Features
25========
26
27pw::string::Format
28------------------
29The ``pw::string::Format`` functions provides are safer alternatives to
30``std::snprintf`` and ``std::vsnprintf``. The snprintf return value is awkward
31to interpret, and misinterpreting it can lead to serious bugs.
32
33Size report: replacing snprintf with pw::string::Format
34^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35The ``Format`` functions have a small, fixed code size cost. However, relative
36to equivalent ``std::snprintf`` calls, there is no incremental code size cost to
37using ``Format``.
38
39.. include:: format_size_report.rst
40
41pw::StringBuilder
42-----------------
43StringBuilder facilitates building formatted strings in a fixed-size buffer.
Wyatt Hepleree3e02f2019-12-05 10:52:31 -080044It is designed to give the flexibility of std::string and std::ostringstream,
45but with a small footprint. However, applications sensitive to code size should
Wyatt Heplerfe85de22019-11-19 17:10:20 -080046use StringBuilder with care.
47
48Size report: replacing snprintf with pw::StringBuilder
49^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50The fixed code size cost of StringBuilder is significant, though smaller than
51std::snprintf. Using StringBuilder's << and append methods exclusively in
52place of snprintf reduces code size, but snprintf may be difficult to avoid.
53
54The incremental code size cost of StringBuilder is comparable to snprintf if
55errors are handled. Each argument to StringBuilder's << expands to a function
56call, but one or two StringBuilder appends may have a smaller code size
57impact than a single snprintf call.
58
59.. include:: string_builder_size_report.rst
60
61Future work
62^^^^^^^^^^^
63* StringBuilder's fixed size cost can be dramatically reduced by limiting
64 support for 64-bit integers.
65* Consider integrating with the tokenizer module.