| 6 | #define BASE_STRINGS_STRINGPRINTF_H_ |
| 7 |
| 8 | #include <stdarg.h> // va_list |
| 9 |
| 10 | #include <string> |
| 11 |
| 12 | #include "base/base_export.h" |
| 13 | #include "base/compiler_specific.h" |
| 14 | #include "build/build_config.h" |
| 15 |
| 16 | namespace base { |
| 17 |
| 18 | // Return a C++ string given printf-like input. |
| 19 | BASE_EXPORT std::string StringPrintf(const char* format, ...) |
| 20 | PRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT; |
| 21 | #if defined(OS_WIN) |
| 22 | // Note: Unfortunately compile time checking of the format string for UTF-16 |
| 23 | // strings is not supported by any compiler, thus these functions should be used |
| 24 | // carefully and sparingly. Also applies to SStringPrintf and StringAppendV |
| 25 | // below. |
| 26 | BASE_EXPORT std::wstring StringPrintf(const wchar_t* format, ...) |
| 27 | WPRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT; |
| 28 | BASE_EXPORT std::u16string StringPrintf(const char16_t* format, ...) |
| 29 | WPRINTF_FORMAT(1, 2) WARN_UNUSED_RESULT; |
| 30 | #endif |
| 31 |
| 32 | // Return a C++ string given vprintf-like input. |
| 33 | BASE_EXPORT std::string StringPrintV(const char* format, va_list ap) |
| 34 | PRINTF_FORMAT(1, 0) WARN_UNUSED_RESULT; |
| 35 |
| 36 | // Store result into a supplied string and return it. |
| 37 | BASE_EXPORT const std::string& SStringPrintf(std::string* dst, |
| 38 | const char* format, |
| 39 | ...) PRINTF_FORMAT(2, 3); |
| 40 | #if defined(OS_WIN) |
| 41 | BASE_EXPORT const std::wstring& SStringPrintf(std::wstring* dst, |
| 42 | const wchar_t* format, |
| 43 | ...) WPRINTF_FORMAT(2, 3); |
| 44 | BASE_EXPORT const std::u16string& SStringPrintf(std::u16string* dst, |
| 45 | const char16_t* format, |
| 46 | ...) WPRINTF_FORMAT(2, 3); |
| 47 | #endif |
| 48 |
| 49 | // Append result to a supplied string. |
| 50 | BASE_EXPORT void StringAppendF(std::string* dst, const char* format, ...) |
| 51 | PRINTF_FORMAT(2, 3); |
| 52 | #if defined(OS_WIN) |
| 53 | BASE_EXPORT void StringAppendF(std::wstring* dst, const wchar_t* format, ...) |
| 54 | WPRINTF_FORMAT(2, 3); |
| 55 | BASE_EXPORT void StringAppendF(std::u16string* dst, const char16_t* format, ...) |
| 56 | WPRINTF_FORMAT(2, 3); |
| 57 | #endif |
| 58 |
| 59 | // Lower-level routine that takes a va_list and appends to a specified |
| 60 | // string. All other routines are just convenience wrappers around it. |
| 61 | BASE_EXPORT void StringAppendV(std::string* dst, const char* format, va_list ap) |
| 62 | PRINTF_FORMAT(2, 0); |
| 63 | #if defined(OS_WIN) |
| 64 | BASE_EXPORT void StringAppendV(std::wstring* dst, |
| 65 | const wchar_t* format, |
| 66 | va_list ap) WPRINTF_FORMAT(2, 0); |
| 67 | BASE_EXPORT void StringAppendV(std::u16string* dst, |
| 68 | const char16_t* format, |
| 69 | va_list ap) WPRINTF_FORMAT(2, 0); |
| 70 | #endif |
| 71 |
| 72 | } // namespace base |
| 73 |
| 74 | #endif // BASE_STRINGS_STRINGPRINTF_H_ |
| 75 |