xref: /aosp_15_r20/external/pigweed/pw_string/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_string:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=========
4*61c4878aSAndroid Build Coastguard Workerpw_string
5*61c4878aSAndroid Build Coastguard Worker=========
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_string
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker   - **Efficient**: No memory allocation, no pointer indirection.
10*61c4878aSAndroid Build Coastguard Worker   - **Easy**: Use the string API you already know.
11*61c4878aSAndroid Build Coastguard Worker   - **Safe**: Never worry about buffer overruns or undefined behavior.
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard Worker   *Pick three!* If you know how to use ``std::string``, just use
14*61c4878aSAndroid Build Coastguard Worker   :cpp:type:`pw::InlineString` in the same way:
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard Worker   .. code-block:: cpp
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker      // Create a string from a C-style char array; storage is pre-allocated!
19*61c4878aSAndroid Build Coastguard Worker      pw::InlineString<16> my_string = "Literally";
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker      // We have some space left, so let's add to the string.
22*61c4878aSAndroid Build Coastguard Worker      my_string.append('?', 3);  // "Literally???"
23*61c4878aSAndroid Build Coastguard Worker
24*61c4878aSAndroid Build Coastguard Worker      // Let's try something evil and extend this past its capacity ��
25*61c4878aSAndroid Build Coastguard Worker      my_string.append('!', 8);
26*61c4878aSAndroid Build Coastguard Worker      // Foiled by a crash! No mysterious bugs or undefined behavior.
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Worker   Need to build up a string? :cpp:type:`pw::StringBuilder` works like
29*61c4878aSAndroid Build Coastguard Worker   ``std::ostringstream``, but with most of the efficiency and memory benefits
30*61c4878aSAndroid Build Coastguard Worker   of :cpp:type:`pw::InlineString`:
31*61c4878aSAndroid Build Coastguard Worker
32*61c4878aSAndroid Build Coastguard Worker   .. code-block:: cpp
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker      // Create a pw::StringBuilder with a built-in buffer
35*61c4878aSAndroid Build Coastguard Worker      pw::StringBuffer<32> my_string_builder = "Is it really this easy?";
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard Worker      // Add to it with idiomatic C++
38*61c4878aSAndroid Build Coastguard Worker      my_string << " YES!";
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker      // Use it like any other string
41*61c4878aSAndroid Build Coastguard Worker      PW_LOG_DEBUG("%s", my_string_builder.c_str());
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker   Check out :ref:`module-pw_string-guide` for more code samples.
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard WorkerString manipulation on embedded systems can be surprisingly challenging.
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker- **C strings?** They're light-weight but come with many pitfalls for those who
48*61c4878aSAndroid Build Coastguard Worker  don't know the standard library deeply.
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker- **C++ strings?** STL string classes are safe and easy to use, but they consume
51*61c4878aSAndroid Build Coastguard Worker  way too much code space and are designed to be used with dynamic memory
52*61c4878aSAndroid Build Coastguard Worker  allocation.
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker- **Roll your own strings?** You don't have time! You have a product to ship!
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard WorkerEmbedded systems need string functionality that is both *safe* and *suitable*
57*61c4878aSAndroid Build Coastguard Workerfor resource-constrained platforms.
58*61c4878aSAndroid Build Coastguard Worker
59*61c4878aSAndroid Build Coastguard Worker.. rst-class:: key-text
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard Worker   ``pw_string`` provides safe string handling functionality with an API that
62*61c4878aSAndroid Build Coastguard Worker   closely matches that of ``std::string``, but without dynamic memory
63*61c4878aSAndroid Build Coastguard Worker   allocation and with a *much* smaller :ref:`binary size impact<module-pw_string-size-reports>`.
64*61c4878aSAndroid Build Coastguard Worker
65*61c4878aSAndroid Build Coastguard Worker--------------------
66*61c4878aSAndroid Build Coastguard WorkerIs it right for you?
67*61c4878aSAndroid Build Coastguard Worker--------------------
68*61c4878aSAndroid Build Coastguard Worker.. rst-class:: key-text
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker   ``pw_string`` is useful any time you need to handle strings in embedded C++.
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard WorkerIf your project written in C, ``pw_string`` is not a good fit since we don't
73*61c4878aSAndroid Build Coastguard Workercurrently expose a C API.
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard WorkerOn the other hand, for larger platforms where code space isn't in short supply
76*61c4878aSAndroid Build Coastguard Workerand dynamic memory allocation isn't a problem, you may find that ``std::string``
77*61c4878aSAndroid Build Coastguard Workermeets your needs.
78*61c4878aSAndroid Build Coastguard Worker
79*61c4878aSAndroid Build Coastguard Worker.. tip::
80*61c4878aSAndroid Build Coastguard Worker   ``pw_string`` works just as well on larger embedded platforms and host
81*61c4878aSAndroid Build Coastguard Worker   systems. Using ``pw_string`` even when you could get away with ``std:string``
82*61c4878aSAndroid Build Coastguard Worker   gives you the flexibility to move to smaller platforms later with much less
83*61c4878aSAndroid Build Coastguard Worker   rework.
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard Worker.. toctree::
86*61c4878aSAndroid Build Coastguard Worker   :hidden:
87*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker   guide
90*61c4878aSAndroid Build Coastguard Worker   api
91*61c4878aSAndroid Build Coastguard Worker   design
92*61c4878aSAndroid Build Coastguard Worker   code_size
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`rocket` Get Started & Guides
97*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_string-get-started
98*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
99*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-primary
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard Worker      Integrate pw_string into your project and learn common use cases
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` API Reference
104*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_string-api
105*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
106*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
107*61c4878aSAndroid Build Coastguard Worker
108*61c4878aSAndroid Build Coastguard Worker      Detailed description of the pw_string's classes and methods
109*61c4878aSAndroid Build Coastguard Worker
110*61c4878aSAndroid Build Coastguard Worker.. grid:: 2
111*61c4878aSAndroid Build Coastguard Worker
112*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` Design & Roadmap
113*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_string-design
114*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
115*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
116*61c4878aSAndroid Build Coastguard Worker
117*61c4878aSAndroid Build Coastguard Worker      Learn why pw_string is designed the way it is, and upcoming plans
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard Worker   .. grid-item-card:: :octicon:`code-square` Code Size Analysis
120*61c4878aSAndroid Build Coastguard Worker      :link: module-pw_string-size-reports
121*61c4878aSAndroid Build Coastguard Worker      :link-type: ref
122*61c4878aSAndroid Build Coastguard Worker      :class-item: sales-pitch-cta-secondary
123*61c4878aSAndroid Build Coastguard Worker
124*61c4878aSAndroid Build Coastguard Worker      Understand pw_string's code footprint and savings potential
125