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