Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: third_party/re2/util/stringprintf.cc

Issue 1544433002: Replace RE2 import with a dependency (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Re-Added LICENSE and OWNERS file Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/re2/util/sparse_set.h ('k') | third_party/re2/util/strutil.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2002 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include "util/util.h"
6
7 namespace re2 {
8
9 static void StringAppendV(string* dst, const char* format, va_list ap) {
10 // First try with a small fixed size buffer
11 char space[1024];
12
13 // It's possible for methods that use a va_list to invalidate
14 // the data in it upon use. The fix is to make a copy
15 // of the structure before using it and use that copy instead.
16 va_list backup_ap;
17 va_copy(backup_ap, ap);
18 int result = vsnprintf(space, sizeof(space), format, backup_ap);
19 va_end(backup_ap);
20
21 if ((result >= 0) && (static_cast<unsigned long>(result) < sizeof(space))) {
22 // It fit
23 dst->append(space, result);
24 return;
25 }
26
27 // Repeatedly increase buffer size until it fits
28 int length = sizeof(space);
29 while (true) {
30 if (result < 0) {
31 // Older behavior: just try doubling the buffer size
32 length *= 2;
33 } else {
34 // We need exactly "result+1" characters
35 length = result+1;
36 }
37 char* buf = new char[length];
38
39 // Restore the va_list before we use it again
40 va_copy(backup_ap, ap);
41 #if !defined(_WIN32)
42 result = vsnprintf(buf, length, format, backup_ap);
43 #else
44 // On Windows, the function takes five arguments, not four. With an array,
45 // the buffer size will be inferred, but not with a pointer. C'est la vie.
46 // (See https://github.com/google/re2/issues/40 for more details.)
47 result = vsnprintf(buf, length, _TRUNCATE, format, backup_ap);
48 #endif
49 va_end(backup_ap);
50
51 if ((result >= 0) && (result < length)) {
52 // It fit
53 dst->append(buf, result);
54 delete[] buf;
55 return;
56 }
57 delete[] buf;
58 }
59 }
60
61 string StringPrintf(const char* format, ...) {
62 va_list ap;
63 va_start(ap, format);
64 string result;
65 StringAppendV(&result, format, ap);
66 va_end(ap);
67 return result;
68 }
69
70 void SStringPrintf(string* dst, const char* format, ...) {
71 va_list ap;
72 va_start(ap, format);
73 dst->clear();
74 StringAppendV(dst, format, ap);
75 va_end(ap);
76 }
77
78 void StringAppendF(string* dst, const char* format, ...) {
79 va_list ap;
80 va_start(ap, format);
81 StringAppendV(dst, format, ap);
82 va_end(ap);
83 }
84
85 } // namespace re2
OLDNEW
« no previous file with comments | « third_party/re2/util/sparse_set.h ('k') | third_party/re2/util/strutil.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698