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) { |
143 Arg(unsigned char c) : i(c), width(sizeof(char)), type(UINT) { } | 143 integer.i = c; |
144 Arg(signed short j) : i(j), width(sizeof(short)), type(INT) { } | 144 integer.width = sizeof(char); |
145 Arg(unsigned short j) : i(j), width(sizeof(short)), type(UINT) { } | 145 } |
146 Arg(signed int j) : i(j), width(sizeof(int)), type(INT) { } | 146 Arg(unsigned char c) : type(UINT) { |
147 Arg(unsigned int j) : i(j), width(sizeof(int)), type(UINT) { } | 147 integer.i = c; |
148 Arg(signed long j) : i(j), width(sizeof(long)), type(INT) { } | 148 integer.width = sizeof(char); |
149 Arg(unsigned long j) : i(j), width(sizeof(long)), type(UINT) { } | 149 } |
150 Arg(signed long long j) : i(j), width(sizeof(long long)), type(INT) { } | 150 Arg(signed short j) : type(INT) { |
151 Arg(unsigned long long j) : i(j), width(sizeof(long long)), type(UINT) { } | 151 integer.i = j; |
| 152 integer.width = sizeof(short); |
| 153 } |
| 154 Arg(unsigned short j) : type(UINT) { |
| 155 integer.i = j; |
| 156 integer.width = sizeof(short); |
| 157 } |
| 158 Arg(signed int j) : type(INT) { |
| 159 integer.i = j; |
| 160 integer.width = sizeof(int); |
| 161 } |
| 162 Arg(unsigned int j) : type(UINT) { |
| 163 integer.i = j; |
| 164 integer.width = sizeof(int); |
| 165 } |
| 166 Arg(signed long j) : type(INT) { |
| 167 integer.i = j; |
| 168 integer.width = sizeof(long); |
| 169 } |
| 170 Arg(unsigned long j) : type(UINT) { |
| 171 integer.i = j; |
| 172 integer.width = sizeof(long); |
| 173 } |
| 174 Arg(signed long long j) : type(INT) { |
| 175 integer.i = j; |
| 176 integer.width = sizeof(long long); |
| 177 } |
| 178 Arg(unsigned long long j) : type(UINT) { |
| 179 integer.i = j; |
| 180 integer.width = sizeof(long long); |
| 181 } |
152 | 182 |
153 // A C-style text string. | 183 // A C-style text string. |
154 Arg(const char* s) : str(s), type(STRING) { } | 184 Arg(const char* s) : str(s), type(STRING) { } |
155 Arg(char* s) : str(s), type(STRING) { } | 185 Arg(char* s) : str(s), type(STRING) { } |
156 | 186 |
157 // Any pointer value that can be cast to a "void*". | 187 // Any pointer value that can be cast to a "void*". |
158 template<class T> Arg(T* p) : ptr((void*)p), type(POINTER) { } | 188 template<class T> Arg(T* p) : ptr((void*)p), type(POINTER) { } |
159 | 189 |
160 union { | 190 union { |
161 // An integer-like value. | 191 // An integer-like value. |
162 struct { | 192 struct { |
163 int64_t i; | 193 int64_t i; |
164 unsigned char width; | 194 unsigned char width; |
165 }; | 195 } integer; |
166 | 196 |
167 // A C-style text string. | 197 // A C-style text string. |
168 const char* str; | 198 const char* str; |
169 | 199 |
170 // A pointer to an arbitrary object. | 200 // A pointer to an arbitrary object. |
171 const void* ptr; | 201 const void* ptr; |
172 }; | 202 }; |
173 const enum Type type; | 203 const enum Type type; |
174 }; | 204 }; |
175 | 205 |
(...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); | 438 BASE_EXPORT ssize_t SafeSNPrintf(char* buf, size_t N, const char* fmt); |
409 template<size_t N> | 439 template<size_t N> |
410 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) { | 440 inline ssize_t SafeSPrintf(char (&buf)[N], const char* fmt) { |
411 return SafeSNPrintf(buf, N, fmt); | 441 return SafeSNPrintf(buf, N, fmt); |
412 } | 442 } |
413 | 443 |
414 } // namespace strings | 444 } // namespace strings |
415 } // namespace base | 445 } // namespace base |
416 | 446 |
417 #endif // BASE_STRINGS_SAFE_SPRINTF_H_ | 447 #endif // BASE_STRINGS_SAFE_SPRINTF_H_ |
OLD | NEW |