1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qglobal.h>
57 Q_DECL_CONSTEXPR inline explicit QLatin1Char(char c) : ch(c) {}
58 Q_DECL_CONSTEXPR inline char toLatin1() const { return ch; }
59 Q_DECL_CONSTEXPR inline ushort unicode() const { return ushort(uchar(ch)); }
66 class Q_CORE_EXPORT QChar {
68 enum SpecialCharacter {
71 ReplacementCharacter = 0xfffd,
72 ObjectReplacementCharacter = 0xfffc,
73 ByteOrderMark = 0xfeff,
74 ByteOrderSwapped = 0xfffe,
75 ParagraphSeparator = 0x2029,
76 LineSeparator = 0x2028
79 Q_DECL_CONSTEXPR QChar() : ucs(0) {}
80 Q_DECL_CONSTEXPR QChar(ushort rc) : ucs(rc){} // implicit
81 Q_DECL_CONSTEXPR QChar(uchar c, uchar r) : ucs(ushort((r << 8) | c)){}
82 Q_DECL_CONSTEXPR QChar(short rc) : ucs(ushort(rc)){} // implicit
83 Q_DECL_CONSTEXPR QChar(uint rc) : ucs(ushort(rc & 0xffff)){}
84 Q_DECL_CONSTEXPR QChar(int rc) : ucs(ushort(rc & 0xffff)){}
85 Q_DECL_CONSTEXPR QChar(SpecialCharacter s) : ucs(ushort(s)) {} // implicit
86 Q_DECL_CONSTEXPR QChar(QLatin1Char ch) : ucs(ch.unicode()) {} // implicit
88 #ifndef QT_NO_CAST_FROM_ASCII
89 QT_ASCII_CAST_WARN Q_DECL_CONSTEXPR explicit QChar(char c) : ucs(uchar(c)) { }
90 QT_ASCII_CAST_WARN Q_DECL_CONSTEXPR explicit QChar(uchar c) : ucs(c) { }
92 // Unicode information
96 Mark_NonSpacing, // Mn
97 Mark_SpacingCombining, // Mc
100 Number_DecimalDigit, // Nd
104 Separator_Space, // Zs
105 Separator_Line, // Zl
106 Separator_Paragraph, // Zp
110 Other_Surrogate, // Cs
111 Other_PrivateUse, // Co
112 Other_NotAssigned, // Cn
114 Letter_Uppercase, // Lu
115 Letter_Lowercase, // Ll
116 Letter_Titlecase, // Lt
117 Letter_Modifier, // Lm
120 Punctuation_Connector, // Pc
121 Punctuation_Dash, // Pd
122 Punctuation_Open, // Ps
123 Punctuation_Close, // Pe
124 Punctuation_InitialQuote, // Pi
125 Punctuation_FinalQuote, // Pf
126 Punctuation_Other, // Po
129 Symbol_Currency, // Sc
130 Symbol_Modifier, // Sk
136 DirL, DirR, DirEN, DirES, DirET, DirAN, DirCS, DirB, DirS, DirWS, DirON,
137 DirLRE, DirLRO, DirAL, DirRLE, DirRLO, DirPDF, DirNSM, DirBN
164 OtherJoining, Dual, Right, Center
169 Combining_BelowLeftAttached = 200,
170 Combining_BelowAttached = 202,
171 Combining_BelowRightAttached = 204,
172 Combining_LeftAttached = 208,
173 Combining_RightAttached = 210,
174 Combining_AboveLeftAttached = 212,
175 Combining_AboveAttached = 214,
176 Combining_AboveRightAttached = 216,
178 Combining_BelowLeft = 218,
179 Combining_Below = 220,
180 Combining_BelowRight = 222,
181 Combining_Left = 224,
182 Combining_Right = 226,
183 Combining_AboveLeft = 228,
184 Combining_Above = 230,
185 Combining_AboveRight = 232,
187 Combining_DoubleBelow = 233,
188 Combining_DoubleAbove = 234,
189 Combining_IotaSubscript = 240
192 enum UnicodeVersion {
193 Unicode_Unassigned, // ### Qt 5: assign with some constantly big value
204 // ****** WHEN ADDING FUNCTIONS, CONSIDER ADDING TO QCharRef TOO
206 Category category() const;
207 Direction direction() const;
208 Joining joining() const;
209 bool hasMirrored() const;
210 unsigned char combiningClass() const;
212 QChar mirroredChar() const;
213 QString decomposition() const;
214 Decomposition decompositionTag() const;
216 int digitValue() const;
217 QChar toLower() const;
218 QChar toUpper() const;
219 QChar toTitleCase() const;
220 QChar toCaseFolded() const;
222 UnicodeVersion unicodeVersion() const;
224 inline char toAscii() const;
225 inline char toLatin1() const;
226 inline ushort unicode() const { return ucs; }
227 inline ushort &unicode() { return ucs; }
229 static inline QChar fromAscii(char c);
230 static inline QChar fromLatin1(char c);
232 inline bool isNull() const { return ucs == 0; }
233 bool isPrint() const;
234 bool isPunct() const;
235 inline bool isSpace() const {
236 return ucs == 0x20 || (ucs <= 0x0D && ucs >= 0x09)
237 || (ucs > 127 && (ucs == 0x0085 || isSpace(ucs)));
240 inline bool isLetter() const {
241 return (ucs >= 'a' && ucs <= 'z')
242 || (ucs <= 'Z' && ucs >= 'A')
243 || (ucs > 127 && isLetter(ucs));
245 bool isNumber() const;
246 inline bool isLetterOrNumber() const
248 return (ucs >= 'a' && ucs <= 'z')
249 || (ucs <= 'Z' && ucs >= 'A')
250 || (ucs <= '9' && ucs >= '0')
251 || (ucs > 127 && isLetterOrNumber(ucs));
253 inline bool isDigit() const
254 { return (ucs <= '9' && ucs >= '0') || (ucs > 127 && isDigit(ucs)); }
255 bool isSymbol() const;
256 inline bool isLower() const {
257 return (ucs >= 'a' && ucs <= 'z')
258 || (ucs > 127 && category(ucs) == Letter_Lowercase);
260 inline bool isUpper() const {
261 return (ucs <= 'Z' && ucs >= 'A')
262 || (ucs > 127 && category(ucs) == Letter_Uppercase);
264 inline bool isTitleCase() const { return category() == Letter_Titlecase; }
266 inline bool isHighSurrogate() const {
267 return ((ucs & 0xfc00) == 0xd800);
269 inline bool isLowSurrogate() const {
270 return ((ucs & 0xfc00) == 0xdc00);
273 inline uchar cell() const { return uchar(ucs & 0xff); }
274 inline uchar row() const { return uchar((ucs>>8)&0xff); }
275 inline void setCell(uchar cell);
276 inline void setRow(uchar row);
278 static inline bool isHighSurrogate(uint ucs4) {
279 return ((ucs4 & 0xfffffc00) == 0xd800);
281 static inline bool isLowSurrogate(uint ucs4) {
282 return ((ucs4 & 0xfffffc00) == 0xdc00);
284 static inline bool requiresSurrogates(uint ucs4) {
285 return (ucs4 >= 0x10000);
287 static inline uint surrogateToUcs4(ushort high, ushort low) {
288 return (uint(high)<<10) + low - 0x35fdc00;
290 static inline uint surrogateToUcs4(QChar high, QChar low) {
291 return (uint(high.ucs)<<10) + low.ucs - 0x35fdc00;
293 static inline ushort highSurrogate(uint ucs4) {
294 return ushort((ucs4>>10) + 0xd7c0);
296 static inline ushort lowSurrogate(uint ucs4) {
297 return ushort(ucs4%0x400 + 0xdc00);
300 static Category QT_FASTCALL category(uint ucs4);
301 static Category QT_FASTCALL category(ushort ucs2);
302 static Direction QT_FASTCALL direction(uint ucs4);
303 static Direction QT_FASTCALL direction(ushort ucs2);
304 static Joining QT_FASTCALL joining(uint ucs4);
305 static Joining QT_FASTCALL joining(ushort ucs2);
306 static unsigned char QT_FASTCALL combiningClass(uint ucs4);
307 static unsigned char QT_FASTCALL combiningClass(ushort ucs2);
309 static uint QT_FASTCALL mirroredChar(uint ucs4);
310 static ushort QT_FASTCALL mirroredChar(ushort ucs2);
311 static Decomposition QT_FASTCALL decompositionTag(uint ucs4);
313 static int QT_FASTCALL digitValue(uint ucs4);
314 static int QT_FASTCALL digitValue(ushort ucs2);
315 static uint QT_FASTCALL toLower(uint ucs4);
316 static ushort QT_FASTCALL toLower(ushort ucs2);
317 static uint QT_FASTCALL toUpper(uint ucs4);
318 static ushort QT_FASTCALL toUpper(ushort ucs2);
319 static uint QT_FASTCALL toTitleCase(uint ucs4);
320 static ushort QT_FASTCALL toTitleCase(ushort ucs2);
321 static uint QT_FASTCALL toCaseFolded(uint ucs4);
322 static ushort QT_FASTCALL toCaseFolded(ushort ucs2);
324 static UnicodeVersion QT_FASTCALL unicodeVersion(uint ucs4);
325 static UnicodeVersion QT_FASTCALL unicodeVersion(ushort ucs2);
327 static UnicodeVersion QT_FASTCALL currentUnicodeVersion();
329 static QString QT_FASTCALL decomposition(uint ucs4);
332 static bool QT_FASTCALL isDigit(ushort ucs2);
333 static bool QT_FASTCALL isLetter(ushort ucs2);
334 static bool QT_FASTCALL isLetterOrNumber(ushort ucs2);
335 static bool QT_FASTCALL isSpace(ushort ucs2);
337 #ifdef QT_NO_CAST_FROM_ASCII
344 Q_DECLARE_TYPEINFO(QChar, Q_MOVABLE_TYPE);
346 inline char QChar::toAscii() const { return ucs > 0xff ? 0 : char(ucs); }
347 inline char QChar::toLatin1() const { return ucs > 0xff ? '\0' : char(ucs); }
348 inline QChar QChar::fromLatin1(char c) { return QChar(ushort(uchar(c))); }
349 inline QChar QChar::fromAscii(char c) { return QChar(ushort(uchar(c))); }
351 inline void QChar::setCell(uchar acell)
352 { ucs = ushort((ucs & 0xff00) + acell); }
353 inline void QChar::setRow(uchar arow)
354 { ucs = ushort((ushort(arow)<<8) + (ucs&0xff)); }
356 inline bool operator==(QChar c1, QChar c2) { return c1.unicode() == c2.unicode(); }
357 inline bool operator!=(QChar c1, QChar c2) { return c1.unicode() != c2.unicode(); }
358 inline bool operator<=(QChar c1, QChar c2) { return c1.unicode() <= c2.unicode(); }
359 inline bool operator>=(QChar c1, QChar c2) { return c1.unicode() >= c2.unicode(); }
360 inline bool operator<(QChar c1, QChar c2) { return c1.unicode() < c2.unicode(); }
361 inline bool operator>(QChar c1, QChar c2) { return c1.unicode() > c2.unicode(); }
363 #ifndef QT_NO_DATASTREAM
364 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QChar);
365 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QChar &);