Merge m-c to fx-team.
[gecko.git] / mfbt / Char16.h
blobfb182dfde206da5adfaae7e0cca9bb56da9d8fb4
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
13 * C11 and C++11 introduce a char16_t type and support for UTF-16 string and
14 * character literals. C++11's char16_t is a distinct builtin type. C11's
15 * char16_t is a typedef for uint_least16_t. Technically, char16_t is a 16-bit
16 * code unit of a Unicode code point, not a "character".
19 #ifdef _MSC_VER
21 * C++11 says char16_t is a distinct builtin type, but Windows's yvals.h
22 * typedefs char16_t as an unsigned short. We would like to alias char16_t
23 * to Windows's 16-bit wchar_t so we can declare UTF-16 literals as constant
24 * expressions (and pass char16_t pointers to Windows APIs). We #define
25 * _CHAR16T here in order to prevent yvals.h from overriding our char16_t
26 * typedefs, which we set to wchar_t for C++ code and to unsigned short for
27 * 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
34 # define _CHAR16T
35 # ifdef __cplusplus
36 typedef wchar_t char16_t;
37 # else
38 typedef unsigned short char16_t;
39 # endif
40 typedef unsigned int char32_t;
41 #elif defined(__cplusplus) && \
42 (__cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__))
43 /* C++11 has a builtin char16_t type. */
44 # define MOZ_UTF16_HELPER(s) u##s
45 /**
46 * This macro is used to distinguish when char16_t would be a distinct
47 * typedef from wchar_t.
49 # define MOZ_CHAR16_IS_NOT_WCHAR
50 #elif !defined(__cplusplus)
51 # if defined(WIN32)
52 # include <yvals.h>
53 typedef wchar_t char16_t;
54 # else
55 /**
56 * We can't use the stdint.h uint16_t type here because including
57 * stdint.h will break building some of our C libraries, such as
58 * sqlite.
60 typedef unsigned short char16_t;
61 # endif
62 #else
63 # error "Char16.h requires C++11 (or something like it) for UTF-16 support."
64 #endif
66 /* This is a temporary hack until bug 927728 is fixed. */
67 #define __PRUNICHAR__
68 typedef char16_t PRUnichar;
71 * Macro arguments used in concatenation or stringification won't be expanded.
72 * Therefore, in order for |MOZ_UTF16(FOO)| to work as expected (which is to
73 * expand |FOO| before doing whatever |MOZ_UTF16| needs to do to it) a helper
74 * macro, |MOZ_UTF16_HELPER| needs to be inserted in between to allow the macro
75 * argument to expand. See "3.10.6 Separate Expansion of Macro Arguments" of the
76 * CPP manual for a more accurate and precise explanation.
78 #define MOZ_UTF16(s) MOZ_UTF16_HELPER(s)
80 #if defined(__cplusplus) && \
81 (__cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__))
82 static_assert(sizeof(char16_t) == 2, "Is char16_t type 16 bits?");
83 static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?");
84 static_assert(sizeof(MOZ_UTF16('A')) == 2, "Is char literal 16 bits?");
85 static_assert(sizeof(MOZ_UTF16("")[0]) == 2, "Is string char 16 bits?");
86 #endif
88 #endif /* mozilla_Char16_h */