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
39 static_assert(sizeof(char16_t
) == sizeof(wchar_t),
40 "char16_t and wchar_t sizes differ");
43 MOZ_IMPLICIT
char16ptr_t(const char16_t
* aPtr
) : mPtr(aPtr
) {}
44 MOZ_IMPLICIT
char16ptr_t(const wchar_t* aPtr
) :
45 mPtr(reinterpret_cast<const char16_t
*>(aPtr
))
48 /* Without this, nullptr assignment would be ambiguous. */
49 constexpr MOZ_IMPLICIT
char16ptr_t(decltype(nullptr)) : mPtr(nullptr) {}
51 operator const char16_t
*() const
55 operator const wchar_t*() const
57 return reinterpret_cast<const wchar_t*>(mPtr
);
62 return const_cast<wchar_t*>(reinterpret_cast<const wchar_t*>(mPtr
));
65 operator const void*() const
69 explicit operator bool() const
71 return mPtr
!= nullptr;
74 /* Explicit cast operators to allow things like (char16_t*)str. */
75 explicit operator char16_t
*() const
77 return const_cast<char16_t
*>(mPtr
);
79 explicit operator wchar_t*() const
81 return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
83 explicit operator int() const
85 return reinterpret_cast<intptr_t>(mPtr
);
87 explicit operator unsigned int() const
89 return reinterpret_cast<uintptr_t>(mPtr
);
91 explicit operator long() const
93 return reinterpret_cast<intptr_t>(mPtr
);
95 explicit operator unsigned long() const
97 return reinterpret_cast<uintptr_t>(mPtr
);
99 explicit operator long long() const
101 return reinterpret_cast<intptr_t>(mPtr
);
103 explicit operator unsigned long long() const
105 return reinterpret_cast<uintptr_t>(mPtr
);
109 * Some Windows API calls accept BYTE* but require that data actually be
110 * WCHAR*. Supporting this requires explicit operators to support the
111 * requisite explicit casts.
113 explicit operator const char*() const
115 return reinterpret_cast<const char*>(mPtr
);
117 explicit operator const unsigned char*() const
119 return reinterpret_cast<const unsigned char*>(mPtr
);
121 explicit operator unsigned char*() const
124 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mPtr
));
126 explicit operator void*() const
128 return const_cast<char16_t
*>(mPtr
);
131 /* Some operators used on pointers. */
132 char16_t
operator[](size_t aIndex
) const
136 bool operator==(const char16ptr_t
& aOther
) const
138 return mPtr
== aOther
.mPtr
;
140 bool operator==(decltype(nullptr)) const
142 return mPtr
== nullptr;
144 bool operator!=(const char16ptr_t
& aOther
) const
146 return mPtr
!= aOther
.mPtr
;
148 bool operator!=(decltype(nullptr)) const
150 return mPtr
!= nullptr;
152 char16ptr_t
operator+(int aValue
) const
154 return char16ptr_t(mPtr
+ aValue
);
156 char16ptr_t
operator+(unsigned int aValue
) const
158 return char16ptr_t(mPtr
+ aValue
);
160 char16ptr_t
operator+(long aValue
) const
162 return char16ptr_t(mPtr
+ aValue
);
164 char16ptr_t
operator+(unsigned long aValue
) const
166 return char16ptr_t(mPtr
+ aValue
);
168 char16ptr_t
operator+(long long aValue
) const
170 return char16ptr_t(mPtr
+ aValue
);
172 char16ptr_t
operator+(unsigned long long aValue
) const
174 return char16ptr_t(mPtr
+ aValue
);
176 ptrdiff_t operator-(const char16ptr_t
& aOther
) const
178 return mPtr
- aOther
.mPtr
;
182 inline decltype((char*)0-(char*)0)
183 operator-(const char16_t
* aX
, const char16ptr_t aY
)
185 return aX
- static_cast<const char16_t
*>(aY
);
190 typedef const char16_t
* char16ptr_t
;
194 static_assert(sizeof(char16_t
) == 2, "Is char16_t type 16 bits?");
195 static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?");
196 static_assert(sizeof(u
'A') == 2, "Is unicode char literal 16 bits?");
197 static_assert(sizeof(u
""[0]) == 2, "Is unicode string char 16 bits?");
201 #endif /* mozilla_Char16_h */