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_STRINGPRINTF_H_ | 5 #ifndef BASE_STRINGS_STRINGPRINTF_H_ |
6 #define BASE_STRINGS_STRINGPRINTF_H_ | 6 #define BASE_STRINGS_STRINGPRINTF_H_ |
7 | 7 |
8 #include <stdarg.h> // va_list | 8 #include <stdarg.h> // va_list |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "build/build_config.h" |
| 15 |
| 16 #ifdef COMPILER_MSVC |
| 17 // For _Printf_format_string_. |
| 18 #include <sal.h> |
| 19 #else |
| 20 // For nacl builds when sal.h is not available. |
| 21 #define _Printf_format_string_ |
| 22 #endif |
14 | 23 |
15 namespace base { | 24 namespace base { |
16 | 25 |
17 // Return a C++ string given printf-like input. | 26 // Return a C++ string given printf-like input. |
18 BASE_EXPORT std::string StringPrintf(const char* format, ...) | 27 BASE_EXPORT std::string StringPrintf(_Printf_format_string_ const char* format, |
| 28 ...) |
19 PRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT; | 29 PRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT; |
20 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
21 BASE_EXPORT std::wstring StringPrintf(const wchar_t* format, ...) | 31 BASE_EXPORT std::wstring StringPrintf( |
22 WPRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT; | 32 _Printf_format_string_ const wchar_t* format, |
| 33 ...) WPRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT; |
23 #endif | 34 #endif |
24 | 35 |
25 // Return a C++ string given vprintf-like input. | 36 // Return a C++ string given vprintf-like input. |
26 BASE_EXPORT std::string StringPrintV(const char* format, va_list ap) | 37 BASE_EXPORT std::string StringPrintV(const char* format, va_list ap) |
27 PRINTF_FORMAT(1, 0) WARN_UNUSED_RESULT; | 38 PRINTF_FORMAT(1, 0) WARN_UNUSED_RESULT; |
28 | 39 |
29 // Store result into a supplied string and return it. | 40 // Store result into a supplied string and return it. |
30 BASE_EXPORT const std::string& SStringPrintf(std::string* dst, | 41 BASE_EXPORT const std::string& SStringPrintf( |
31 const char* format, ...) | 42 std::string* dst, |
32 PRINTF_FORMAT(2, 3); | 43 _Printf_format_string_ const char* format, |
| 44 ...) PRINTF_FORMAT(2, 3); |
33 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
34 BASE_EXPORT const std::wstring& SStringPrintf(std::wstring* dst, | 46 BASE_EXPORT const std::wstring& SStringPrintf( |
35 const wchar_t* format, ...) | 47 std::wstring* dst, |
36 WPRINTF_FORMAT(2, 3); | 48 _Printf_format_string_ const wchar_t* format, |
| 49 ...) WPRINTF_FORMAT(2, 3); |
37 #endif | 50 #endif |
38 | 51 |
39 // Append result to a supplied string. | 52 // Append result to a supplied string. |
40 BASE_EXPORT void StringAppendF(std::string* dst, const char* format, ...) | 53 BASE_EXPORT void StringAppendF(std::string* dst, |
41 PRINTF_FORMAT(2, 3); | 54 _Printf_format_string_ const char* format, |
| 55 ...) PRINTF_FORMAT(2, 3); |
42 #if defined(OS_WIN) | 56 #if defined(OS_WIN) |
43 BASE_EXPORT void StringAppendF(std::wstring* dst, const wchar_t* format, ...) | 57 BASE_EXPORT void StringAppendF(std::wstring* dst, |
44 WPRINTF_FORMAT(2, 3); | 58 _Printf_format_string_ const wchar_t* format, |
| 59 ...) WPRINTF_FORMAT(2, 3); |
45 #endif | 60 #endif |
46 | 61 |
47 // Lower-level routine that takes a va_list and appends to a specified | 62 // Lower-level routine that takes a va_list and appends to a specified |
48 // string. All other routines are just convenience wrappers around it. | 63 // string. All other routines are just convenience wrappers around it. |
49 BASE_EXPORT void StringAppendV(std::string* dst, const char* format, va_list ap) | 64 BASE_EXPORT void StringAppendV(std::string* dst, const char* format, va_list ap) |
50 PRINTF_FORMAT(2, 0); | 65 PRINTF_FORMAT(2, 0); |
51 #if defined(OS_WIN) | 66 #if defined(OS_WIN) |
52 BASE_EXPORT void StringAppendV(std::wstring* dst, | 67 BASE_EXPORT void StringAppendV(std::wstring* dst, |
53 const wchar_t* format, va_list ap) | 68 const wchar_t* format, va_list ap) |
54 WPRINTF_FORMAT(2, 0); | 69 WPRINTF_FORMAT(2, 0); |
55 #endif | 70 #endif |
56 | 71 |
57 } // namespace base | 72 } // namespace base |
58 | 73 |
59 #endif // BASE_STRINGS_STRINGPRINTF_H_ | 74 #endif // BASE_STRINGS_STRINGPRINTF_H_ |
OLD | NEW |