Line data Source code
1 : //
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 : #include "common/debug.h"
9 :
10 : #include <stdio.h>
11 :
12 : #include <limits>
13 : #include <vector>
14 :
15 : namespace angle
16 : {
17 : const uintptr_t DirtyPointer = std::numeric_limits<uintptr_t>::max();
18 : }
19 :
20 0 : size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& outBuffer)
21 : {
22 : // Attempt to just print to the current buffer
23 0 : int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
24 0 : if (len < 0 || static_cast<size_t>(len) >= outBuffer.size())
25 : {
26 : // Buffer was not large enough, calculate the required size and resize the buffer
27 0 : len = vsnprintf(NULL, 0, fmt, vararg);
28 0 : outBuffer.resize(len + 1);
29 :
30 : // Print again
31 0 : len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
32 : }
33 0 : ASSERT(len >= 0);
34 0 : return static_cast<size_t>(len);
35 : }
36 :
37 0 : std::string FormatString(const char *fmt, va_list vararg)
38 : {
39 0 : static std::vector<char> buffer(512);
40 :
41 0 : size_t len = FormatStringIntoVector(fmt, vararg, buffer);
42 0 : return std::string(&buffer[0], len);
43 : }
44 :
45 0 : std::string FormatString(const char *fmt, ...)
46 : {
47 : va_list vararg;
48 0 : va_start(vararg, fmt);
49 0 : std::string result = FormatString(fmt, vararg);
50 0 : va_end(vararg);
51 0 : return result;
52 : }
|