1 //===-- VASprintf.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/VASPrintf.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
19 bool lldb_private::VASprintf(llvm::SmallVectorImpl
<char> &buf
, const char *fmt
,
21 llvm::SmallString
<16> error("<Encoding error>");
24 // Copy in case our first call to vsnprintf doesn't fit into our buffer
26 va_copy(copy_args
, args
);
28 buf
.resize(buf
.capacity());
29 // Write up to `capacity` bytes, ignoring the current size.
30 int length
= ::vsnprintf(buf
.data(), buf
.size(), fmt
, args
);
37 if (size_t(length
) >= buf
.size()) {
38 // The error formatted string didn't fit into our buffer, resize it to the
39 // exact needed size, and retry
40 buf
.resize(length
+ 1);
41 length
= ::vsnprintf(buf
.data(), buf
.size(), fmt
, copy_args
);
47 assert(size_t(length
) < buf
.size());