xref: /aosp_15_r20/external/libchrome/base/strings/stringprintf.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/strings/stringprintf.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <errno.h>
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <vector>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/scoped_clear_errno.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/strings/utf_string_conversions.h"
16*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker namespace base {
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker namespace {
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter
23*635a8641SAndroid Build Coastguard Worker // is the size of the buffer. These return the number of characters in the
24*635a8641SAndroid Build Coastguard Worker // formatted string excluding the NUL terminator. If the buffer is not
25*635a8641SAndroid Build Coastguard Worker // large enough to accommodate the formatted string without truncation, they
26*635a8641SAndroid Build Coastguard Worker // return the number of characters that would be in the fully-formatted string
27*635a8641SAndroid Build Coastguard Worker // (vsnprintf, and vswprintf on Windows), or -1 (vswprintf on POSIX platforms).
vsnprintfT(char * buffer,size_t buf_size,const char * format,va_list argptr)28*635a8641SAndroid Build Coastguard Worker inline int vsnprintfT(char* buffer,
29*635a8641SAndroid Build Coastguard Worker                       size_t buf_size,
30*635a8641SAndroid Build Coastguard Worker                       const char* format,
31*635a8641SAndroid Build Coastguard Worker                       va_list argptr) {
32*635a8641SAndroid Build Coastguard Worker   return base::vsnprintf(buffer, buf_size, format, argptr);
33*635a8641SAndroid Build Coastguard Worker }
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
vsnprintfT(wchar_t * buffer,size_t buf_size,const wchar_t * format,va_list argptr)36*635a8641SAndroid Build Coastguard Worker inline int vsnprintfT(wchar_t* buffer,
37*635a8641SAndroid Build Coastguard Worker                       size_t buf_size,
38*635a8641SAndroid Build Coastguard Worker                       const wchar_t* format,
39*635a8641SAndroid Build Coastguard Worker                       va_list argptr) {
40*635a8641SAndroid Build Coastguard Worker   return base::vswprintf(buffer, buf_size, format, argptr);
41*635a8641SAndroid Build Coastguard Worker }
42*635a8641SAndroid Build Coastguard Worker #endif
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker // Templatized backend for StringPrintF/StringAppendF. This does not finalize
45*635a8641SAndroid Build Coastguard Worker // the va_list, the caller is expected to do that.
46*635a8641SAndroid Build Coastguard Worker template <class StringType>
StringAppendVT(StringType * dst,const typename StringType::value_type * format,va_list ap)47*635a8641SAndroid Build Coastguard Worker static void StringAppendVT(StringType* dst,
48*635a8641SAndroid Build Coastguard Worker                            const typename StringType::value_type* format,
49*635a8641SAndroid Build Coastguard Worker                            va_list ap) {
50*635a8641SAndroid Build Coastguard Worker   // First try with a small fixed size buffer.
51*635a8641SAndroid Build Coastguard Worker   // This buffer size should be kept in sync with StringUtilTest.GrowBoundary
52*635a8641SAndroid Build Coastguard Worker   // and StringUtilTest.StringPrintfBounds.
53*635a8641SAndroid Build Coastguard Worker   typename StringType::value_type stack_buf[1024];
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker   va_list ap_copy;
56*635a8641SAndroid Build Coastguard Worker   va_copy(ap_copy, ap);
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker #if !defined(OS_WIN)
59*635a8641SAndroid Build Coastguard Worker   ScopedClearErrno clear_errno;
60*635a8641SAndroid Build Coastguard Worker #endif
61*635a8641SAndroid Build Coastguard Worker   int result = vsnprintfT(stack_buf, arraysize(stack_buf), format, ap_copy);
62*635a8641SAndroid Build Coastguard Worker   va_end(ap_copy);
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker   if (result >= 0 && result < static_cast<int>(arraysize(stack_buf))) {
65*635a8641SAndroid Build Coastguard Worker     // It fit.
66*635a8641SAndroid Build Coastguard Worker     dst->append(stack_buf, result);
67*635a8641SAndroid Build Coastguard Worker     return;
68*635a8641SAndroid Build Coastguard Worker   }
69*635a8641SAndroid Build Coastguard Worker 
70*635a8641SAndroid Build Coastguard Worker   // Repeatedly increase buffer size until it fits.
71*635a8641SAndroid Build Coastguard Worker   int mem_length = arraysize(stack_buf);
72*635a8641SAndroid Build Coastguard Worker   while (true) {
73*635a8641SAndroid Build Coastguard Worker     if (result < 0) {
74*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
75*635a8641SAndroid Build Coastguard Worker       // On Windows, vsnprintfT always returns the number of characters in a
76*635a8641SAndroid Build Coastguard Worker       // fully-formatted string, so if we reach this point, something else is
77*635a8641SAndroid Build Coastguard Worker       // wrong and no amount of buffer-doubling is going to fix it.
78*635a8641SAndroid Build Coastguard Worker       return;
79*635a8641SAndroid Build Coastguard Worker #else
80*635a8641SAndroid Build Coastguard Worker       if (errno != 0 && errno != EOVERFLOW)
81*635a8641SAndroid Build Coastguard Worker         return;
82*635a8641SAndroid Build Coastguard Worker       // Try doubling the buffer size.
83*635a8641SAndroid Build Coastguard Worker       mem_length *= 2;
84*635a8641SAndroid Build Coastguard Worker #endif
85*635a8641SAndroid Build Coastguard Worker     } else {
86*635a8641SAndroid Build Coastguard Worker       // We need exactly "result + 1" characters.
87*635a8641SAndroid Build Coastguard Worker       mem_length = result + 1;
88*635a8641SAndroid Build Coastguard Worker     }
89*635a8641SAndroid Build Coastguard Worker 
90*635a8641SAndroid Build Coastguard Worker     if (mem_length > 32 * 1024 * 1024) {
91*635a8641SAndroid Build Coastguard Worker       // That should be plenty, don't try anything larger.  This protects
92*635a8641SAndroid Build Coastguard Worker       // against huge allocations when using vsnprintfT implementations that
93*635a8641SAndroid Build Coastguard Worker       // return -1 for reasons other than overflow without setting errno.
94*635a8641SAndroid Build Coastguard Worker       DLOG(WARNING) << "Unable to printf the requested string due to size.";
95*635a8641SAndroid Build Coastguard Worker       return;
96*635a8641SAndroid Build Coastguard Worker     }
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker     std::vector<typename StringType::value_type> mem_buf(mem_length);
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker     // NOTE: You can only use a va_list once.  Since we're in a while loop, we
101*635a8641SAndroid Build Coastguard Worker     // need to make a new copy each time so we don't use up the original.
102*635a8641SAndroid Build Coastguard Worker     va_copy(ap_copy, ap);
103*635a8641SAndroid Build Coastguard Worker     result = vsnprintfT(&mem_buf[0], mem_length, format, ap_copy);
104*635a8641SAndroid Build Coastguard Worker     va_end(ap_copy);
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker     if ((result >= 0) && (result < mem_length)) {
107*635a8641SAndroid Build Coastguard Worker       // It fit.
108*635a8641SAndroid Build Coastguard Worker       dst->append(&mem_buf[0], result);
109*635a8641SAndroid Build Coastguard Worker       return;
110*635a8641SAndroid Build Coastguard Worker     }
111*635a8641SAndroid Build Coastguard Worker   }
112*635a8641SAndroid Build Coastguard Worker }
113*635a8641SAndroid Build Coastguard Worker 
114*635a8641SAndroid Build Coastguard Worker }  // namespace
115*635a8641SAndroid Build Coastguard Worker 
StringPrintf(const char * format,...)116*635a8641SAndroid Build Coastguard Worker std::string StringPrintf(const char* format, ...) {
117*635a8641SAndroid Build Coastguard Worker   va_list ap;
118*635a8641SAndroid Build Coastguard Worker   va_start(ap, format);
119*635a8641SAndroid Build Coastguard Worker   std::string result;
120*635a8641SAndroid Build Coastguard Worker   StringAppendV(&result, format, ap);
121*635a8641SAndroid Build Coastguard Worker   va_end(ap);
122*635a8641SAndroid Build Coastguard Worker   return result;
123*635a8641SAndroid Build Coastguard Worker }
124*635a8641SAndroid Build Coastguard Worker 
125*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
StringPrintf(const wchar_t * format,...)126*635a8641SAndroid Build Coastguard Worker std::wstring StringPrintf(const wchar_t* format, ...) {
127*635a8641SAndroid Build Coastguard Worker   va_list ap;
128*635a8641SAndroid Build Coastguard Worker   va_start(ap, format);
129*635a8641SAndroid Build Coastguard Worker   std::wstring result;
130*635a8641SAndroid Build Coastguard Worker   StringAppendV(&result, format, ap);
131*635a8641SAndroid Build Coastguard Worker   va_end(ap);
132*635a8641SAndroid Build Coastguard Worker   return result;
133*635a8641SAndroid Build Coastguard Worker }
134*635a8641SAndroid Build Coastguard Worker #endif
135*635a8641SAndroid Build Coastguard Worker 
StringPrintV(const char * format,va_list ap)136*635a8641SAndroid Build Coastguard Worker std::string StringPrintV(const char* format, va_list ap) {
137*635a8641SAndroid Build Coastguard Worker   std::string result;
138*635a8641SAndroid Build Coastguard Worker   StringAppendV(&result, format, ap);
139*635a8641SAndroid Build Coastguard Worker   return result;
140*635a8641SAndroid Build Coastguard Worker }
141*635a8641SAndroid Build Coastguard Worker 
SStringPrintf(std::string * dst,const char * format,...)142*635a8641SAndroid Build Coastguard Worker const std::string& SStringPrintf(std::string* dst, const char* format, ...) {
143*635a8641SAndroid Build Coastguard Worker   va_list ap;
144*635a8641SAndroid Build Coastguard Worker   va_start(ap, format);
145*635a8641SAndroid Build Coastguard Worker   dst->clear();
146*635a8641SAndroid Build Coastguard Worker   StringAppendV(dst, format, ap);
147*635a8641SAndroid Build Coastguard Worker   va_end(ap);
148*635a8641SAndroid Build Coastguard Worker   return *dst;
149*635a8641SAndroid Build Coastguard Worker }
150*635a8641SAndroid Build Coastguard Worker 
151*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
SStringPrintf(std::wstring * dst,const wchar_t * format,...)152*635a8641SAndroid Build Coastguard Worker const std::wstring& SStringPrintf(std::wstring* dst,
153*635a8641SAndroid Build Coastguard Worker                                   const wchar_t* format, ...) {
154*635a8641SAndroid Build Coastguard Worker   va_list ap;
155*635a8641SAndroid Build Coastguard Worker   va_start(ap, format);
156*635a8641SAndroid Build Coastguard Worker   dst->clear();
157*635a8641SAndroid Build Coastguard Worker   StringAppendV(dst, format, ap);
158*635a8641SAndroid Build Coastguard Worker   va_end(ap);
159*635a8641SAndroid Build Coastguard Worker   return *dst;
160*635a8641SAndroid Build Coastguard Worker }
161*635a8641SAndroid Build Coastguard Worker #endif
162*635a8641SAndroid Build Coastguard Worker 
StringAppendF(std::string * dst,const char * format,...)163*635a8641SAndroid Build Coastguard Worker void StringAppendF(std::string* dst, const char* format, ...) {
164*635a8641SAndroid Build Coastguard Worker   va_list ap;
165*635a8641SAndroid Build Coastguard Worker   va_start(ap, format);
166*635a8641SAndroid Build Coastguard Worker   StringAppendV(dst, format, ap);
167*635a8641SAndroid Build Coastguard Worker   va_end(ap);
168*635a8641SAndroid Build Coastguard Worker }
169*635a8641SAndroid Build Coastguard Worker 
170*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
StringAppendF(std::wstring * dst,const wchar_t * format,...)171*635a8641SAndroid Build Coastguard Worker void StringAppendF(std::wstring* dst, const wchar_t* format, ...) {
172*635a8641SAndroid Build Coastguard Worker   va_list ap;
173*635a8641SAndroid Build Coastguard Worker   va_start(ap, format);
174*635a8641SAndroid Build Coastguard Worker   StringAppendV(dst, format, ap);
175*635a8641SAndroid Build Coastguard Worker   va_end(ap);
176*635a8641SAndroid Build Coastguard Worker }
177*635a8641SAndroid Build Coastguard Worker #endif
178*635a8641SAndroid Build Coastguard Worker 
StringAppendV(std::string * dst,const char * format,va_list ap)179*635a8641SAndroid Build Coastguard Worker void StringAppendV(std::string* dst, const char* format, va_list ap) {
180*635a8641SAndroid Build Coastguard Worker   StringAppendVT(dst, format, ap);
181*635a8641SAndroid Build Coastguard Worker }
182*635a8641SAndroid Build Coastguard Worker 
183*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
StringAppendV(std::wstring * dst,const wchar_t * format,va_list ap)184*635a8641SAndroid Build Coastguard Worker void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) {
185*635a8641SAndroid Build Coastguard Worker   StringAppendVT(dst, format, ap);
186*635a8641SAndroid Build Coastguard Worker }
187*635a8641SAndroid Build Coastguard Worker #endif
188*635a8641SAndroid Build Coastguard Worker 
189*635a8641SAndroid Build Coastguard Worker }  // namespace base
190