blob: 1da7fca1011f989f414e0e74b49842add9aca8a9 [file] [log] [blame]
Geoff Langda5777c2014-07-11 09:52:58 -04001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "common/angleutils.h"
8
9#include <vector>
10
11std::string FormatString(const std::string &fmt, va_list vararg)
12{
13 static std::vector<char> buffer(512);
14
15 // Attempt to just print to the current buffer
16 int len = vsnprintf(&buffer[0], buffer.size(), fmt.c_str(), vararg);
17 if (len < 0 || static_cast<size_t>(len) >= buffer.size())
18 {
19 // Buffer was not large enough, calculate the required size and resize the buffer
20 len = vsnprintf(NULL, 0, fmt.c_str(), vararg);
21 buffer.resize(len + 1);
22
23 // Print again
24 vsnprintf(&buffer[0], buffer.size(), fmt.c_str(), vararg);
25 }
26
27 return std::string(buffer.data(), len);
28}
29
30std::string FormatString(const std::string &fmt, ...)
31{
32 va_list vararg;
33 va_start(vararg, fmt);
34 std::string result = FormatString(fmt, vararg);
35 va_end(vararg);
36 return result;
37}