1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Implements a UTF-16 character type. */
9 #ifndef mozilla_Char16_h
10 #define mozilla_Char16_h
15 * C++11 introduces a char16_t type and support for UTF-16 string and character
16 * literals. C++11's char16_t is a distinct builtin type. Technically, char16_t
17 * is a 16-bit code unit of a Unicode code point, not a "character".
22 * C++11 says char16_t is a distinct builtin type, but Windows's yvals.h
23 * typedefs char16_t as an unsigned short. We would like to alias char16_t
24 * to Windows's 16-bit wchar_t so we can declare UTF-16 literals as constant
25 * expressions (and pass char16_t pointers to Windows APIs). We #define
26 * _CHAR16T here in order to prevent yvals.h from overriding our char16_t
27 * typedefs, which we set to wchar_t for C++ code.
29 * In addition, #defining _CHAR16T will prevent yvals.h from defining a
30 * char32_t type, so we have to undo that damage here and provide our own,
31 * which is identical to the yvals.h type.
33 # define MOZ_UTF16_HELPER(s) L##s
35 typedef wchar_t char16_t
;
36 typedef unsigned int char32_t
;
38 /* C++11 has a builtin char16_t type. */
39 # define MOZ_UTF16_HELPER(s) u##s
41 * This macro is used to distinguish when char16_t would be a distinct
42 * typedef from wchar_t.
44 # define MOZ_CHAR16_IS_NOT_WCHAR
46 # define MOZ_USE_CHAR16_WRAPPER
50 #ifdef MOZ_USE_CHAR16_WRAPPER
53 * Win32 API extensively uses wchar_t, which is represented by a separated
54 * builtin type than char16_t per spec. It's not the case for MSVC, but GCC
55 * follows the spec. We want to mix wchar_t and char16_t on Windows builds.
56 * This class is supposed to make it easier. It stores char16_t const pointer,
57 * but provides implicit casts for wchar_t as well. On other platforms, we
58 * simply use |typedef const char16_t* char16ptr_t|. Here, we want to make
59 * the class as similar to this typedef, including providing some casts that
60 * are allowed by the typedef.
66 static_assert(sizeof(char16_t
) == sizeof(wchar_t),
67 "char16_t and wchar_t sizes differ");
70 char16ptr_t(const char16_t
* aPtr
) : mPtr(aPtr
) {}
71 char16ptr_t(const wchar_t* aPtr
) :
72 mPtr(reinterpret_cast<const char16_t
*>(aPtr
))
75 /* Without this, nullptr assignment would be ambiguous. */
76 constexpr char16ptr_t(decltype(nullptr)) : mPtr(nullptr) {}
78 operator const char16_t
*() const
82 operator const wchar_t*() const
84 return reinterpret_cast<const wchar_t*>(mPtr
);
86 operator const void*() const
92 return mPtr
!= nullptr;
94 operator std::wstring() const
96 return std::wstring(static_cast<const wchar_t*>(*this));
99 /* Explicit cast operators to allow things like (char16_t*)str. */
100 explicit operator char16_t
*() const
102 return const_cast<char16_t
*>(mPtr
);
104 explicit operator wchar_t*() const
106 return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
108 explicit operator int() const
110 return reinterpret_cast<intptr_t>(mPtr
);
112 explicit operator unsigned int() const
114 return reinterpret_cast<uintptr_t>(mPtr
);
116 explicit operator long() const
118 return reinterpret_cast<intptr_t>(mPtr
);
120 explicit operator unsigned long() const
122 return reinterpret_cast<uintptr_t>(mPtr
);
124 explicit operator long long() const
126 return reinterpret_cast<intptr_t>(mPtr
);
128 explicit operator unsigned long long() const
130 return reinterpret_cast<uintptr_t>(mPtr
);
134 * Some Windows API calls accept BYTE* but require that data actually be
135 * WCHAR*. Supporting this requires explicit operators to support the
136 * requisite explicit casts.
138 explicit operator const char*() const
140 return reinterpret_cast<const char*>(mPtr
);
142 explicit operator const unsigned char*() const
144 return reinterpret_cast<const unsigned char*>(mPtr
);
146 explicit operator unsigned char*() const
149 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mPtr
));
151 explicit operator void*() const
153 return const_cast<char16_t
*>(mPtr
);
156 /* Some operators used on pointers. */
157 char16_t
operator[](size_t aIndex
) const
161 bool operator==(const char16ptr_t
& aOther
) const
163 return mPtr
== aOther
.mPtr
;
165 bool operator==(decltype(nullptr)) const
167 return mPtr
== nullptr;
169 bool operator!=(const char16ptr_t
& aOther
) const
171 return mPtr
!= aOther
.mPtr
;
173 bool operator!=(decltype(nullptr)) const
175 return mPtr
!= nullptr;
177 char16ptr_t
operator+(int aValue
) const
179 return char16ptr_t(mPtr
+ aValue
);
181 char16ptr_t
operator+(unsigned int aValue
) const
183 return char16ptr_t(mPtr
+ aValue
);
185 char16ptr_t
operator+(long aValue
) const
187 return char16ptr_t(mPtr
+ aValue
);
189 char16ptr_t
operator+(unsigned long aValue
) const
191 return char16ptr_t(mPtr
+ aValue
);
193 char16ptr_t
operator+(long long aValue
) const
195 return char16ptr_t(mPtr
+ aValue
);
197 char16ptr_t
operator+(unsigned long long aValue
) const
199 return char16ptr_t(mPtr
+ aValue
);
201 ptrdiff_t operator-(const char16ptr_t
& aOther
) const
203 return mPtr
- aOther
.mPtr
;
207 inline decltype((char*)0-(char*)0)
208 operator-(const char16_t
* aX
, const char16ptr_t aY
)
210 return aX
- static_cast<const char16_t
*>(aY
);
215 typedef const char16_t
* char16ptr_t
;
220 * Macro arguments used in concatenation or stringification won't be expanded.
221 * Therefore, in order for |MOZ_UTF16(FOO)| to work as expected (which is to
222 * expand |FOO| before doing whatever |MOZ_UTF16| needs to do to it) a helper
223 * macro, |MOZ_UTF16_HELPER| needs to be inserted in between to allow the macro
224 * argument to expand. See "3.10.6 Separate Expansion of Macro Arguments" of the
225 * CPP manual for a more accurate and precise explanation.
227 #define MOZ_UTF16(s) MOZ_UTF16_HELPER(s)
229 static_assert(sizeof(char16_t
) == 2, "Is char16_t type 16 bits?");
230 static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?");
231 static_assert(sizeof(MOZ_UTF16('A')) == 2, "Is char literal 16 bits?");
232 static_assert(sizeof(MOZ_UTF16("")[0]) == 2, "Is string char 16 bits?");
236 #endif /* mozilla_Char16_h */