1 // Copyright 2019 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
15 // This size report uses pw::string::Format and std::snprintf to write to the
16 // same buffer numerous times. No error handling or size checking is done.
18 // This compares the overhead of calling pw::string::Format with a span to
19 // calling std::snprintf with a separate pointer and buffer size.
22 #error "USE_FORMAT must be defined"
27 #include "pw_string/format.h"
29 #define FORMAT_CASE(...) pw::string::Format(buffer, __VA_ARGS__)
31 #else // std::snprintf
35 #define FORMAT_CASE(...) std::snprintf(buffer, buffer_size, __VA_ARGS__)
39 namespace pw::string {
41 char* volatile get_buffer;
42 volatile unsigned get_size;
44 void OutputStringsToBuffer() {
46 auto buffer = std::span(get_buffer, get_size);
48 char* buffer = get_buffer;
49 unsigned buffer_size = get_size;
52 const char* string = get_buffer;
53 unsigned value = get_size;
55 FORMAT_CASE("The quick brown");
56 FORMAT_CASE("%s", string);
57 FORMAT_CASE("jumped over the %s d%ug.", string, value);
58 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
59 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
60 FORMAT_CASE("The quick brown");
61 FORMAT_CASE("%s", string);
62 FORMAT_CASE("jumped over the %s d%ug.", string, value);
63 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
64 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
66 FORMAT_CASE("The quick brown");
67 FORMAT_CASE("%s", string);
68 FORMAT_CASE("jumped over the %s d%ug.", string, value);
69 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
70 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
71 FORMAT_CASE("The quick brown");
72 FORMAT_CASE("%s", string);
73 FORMAT_CASE("jumped over the %s d%ug.", string, value);
74 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
75 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
77 FORMAT_CASE("The quick brown");
78 FORMAT_CASE("%s", string);
79 FORMAT_CASE("jumped over the %s d%ug.", string, value);
80 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
81 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
82 FORMAT_CASE("The quick brown");
83 FORMAT_CASE("%s", string);
84 FORMAT_CASE("jumped over the %s d%ug.", string, value);
85 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
86 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
88 FORMAT_CASE("The quick brown");
89 FORMAT_CASE("%s", string);
90 FORMAT_CASE("jumped over the %s d%ug.", string, value);
91 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
92 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
93 FORMAT_CASE("The quick brown");
94 FORMAT_CASE("%s", string);
95 FORMAT_CASE("jumped over the %s d%ug.", string, value);
96 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
97 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
99 FORMAT_CASE("The quick brown");
100 FORMAT_CASE("%s", string);
101 FORMAT_CASE("jumped over the %s d%ug.", string, value);
102 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
103 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
104 FORMAT_CASE("The quick brown");
105 FORMAT_CASE("%s", string);
106 FORMAT_CASE("jumped over the %s d%ug.", string, value);
107 FORMAT_CASE("One two %s %d %s", "three", 4, "five");
108 FORMAT_CASE("a %c %x d %s %f g", 'b', 0xc, "e", 0.0f);
111 } // namespace pw::string
114 pw::string::OutputStringsToBuffer();