Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_string / size_report / format_many_without_error_handling.cc
1 // Copyright 2019 The Pigweed Authors
2 //
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
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
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
13 // the License.
14
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.
17 //
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.
20
21 #ifndef USE_FORMAT
22 #error "USE_FORMAT must be defined"
23 #endif  // USE_FORMAT
24
25 #if USE_FORMAT
26
27 #include "pw_string/format.h"
28
29 #define FORMAT_CASE(...) pw::string::Format(buffer, __VA_ARGS__)
30
31 #else  // std::snprintf
32
33 #include <cstdio>
34
35 #define FORMAT_CASE(...) std::snprintf(buffer, buffer_size, __VA_ARGS__)
36
37 #endif  // USE_FORMAT
38
39 namespace pw::string {
40
41 char* volatile get_buffer;
42 volatile unsigned get_size;
43
44 void OutputStringsToBuffer() {
45 #if USE_FORMAT
46   auto buffer = std::span(get_buffer, get_size);
47 #else
48   char* buffer = get_buffer;
49   unsigned buffer_size = get_size;
50 #endif  // USE_FORMAT
51
52   const char* string = get_buffer;
53   unsigned value = get_size;
54
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);
65
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);
76
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);
87
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);
98
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);
109 }
110
111 }  // namespace pw::string
112
113 int main() {
114   pw::string::OutputStringsToBuffer();
115   return 0;
116 }