Char16.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/. */
  6. /* Implements a UTF-16 character type. */
  7. #ifndef mozilla_Char16_h
  8. #define mozilla_Char16_h
  9. #ifdef __cplusplus
  10. /*
  11. * C++11 introduces a char16_t type and support for UTF-16 string and character
  12. * literals. C++11's char16_t is a distinct builtin type. Technically, char16_t
  13. * is a 16-bit code unit of a Unicode code point, not a "character".
  14. */
  15. #ifdef WIN32
  16. # define MOZ_USE_CHAR16_WRAPPER
  17. # include <cstdint>
  18. /**
  19. * Win32 API extensively uses wchar_t, which is represented by a separated
  20. * builtin type than char16_t per spec. It's not the case for MSVC prior to
  21. * MSVC 2015, but other compilers follow the spec. We want to mix wchar_t and
  22. * char16_t on Windows builds. This class is supposed to make it easier. It
  23. * stores char16_t const pointer, but provides implicit casts for wchar_t as
  24. * well. On other platforms, we simply use
  25. * |typedef const char16_t* char16ptr_t|. Here, we want to make the class as
  26. * similar to this typedef, including providing some casts that are allowed
  27. * by the typedef.
  28. */
  29. class char16ptr_t
  30. {
  31. private:
  32. const char16_t* mPtr;
  33. static_assert(sizeof(char16_t) == sizeof(wchar_t),
  34. "char16_t and wchar_t sizes differ");
  35. public:
  36. char16ptr_t(const char16_t* aPtr) : mPtr(aPtr) {}
  37. char16ptr_t(const wchar_t* aPtr) :
  38. mPtr(reinterpret_cast<const char16_t*>(aPtr))
  39. {}
  40. /* Without this, nullptr assignment would be ambiguous. */
  41. constexpr char16ptr_t(decltype(nullptr)) : mPtr(nullptr) {}
  42. operator const char16_t*() const
  43. {
  44. return mPtr;
  45. }
  46. operator const wchar_t*() const
  47. {
  48. return reinterpret_cast<const wchar_t*>(mPtr);
  49. }
  50. operator const void*() const
  51. {
  52. return mPtr;
  53. }
  54. operator bool() const
  55. {
  56. return mPtr != nullptr;
  57. }
  58. /* Explicit cast operators to allow things like (char16_t*)str. */
  59. explicit operator char16_t*() const
  60. {
  61. return const_cast<char16_t*>(mPtr);
  62. }
  63. explicit operator wchar_t*() const
  64. {
  65. return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
  66. }
  67. explicit operator int() const
  68. {
  69. return reinterpret_cast<intptr_t>(mPtr);
  70. }
  71. explicit operator unsigned int() const
  72. {
  73. return reinterpret_cast<uintptr_t>(mPtr);
  74. }
  75. explicit operator long() const
  76. {
  77. return reinterpret_cast<intptr_t>(mPtr);
  78. }
  79. explicit operator unsigned long() const
  80. {
  81. return reinterpret_cast<uintptr_t>(mPtr);
  82. }
  83. explicit operator long long() const
  84. {
  85. return reinterpret_cast<intptr_t>(mPtr);
  86. }
  87. explicit operator unsigned long long() const
  88. {
  89. return reinterpret_cast<uintptr_t>(mPtr);
  90. }
  91. /**
  92. * Some Windows API calls accept BYTE* but require that data actually be
  93. * WCHAR*. Supporting this requires explicit operators to support the
  94. * requisite explicit casts.
  95. */
  96. explicit operator const char*() const
  97. {
  98. return reinterpret_cast<const char*>(mPtr);
  99. }
  100. explicit operator const unsigned char*() const
  101. {
  102. return reinterpret_cast<const unsigned char*>(mPtr);
  103. }
  104. explicit operator unsigned char*() const
  105. {
  106. return
  107. const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mPtr));
  108. }
  109. explicit operator void*() const
  110. {
  111. return const_cast<char16_t*>(mPtr);
  112. }
  113. /* Some operators used on pointers. */
  114. char16_t operator[](size_t aIndex) const
  115. {
  116. return mPtr[aIndex];
  117. }
  118. bool operator==(const char16ptr_t& aOther) const
  119. {
  120. return mPtr == aOther.mPtr;
  121. }
  122. bool operator==(decltype(nullptr)) const
  123. {
  124. return mPtr == nullptr;
  125. }
  126. bool operator!=(const char16ptr_t& aOther) const
  127. {
  128. return mPtr != aOther.mPtr;
  129. }
  130. bool operator!=(decltype(nullptr)) const
  131. {
  132. return mPtr != nullptr;
  133. }
  134. char16ptr_t operator+(int aValue) const
  135. {
  136. return char16ptr_t(mPtr + aValue);
  137. }
  138. char16ptr_t operator+(unsigned int aValue) const
  139. {
  140. return char16ptr_t(mPtr + aValue);
  141. }
  142. char16ptr_t operator+(long aValue) const
  143. {
  144. return char16ptr_t(mPtr + aValue);
  145. }
  146. char16ptr_t operator+(unsigned long aValue) const
  147. {
  148. return char16ptr_t(mPtr + aValue);
  149. }
  150. char16ptr_t operator+(long long aValue) const
  151. {
  152. return char16ptr_t(mPtr + aValue);
  153. }
  154. char16ptr_t operator+(unsigned long long aValue) const
  155. {
  156. return char16ptr_t(mPtr + aValue);
  157. }
  158. ptrdiff_t operator-(const char16ptr_t& aOther) const
  159. {
  160. return mPtr - aOther.mPtr;
  161. }
  162. };
  163. inline decltype((char*)0-(char*)0)
  164. operator-(const char16_t* aX, const char16ptr_t aY)
  165. {
  166. return aX - static_cast<const char16_t*>(aY);
  167. }
  168. #else
  169. typedef const char16_t* char16ptr_t;
  170. #endif
  171. static_assert(sizeof(char16_t) == 2, "Is char16_t type 16 bits?");
  172. static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?");
  173. static_assert(sizeof(u'A') == 2, "Is unicode char literal 16 bits?");
  174. static_assert(sizeof(u""[0]) == 2, "Is unicode string char 16 bits?");
  175. #endif
  176. #endif /* mozilla_Char16_h */