stringprintf.h 2.9 KB

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