xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/lib/cpp-string/string_printf.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 #include <cpp-string/string_printf.h>
16 #include <errno.h>
17 #include <stdarg.h>
18 #include <stddef.h>
19 #include <stdio.h>
20 
21 namespace bt_lib_cpp_string {
22 namespace {
23 
StringVAppendfHelper(std::string * dest,const char * format,va_list ap)24 void StringVAppendfHelper(std::string* dest, const char* format, va_list ap) {
25   // Size of the small stack buffer to use. This should be kept in sync
26   // with the numbers in StringPrintfTest.
27   constexpr size_t kStackBufferSize = 1024u;
28 
29   char stack_buf[kStackBufferSize];
30   // |result| is the number of characters that would have been written if
31   // kStackBufferSize were sufficiently large, not counting the terminating null
32   // character. |vsnprintf()| always null-terminates!
33   int result = vsnprintf(stack_buf, kStackBufferSize, format, ap);
34   if (result < 0) {
35     // As far as I can tell, we'd only get |EOVERFLOW| if the result is so large
36     // that it can't be represented by an |int| (in which case retrying would be
37     // futile), so Chromium's implementation is wrong.
38     return;
39   }
40 
41   // Only append what fit into our stack buffer.
42   // Strings that are too long will be truncated.
43   size_t actual_len_excluding_null = result;
44   if (actual_len_excluding_null > kStackBufferSize - 1) {
45     actual_len_excluding_null = kStackBufferSize - 1;
46   }
47   dest->append(stack_buf, actual_len_excluding_null);
48 }
49 
50 }  // namespace
51 
StringPrintf(const char * format,...)52 std::string StringPrintf(const char* format, ...) {
53   va_list ap;
54   va_start(ap, format);
55   std::string rv;
56   StringVAppendf(&rv, format, ap);
57   va_end(ap);
58   return rv;
59 }
60 
StringVPrintf(const char * format,va_list ap)61 std::string StringVPrintf(const char* format, va_list ap) {
62   std::string rv;
63   StringVAppendf(&rv, format, ap);
64   return rv;
65 }
66 
StringAppendf(std::string * dest,const char * format,...)67 void StringAppendf(std::string* dest, const char* format, ...) {
68   va_list ap;
69   va_start(ap, format);
70   StringVAppendf(dest, format, ap);
71   va_end(ap);
72 }
73 
StringVAppendf(std::string * dest,const char * format,va_list ap)74 void StringVAppendf(std::string* dest, const char* format, va_list ap) {
75   int old_errno = errno;
76   StringVAppendfHelper(dest, format, ap);
77   errno = old_errno;
78 }
79 
80 }  // namespace bt_lib_cpp_string
81