1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_STRINGS_UTF_STRING_CONVERSIONS_H_
6 #define BASE_STRINGS_UTF_STRING_CONVERSIONS_H_
7
8 #include <stddef.h>
9
10 #include <string>
11 #include <string_view>
12
13 #include "base/base_export.h"
14 #include "base/strings/string_piece.h"
15 #include "base/types/always_false.h"
16 #include "build/build_config.h"
17
18 namespace base {
19
20 // These convert between UTF-8, -16, and -32 strings. They are potentially slow,
21 // so avoid unnecessary conversions. The low-level versions return a boolean
22 // indicating whether the conversion was 100% valid. In this case, it will still
23 // do the best it can and put the result in the output buffer. The versions that
24 // return strings ignore this error and just return the best conversion
25 // possible.
26 BASE_EXPORT bool WideToUTF8(const wchar_t* src, size_t src_len,
27 std::string* output);
28 [[nodiscard]] BASE_EXPORT std::string WideToUTF8(std::wstring_view wide);
29 BASE_EXPORT bool UTF8ToWide(const char* src, size_t src_len,
30 std::wstring* output);
31 [[nodiscard]] BASE_EXPORT std::wstring UTF8ToWide(StringPiece utf8);
32
33 BASE_EXPORT bool WideToUTF16(const wchar_t* src,
34 size_t src_len,
35 std::u16string* output);
36 [[nodiscard]] BASE_EXPORT std::u16string WideToUTF16(std::wstring_view wide);
37 BASE_EXPORT bool UTF16ToWide(const char16_t* src,
38 size_t src_len,
39 std::wstring* output);
40 [[nodiscard]] BASE_EXPORT std::wstring UTF16ToWide(StringPiece16 utf16);
41
42 BASE_EXPORT bool UTF8ToUTF16(const char* src,
43 size_t src_len,
44 std::u16string* output);
45 [[nodiscard]] BASE_EXPORT std::u16string UTF8ToUTF16(StringPiece utf8);
46 BASE_EXPORT bool UTF16ToUTF8(const char16_t* src,
47 size_t src_len,
48 std::string* output);
49 [[nodiscard]] BASE_EXPORT std::string UTF16ToUTF8(StringPiece16 utf16);
50
51 // This converts an ASCII string, typically a hardcoded constant, to a UTF16
52 // string.
53 [[nodiscard]] BASE_EXPORT std::u16string ASCIIToUTF16(StringPiece ascii);
54
55 // Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
56 // beforehand.
57 [[nodiscard]] BASE_EXPORT std::string UTF16ToASCII(StringPiece16 utf16);
58
59 #if defined(WCHAR_T_IS_16_BIT)
60 // This converts an ASCII string, typically a hardcoded constant, to a wide
61 // string.
62 [[nodiscard]] BASE_EXPORT std::wstring ASCIIToWide(StringPiece ascii);
63
64 // Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
65 // beforehand.
66 [[nodiscard]] BASE_EXPORT std::string WideToASCII(std::wstring_view wide);
67 #endif // defined(WCHAR_T_IS_16_BIT)
68
69 // The conversion functions in this file should not be used to convert string
70 // literals. Instead, the corresponding prefixes (e.g. u"" for UTF16 or L"" for
71 // Wide) should be used. Catch those cases with overloads that assert at compile
72 // time.
73 template <size_t N>
WideToUTF16(const wchar_t (& str)[N])74 [[noreturn]] std::u16string WideToUTF16(const wchar_t (&str)[N]) {
75 static_assert(AlwaysFalse<decltype(N)>,
76 "Error: Use u\"...\" to create a std::u16string literal.");
77 }
78
79 template <size_t N>
UTF8ToUTF16(const char (& str)[N])80 [[noreturn]] std::u16string UTF8ToUTF16(const char (&str)[N]) {
81 static_assert(AlwaysFalse<decltype(N)>,
82 "Error: Use u\"...\" to create a std::u16string literal.");
83 }
84
85 template <size_t N>
ASCIIToUTF16(const char (& str)[N])86 [[noreturn]] std::u16string ASCIIToUTF16(const char (&str)[N]) {
87 static_assert(AlwaysFalse<decltype(N)>,
88 "Error: Use u\"...\" to create a std::u16string literal.");
89 }
90
91 // Mutable character arrays are usually only populated during runtime. Continue
92 // to allow this conversion.
93 template <size_t N>
ASCIIToUTF16(char (& str)[N])94 std::u16string ASCIIToUTF16(char (&str)[N]) {
95 return ASCIIToUTF16(StringPiece(str));
96 }
97
98 } // namespace base
99
100 #endif // BASE_STRINGS_UTF_STRING_CONVERSIONS_H_
101