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".
21 # define MOZ_USE_CHAR16_WRAPPER
23 # include "mozilla/Attributes.h"
25 * Win32 API extensively uses wchar_t, which is represented by a separated
26 * builtin type than char16_t per spec. It's not the case for MSVC prior to
27 * MSVC 2015, but other compilers follow the spec. We want to mix wchar_t and
28 * char16_t on Windows builds. This class is supposed to make it easier. It
29 * stores char16_t const pointer, but provides implicit casts for wchar_t as
30 * well. On other platforms, we simply use
31 * |typedef const char16_t* char16ptr_t|. Here, we want to make the class as
32 * similar to this typedef, including providing some casts that are allowed
38 static_assert(sizeof(char16_t
) == sizeof(wchar_t),
39 "char16_t and wchar_t sizes differ");
42 constexpr MOZ_IMPLICIT
char16ptr_t(const char16_t
* aPtr
) : mPtr(aPtr
) {}
43 MOZ_IMPLICIT
char16ptr_t(const wchar_t* aPtr
)
44 : mPtr(reinterpret_cast<const char16_t
*>(aPtr
)) {}
46 /* Without this, nullptr assignment would be ambiguous. */
47 constexpr MOZ_IMPLICIT
char16ptr_t(decltype(nullptr)) : mPtr(nullptr) {}
49 constexpr operator const char16_t
*() const { return mPtr
; }
50 operator const wchar_t*() const {
51 return reinterpret_cast<const wchar_t*>(mPtr
);
55 return const_cast<wchar_t*>(reinterpret_cast<const wchar_t*>(mPtr
));
58 constexpr operator const void*() const { return mPtr
; }
59 constexpr explicit operator bool() const { return mPtr
!= nullptr; }
61 /* Explicit cast operators to allow things like (char16_t*)str. */
62 constexpr explicit operator char16_t
*() const {
63 return const_cast<char16_t
*>(mPtr
);
65 explicit operator wchar_t*() const {
66 return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
68 explicit operator int() const { return reinterpret_cast<intptr_t>(mPtr
); }
69 explicit operator unsigned int() const {
70 return reinterpret_cast<uintptr_t>(mPtr
);
72 explicit operator long() const { return reinterpret_cast<intptr_t>(mPtr
); }
73 explicit operator unsigned long() const {
74 return reinterpret_cast<uintptr_t>(mPtr
);
76 explicit operator long long() const {
77 return reinterpret_cast<intptr_t>(mPtr
);
79 explicit operator unsigned long long() const {
80 return reinterpret_cast<uintptr_t>(mPtr
);
84 * Some Windows API calls accept BYTE* but require that data actually be
85 * WCHAR*. Supporting this requires explicit operators to support the
86 * requisite explicit casts.
88 explicit operator const char*() const {
89 return reinterpret_cast<const char*>(mPtr
);
91 explicit operator const unsigned char*() const {
92 return reinterpret_cast<const unsigned char*>(mPtr
);
94 explicit operator unsigned char*() const {
95 return const_cast<unsigned char*>(
96 reinterpret_cast<const unsigned char*>(mPtr
));
98 explicit operator void*() const { return const_cast<char16_t
*>(mPtr
); }
100 /* Some operators used on pointers. */
101 char16_t
operator[](size_t aIndex
) const { return mPtr
[aIndex
]; }
102 bool operator==(const char16ptr_t
& aOther
) const {
103 return mPtr
== aOther
.mPtr
;
105 bool operator==(decltype(nullptr)) const { return mPtr
== nullptr; }
106 bool operator!=(const char16ptr_t
& aOther
) const {
107 return mPtr
!= aOther
.mPtr
;
109 bool operator!=(decltype(nullptr)) const { return mPtr
!= nullptr; }
110 char16ptr_t
operator+(int aValue
) const { return char16ptr_t(mPtr
+ aValue
); }
111 char16ptr_t
operator+(unsigned int aValue
) const {
112 return char16ptr_t(mPtr
+ aValue
);
114 char16ptr_t
operator+(long aValue
) const {
115 return char16ptr_t(mPtr
+ aValue
);
117 char16ptr_t
operator+(unsigned long aValue
) const {
118 return char16ptr_t(mPtr
+ aValue
);
120 char16ptr_t
operator+(long long aValue
) const {
121 return char16ptr_t(mPtr
+ aValue
);
123 char16ptr_t
operator+(unsigned long long aValue
) const {
124 return char16ptr_t(mPtr
+ aValue
);
126 ptrdiff_t operator-(const char16ptr_t
& aOther
) const {
127 return mPtr
- aOther
.mPtr
;
131 inline decltype((char*)0 - (char*)0) operator-(const char16_t
* aX
,
132 const char16ptr_t aY
) {
133 return aX
- static_cast<const char16_t
*>(aY
);
138 typedef const char16_t
* char16ptr_t
;
142 static_assert(sizeof(char16_t
) == 2, "Is char16_t type 16 bits?");
143 static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?");
144 static_assert(sizeof(u
'A') == 2, "Is unicode char literal 16 bits?");
145 static_assert(sizeof(u
""[0]) == 2, "Is unicode string char 16 bits?");
149 #endif /* mozilla_Char16_h */