utf_string_conversions.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2011 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_UTF_STRING_CONVERSIONS_H_
  5. #define BASE_STRINGS_UTF_STRING_CONVERSIONS_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/strings/string_piece.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. // These convert between UTF-8, -16, and -32 strings. They are potentially slow,
  13. // so avoid unnecessary conversions. The low-level versions return a boolean
  14. // indicating whether the conversion was 100% valid. In this case, it will still
  15. // do the best it can and put the result in the output buffer. The versions that
  16. // return strings ignore this error and just return the best conversion
  17. // possible.
  18. BASE_EXPORT bool WideToUTF8(const wchar_t* src, size_t src_len,
  19. std::string* output);
  20. [[nodiscard]] BASE_EXPORT std::string WideToUTF8(WStringPiece wide);
  21. BASE_EXPORT bool UTF8ToWide(const char* src, size_t src_len,
  22. std::wstring* output);
  23. [[nodiscard]] BASE_EXPORT std::wstring UTF8ToWide(StringPiece utf8);
  24. BASE_EXPORT bool WideToUTF16(const wchar_t* src,
  25. size_t src_len,
  26. std::u16string* output);
  27. [[nodiscard]] BASE_EXPORT std::u16string WideToUTF16(WStringPiece wide);
  28. BASE_EXPORT bool UTF16ToWide(const char16_t* src,
  29. size_t src_len,
  30. std::wstring* output);
  31. [[nodiscard]] BASE_EXPORT std::wstring UTF16ToWide(StringPiece16 utf16);
  32. BASE_EXPORT bool UTF8ToUTF16(const char* src,
  33. size_t src_len,
  34. std::u16string* output);
  35. [[nodiscard]] BASE_EXPORT std::u16string UTF8ToUTF16(StringPiece utf8);
  36. BASE_EXPORT bool UTF16ToUTF8(const char16_t* src,
  37. size_t src_len,
  38. std::string* output);
  39. [[nodiscard]] BASE_EXPORT std::string UTF16ToUTF8(StringPiece16 utf16);
  40. // This converts an ASCII string, typically a hardcoded constant, to a UTF16
  41. // string.
  42. [[nodiscard]] BASE_EXPORT std::u16string ASCIIToUTF16(StringPiece ascii);
  43. // Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
  44. // beforehand.
  45. [[nodiscard]] BASE_EXPORT std::string UTF16ToASCII(StringPiece16 utf16);
  46. #if defined(WCHAR_T_IS_UTF16)
  47. // This converts an ASCII string, typically a hardcoded constant, to a wide
  48. // string.
  49. [[nodiscard]] BASE_EXPORT std::wstring ASCIIToWide(StringPiece ascii);
  50. // Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
  51. // beforehand.
  52. [[nodiscard]] BASE_EXPORT std::string WideToASCII(WStringPiece wide);
  53. #endif // defined(WCHAR_T_IS_UTF16)
  54. // The conversion functions in this file should not be used to convert string
  55. // literals. Instead, the corresponding prefixes (e.g. u"" for UTF16 or L"" for
  56. // Wide) should be used. Deleting the overloads here catches these cases at
  57. // compile time.
  58. template <size_t N>
  59. std::u16string WideToUTF16(const wchar_t (&str)[N]) {
  60. static_assert(N == 0, "Error: Use the u\"...\" prefix instead.");
  61. return std::u16string();
  62. }
  63. template <size_t N>
  64. std::u16string UTF8ToUTF16(const char (&str)[N]) {
  65. static_assert(N == 0, "Error: Use the u\"...\" prefix instead.");
  66. return std::u16string();
  67. }
  68. template <size_t N>
  69. std::u16string ASCIIToUTF16(const char (&str)[N]) {
  70. static_assert(N == 0, "Error: Use the u\"...\" prefix instead.");
  71. return std::u16string();
  72. }
  73. // Mutable character arrays are usually only populated during runtime. Continue
  74. // to allow this conversion.
  75. template <size_t N>
  76. std::u16string ASCIIToUTF16(char (&str)[N]) {
  77. return ASCIIToUTF16(StringPiece(str));
  78. }
  79. } // namespace base
  80. #endif // BASE_STRINGS_UTF_STRING_CONVERSIONS_H_