1 //===-- VASPrintf.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/VASPrintf.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h" // for SmallVectorImpl
14 #include "llvm/ADT/StringRef.h" // for StringRef
16 #include <assert.h> // for assert
17 #include <stdarg.h> // for va_end, va_list, va_copy
18 #include <stdio.h> // for vsnprintf, size_t
20 bool lldb_private::VASprintf(llvm::SmallVectorImpl<char> &buf, const char *fmt,
22 llvm::SmallString<16> error("<Encoding error>");
25 // Copy in case our first call to vsnprintf doesn't fit into our buffer
27 va_copy(copy_args, args);
29 buf.resize(buf.capacity());
30 // Write up to `capacity` bytes, ignoring the current size.
31 int length = ::vsnprintf(buf.data(), buf.size(), fmt, args);
38 if (size_t(length) >= buf.size()) {
39 // The error formatted string didn't fit into our buffer, resize it
40 // to the exact needed size, and retry
41 buf.resize(length + 1);
42 length = ::vsnprintf(buf.data(), buf.size(), fmt, copy_args);
48 assert(size_t(length) < buf.size());