OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_STRINGS_SAFE_SPRINTF_H_ | 5 #ifndef BASE_STRINGS_SAFE_SPRINTF_H_ |
6 #define BASE_STRINGS_SAFE_SPRINTF_H_ | 6 #define BASE_STRINGS_SAFE_SPRINTF_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 132 |
133 namespace internal { | 133 namespace internal { |
134 // Helpers that use C++ overloading, templates, and specializations to deduce | 134 // Helpers that use C++ overloading, templates, and specializations to deduce |
135 // and record type information from function arguments. This allows us to | 135 // and record type information from function arguments. This allows us to |
136 // later write a type-safe version of snprintf(). | 136 // later write a type-safe version of snprintf(). |
137 | 137 |
138 struct Arg { | 138 struct Arg { |
139 enum Type { INT, UINT, STRING, POINTER }; | 139 enum Type { INT, UINT, STRING, POINTER }; |
140 | 140 |
141 // Any integer-like value. | 141 // Any integer-like value. |
142 Arg(signed char c) : i(c), width(sizeof(char)), type(INT) { } | 142 Arg(signed char c) : type(INT) { integer = { c, sizeof(char) }; } |
143 Arg(unsigned char c) : i(c), width(sizeof(char)), type(UINT) { } | 143 Arg(unsigned char c) : type(UINT) { integer = { c, sizeof(char) }; } |
144 Arg(signed short j) : i(j), width(sizeof(short)), type(INT) { } | 144 Arg(signed short j) : type(INT) { integer = { j, sizeof(short) }; } |
145 Arg(unsigned short j) : i(j), width(sizeof(short)), type(UINT) { } | 145 Arg(unsigned short j) : type(UINT) { integer = { j, sizeof(short) }; } |
146 Arg(signed int j) : i(j), width(sizeof(int)), type(INT) { } | 146 Arg(signed int j) : type(INT) { integer = { j, sizeof(int) }; } |
147 Arg(unsigned int j) : i(j), width(sizeof(int)), type(UINT) { } | 147 Arg(unsigned int j) : type(UINT) { integer = { j, sizeof(int) }; } |
148 Arg(signed long j) : i(j), width(sizeof(long)), type(INT) { } | 148 Arg(signed long j) : type(INT) { integer = { j, sizeof(long) }; } |
149 Arg(unsigned long j) : i(j), width(sizeof(long)), type(UINT) { } | 149 Arg(unsigned long j) : type(UINT) { integer = { j, sizeof(long) }; } |
150 Arg(signed long long j) : i(j), width(sizeof(long long)), type(INT) { } | 150 Arg(signed long long j) : type(INT) { integer = { j, sizeof(long long) }; } |
151 Arg(unsigned long long j) : i(j), width(sizeof(long long)), type(UINT) { } | 151 Arg(unsigned long long j) : type(UINT) { integer = { j, sizeof(long long) }; } |
152 | 152 |
153 // A C-style text string. | 153 // A C-style text string. |
154 Arg(const char* s) : str(s), type(STRING) { } | 154 Arg(const char* s) : str(s), type(STRING) { } |
155 Arg(char* s) : str(s), type(STRING) { } | 155 Arg(char* s) : str(s), type(STRING) { } |
156 | 156 |
157 // Any pointer value that can be cast to a "void*". | 157 // Any pointer value that can be cast to a "void*". |
158 template<class T> Arg(T* p) : ptr((void*)p), type(POINTER) { } | 158 template<class T> Arg(T* p) : ptr((void*)p), type(POINTER) { } |
159 | 159 |
160 union { | 160 union { |
161 // An integer-like value. | 161 // An integer-like value. |
162 struct { | 162 struct { |
163 int64_t i; | 163 int64_t i; |
164 unsigned char width; | 164 unsigned char width; |
165 }; | 165 } integer; |
166 | 166 |
167 // A C-style text string. | 167 // A C-style text string. |
168 const char* str; | 168 const char* str; |
169 | 169 |
170 // A pointer to an arbitrary object. | 170 // A pointer to an arbitrary object. |
171 const void* ptr; | 171 const void* ptr; |
172 }; | 172 }; |
173 const enum Type type; | 173 const enum Type type; |
174 }; | 174 }; |
175 | 175 |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 BASE_EXPORT ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt); | 408 BASE_EXPORT ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt); |
409 template<size_t N> | 409 template<size_t N> |
410 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) { | 410 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) { |
411 return SafeSNPrintf(buf, N, fmt); | 411 return SafeSNPrintf(buf, N, fmt); |
412 } | 412 } |
413 | 413 |
414 } // namespace strings | 414 } // namespace strings |
415 } // namespace base | 415 } // namespace base |
416 | 416 |
417 #endif // BASE_STRINGS_SAFE_SPRINTF_H_ | 417 #endif // BASE_STRINGS_SAFE_SPRINTF_H_ |
OLD | NEW |