stringprintf.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. [[nodiscard]] BASE_EXPORT std::string StringPrintf(const char* format, ...)
  14. PRINTF_FORMAT(1, 2);
  15. #if BUILDFLAG(IS_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. [[nodiscard]] BASE_EXPORT std::wstring StringPrintf(const wchar_t* format, ...)
  21. WPRINTF_FORMAT(1, 2);
  22. [[nodiscard]] BASE_EXPORT std::u16string StringPrintf(const char16_t* format,
  23. ...) WPRINTF_FORMAT(1, 2);
  24. #endif
  25. // Return a C++ string given vprintf-like input.
  26. [[nodiscard]] BASE_EXPORT std::string StringPrintV(const char* format,
  27. va_list ap)
  28. PRINTF_FORMAT(1, 0);
  29. // Store result into a supplied string and return it.
  30. BASE_EXPORT const std::string& SStringPrintf(std::string* dst,
  31. const char* format,
  32. ...) PRINTF_FORMAT(2, 3);
  33. #if BUILDFLAG(IS_WIN)
  34. BASE_EXPORT const std::wstring& SStringPrintf(std::wstring* dst,
  35. const wchar_t* format,
  36. ...) WPRINTF_FORMAT(2, 3);
  37. BASE_EXPORT const std::u16string& SStringPrintf(std::u16string* dst,
  38. const char16_t* format,
  39. ...) WPRINTF_FORMAT(2, 3);
  40. #endif
  41. // Append result to a supplied string.
  42. BASE_EXPORT void StringAppendF(std::string* dst, const char* format, ...)
  43. PRINTF_FORMAT(2, 3);
  44. #if BUILDFLAG(IS_WIN)
  45. BASE_EXPORT void StringAppendF(std::wstring* dst, const wchar_t* format, ...)
  46. WPRINTF_FORMAT(2, 3);
  47. BASE_EXPORT void StringAppendF(std::u16string* dst, const char16_t* format, ...)
  48. WPRINTF_FORMAT(2, 3);
  49. #endif
  50. // Lower-level routine that takes a va_list and appends to a specified
  51. // string. All other routines are just convenience wrappers around it.
  52. BASE_EXPORT void StringAppendV(std::string* dst, const char* format, va_list ap)
  53. PRINTF_FORMAT(2, 0);
  54. #if BUILDFLAG(IS_WIN)
  55. BASE_EXPORT void StringAppendV(std::wstring* dst,
  56. const wchar_t* format,
  57. va_list ap) WPRINTF_FORMAT(2, 0);
  58. BASE_EXPORT void StringAppendV(std::u16string* dst,
  59. const char16_t* format,
  60. va_list ap) WPRINTF_FORMAT(2, 0);
  61. #endif
  62. } // namespace base
  63. #endif // BASE_STRINGS_STRINGPRINTF_H_