Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 : ** Contact: http://www.qt-project.org/legal
5 : **
6 : ** This file is part of the QtCore module of the Qt Toolkit.
7 : **
8 : ** $QT_BEGIN_LICENSE:LGPL$
9 : ** Commercial License Usage
10 : ** Licensees holding valid commercial Qt licenses may use this file in
11 : ** accordance with the commercial license agreement provided with the
12 : ** Software or, alternatively, in accordance with the terms contained in
13 : ** a written agreement between you and Digia. For licensing terms and
14 : ** conditions see http://qt.digia.com/licensing. For further information
15 : ** use the contact form at http://qt.digia.com/contact-us.
16 : **
17 : ** GNU Lesser General Public License Usage
18 : ** Alternatively, this file may be used under the terms of the GNU Lesser
19 : ** General Public License version 2.1 as published by the Free Software
20 : ** Foundation and appearing in the file LICENSE.LGPL included in the
21 : ** packaging of this file. Please review the following information to
22 : ** ensure the GNU Lesser General Public License version 2.1 requirements
23 : ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 : **
25 : ** In addition, as a special exception, Digia gives you certain additional
26 : ** rights. These rights are described in the Digia Qt LGPL Exception
27 : ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 : **
29 : ** GNU General Public License Usage
30 : ** Alternatively, this file may be used under the terms of the GNU
31 : ** General Public License version 3.0 as published by the Free Software
32 : ** Foundation and appearing in the file LICENSE.GPL included in the
33 : ** packaging of this file. Please review the following information to
34 : ** ensure the GNU General Public License version 3.0 requirements will be
35 : ** met: http://www.gnu.org/copyleft/gpl.html.
36 : **
37 : **
38 : ** $QT_END_LICENSE$
39 : **
40 : ****************************************************************************/
41 :
42 : #ifndef QCHAR_H
43 : #define QCHAR_H
44 :
45 : #include <QtCore/qglobal.h>
46 :
47 : QT_BEGIN_NAMESPACE
48 :
49 :
50 : class QString;
51 :
52 : struct QLatin1Char
53 : {
54 : public:
55 0 : Q_DECL_CONSTEXPR inline explicit QLatin1Char(char c) : ch(c) {}
56 : Q_DECL_CONSTEXPR inline char toLatin1() const { return ch; }
57 0 : Q_DECL_CONSTEXPR inline ushort unicode() const { return ushort(uchar(ch)); }
58 :
59 : private:
60 : char ch;
61 : };
62 :
63 :
64 : class Q_CORE_EXPORT QChar {
65 : public:
66 : enum SpecialCharacter {
67 : Null = 0x0000,
68 : Tabulation = 0x0009,
69 : LineFeed = 0x000a,
70 : CarriageReturn = 0x000d,
71 : Space = 0x0020,
72 : Nbsp = 0x00a0,
73 : SoftHyphen = 0x00ad,
74 : ReplacementCharacter = 0xfffd,
75 : ObjectReplacementCharacter = 0xfffc,
76 : ByteOrderMark = 0xfeff,
77 : ByteOrderSwapped = 0xfffe,
78 : ParagraphSeparator = 0x2029,
79 : LineSeparator = 0x2028,
80 : LastValidCodePoint = 0x10ffff
81 : };
82 :
83 : Q_DECL_CONSTEXPR QChar() : ucs(0) {}
84 0 : Q_DECL_CONSTEXPR QChar(ushort rc) : ucs(rc){} // implicit
85 : Q_DECL_CONSTEXPR QChar(uchar c, uchar r) : ucs(ushort((r << 8) | c)){}
86 : Q_DECL_CONSTEXPR QChar(short rc) : ucs(ushort(rc)){} // implicit
87 : Q_DECL_CONSTEXPR QChar(uint rc) : ucs(ushort(rc & 0xffff)){}
88 : Q_DECL_CONSTEXPR QChar(int rc) : ucs(ushort(rc & 0xffff)){}
89 : Q_DECL_CONSTEXPR QChar(SpecialCharacter s) : ucs(ushort(s)) {} // implicit
90 0 : Q_DECL_CONSTEXPR QChar(QLatin1Char ch) : ucs(ch.unicode()) {} // implicit
91 :
92 : #ifndef QT_NO_CAST_FROM_ASCII
93 : QT_ASCII_CAST_WARN Q_DECL_CONSTEXPR explicit QChar(char c) : ucs(uchar(c)) { }
94 : QT_ASCII_CAST_WARN Q_DECL_CONSTEXPR explicit QChar(uchar c) : ucs(c) { }
95 : #endif
96 : // Unicode information
97 :
98 : enum Category
99 : {
100 : Mark_NonSpacing, // Mn
101 : Mark_SpacingCombining, // Mc
102 : Mark_Enclosing, // Me
103 :
104 : Number_DecimalDigit, // Nd
105 : Number_Letter, // Nl
106 : Number_Other, // No
107 :
108 : Separator_Space, // Zs
109 : Separator_Line, // Zl
110 : Separator_Paragraph, // Zp
111 :
112 : Other_Control, // Cc
113 : Other_Format, // Cf
114 : Other_Surrogate, // Cs
115 : Other_PrivateUse, // Co
116 : Other_NotAssigned, // Cn
117 :
118 : Letter_Uppercase, // Lu
119 : Letter_Lowercase, // Ll
120 : Letter_Titlecase, // Lt
121 : Letter_Modifier, // Lm
122 : Letter_Other, // Lo
123 :
124 : Punctuation_Connector, // Pc
125 : Punctuation_Dash, // Pd
126 : Punctuation_Open, // Ps
127 : Punctuation_Close, // Pe
128 : Punctuation_InitialQuote, // Pi
129 : Punctuation_FinalQuote, // Pf
130 : Punctuation_Other, // Po
131 :
132 : Symbol_Math, // Sm
133 : Symbol_Currency, // Sc
134 : Symbol_Modifier, // Sk
135 : Symbol_Other // So
136 : };
137 :
138 : enum Script
139 : {
140 : Script_Unknown,
141 : Script_Inherited,
142 : Script_Common,
143 :
144 : Script_Latin,
145 : Script_Greek,
146 : Script_Cyrillic,
147 : Script_Armenian,
148 : Script_Hebrew,
149 : Script_Arabic,
150 : Script_Syriac,
151 : Script_Thaana,
152 : Script_Devanagari,
153 : Script_Bengali,
154 : Script_Gurmukhi,
155 : Script_Gujarati,
156 : Script_Oriya,
157 : Script_Tamil,
158 : Script_Telugu,
159 : Script_Kannada,
160 : Script_Malayalam,
161 : Script_Sinhala,
162 : Script_Thai,
163 : Script_Lao,
164 : Script_Tibetan,
165 : Script_Myanmar,
166 : Script_Georgian,
167 : Script_Hangul,
168 : Script_Ethiopic,
169 : Script_Cherokee,
170 : Script_CanadianAboriginal,
171 : Script_Ogham,
172 : Script_Runic,
173 : Script_Khmer,
174 : Script_Mongolian,
175 : Script_Hiragana,
176 : Script_Katakana,
177 : Script_Bopomofo,
178 : Script_Han,
179 : Script_Yi,
180 : Script_OldItalic,
181 : Script_Gothic,
182 : Script_Deseret,
183 : Script_Tagalog,
184 : Script_Hanunoo,
185 : Script_Buhid,
186 : Script_Tagbanwa,
187 : Script_Coptic,
188 :
189 : // Unicode 4.0 additions
190 : Script_Limbu,
191 : Script_TaiLe,
192 : Script_LinearB,
193 : Script_Ugaritic,
194 : Script_Shavian,
195 : Script_Osmanya,
196 : Script_Cypriot,
197 : Script_Braille,
198 :
199 : // Unicode 4.1 additions
200 : Script_Buginese,
201 : Script_NewTaiLue,
202 : Script_Glagolitic,
203 : Script_Tifinagh,
204 : Script_SylotiNagri,
205 : Script_OldPersian,
206 : Script_Kharoshthi,
207 :
208 : // Unicode 5.0 additions
209 : Script_Balinese,
210 : Script_Cuneiform,
211 : Script_Phoenician,
212 : Script_PhagsPa,
213 : Script_Nko,
214 :
215 : // Unicode 5.1 additions
216 : Script_Sundanese,
217 : Script_Lepcha,
218 : Script_OlChiki,
219 : Script_Vai,
220 : Script_Saurashtra,
221 : Script_KayahLi,
222 : Script_Rejang,
223 : Script_Lycian,
224 : Script_Carian,
225 : Script_Lydian,
226 : Script_Cham,
227 :
228 : // Unicode 5.2 additions
229 : Script_TaiTham,
230 : Script_TaiViet,
231 : Script_Avestan,
232 : Script_EgyptianHieroglyphs,
233 : Script_Samaritan,
234 : Script_Lisu,
235 : Script_Bamum,
236 : Script_Javanese,
237 : Script_MeeteiMayek,
238 : Script_ImperialAramaic,
239 : Script_OldSouthArabian,
240 : Script_InscriptionalParthian,
241 : Script_InscriptionalPahlavi,
242 : Script_OldTurkic,
243 : Script_Kaithi,
244 :
245 : // Unicode 6.0 additions
246 : Script_Batak,
247 : Script_Brahmi,
248 : Script_Mandaic,
249 :
250 : // Unicode 6.1 additions
251 : Script_Chakma,
252 : Script_MeroiticCursive,
253 : Script_MeroiticHieroglyphs,
254 : Script_Miao,
255 : Script_Sharada,
256 : Script_SoraSompeng,
257 : Script_Takri,
258 :
259 : ScriptCount
260 : };
261 :
262 : enum Direction
263 : {
264 : DirL, DirR, DirEN, DirES, DirET, DirAN, DirCS, DirB, DirS, DirWS, DirON,
265 : DirLRE, DirLRO, DirAL, DirRLE, DirRLO, DirPDF, DirNSM, DirBN,
266 : DirLRI, DirRLI, DirFSI, DirPDI
267 : };
268 :
269 : enum Decomposition
270 : {
271 : NoDecomposition,
272 : Canonical,
273 : Font,
274 : NoBreak,
275 : Initial,
276 : Medial,
277 : Final,
278 : Isolated,
279 : Circle,
280 : Super,
281 : Sub,
282 : Vertical,
283 : Wide,
284 : Narrow,
285 : Small,
286 : Square,
287 : Compat,
288 : Fraction
289 : };
290 :
291 : enum JoiningType {
292 : Joining_None,
293 : Joining_Causing,
294 : Joining_Dual,
295 : Joining_Right,
296 : Joining_Left,
297 : Joining_Transparent
298 : };
299 :
300 : #if QT_DEPRECATED_SINCE(5, 3)
301 : enum Joining
302 : {
303 : OtherJoining, Dual, Right, Center
304 : };
305 : #endif
306 :
307 : enum CombiningClass
308 : {
309 : Combining_BelowLeftAttached = 200,
310 : Combining_BelowAttached = 202,
311 : Combining_BelowRightAttached = 204,
312 : Combining_LeftAttached = 208,
313 : Combining_RightAttached = 210,
314 : Combining_AboveLeftAttached = 212,
315 : Combining_AboveAttached = 214,
316 : Combining_AboveRightAttached = 216,
317 :
318 : Combining_BelowLeft = 218,
319 : Combining_Below = 220,
320 : Combining_BelowRight = 222,
321 : Combining_Left = 224,
322 : Combining_Right = 226,
323 : Combining_AboveLeft = 228,
324 : Combining_Above = 230,
325 : Combining_AboveRight = 232,
326 :
327 : Combining_DoubleBelow = 233,
328 : Combining_DoubleAbove = 234,
329 : Combining_IotaSubscript = 240
330 : };
331 :
332 : enum UnicodeVersion {
333 : Unicode_Unassigned,
334 : Unicode_1_1,
335 : Unicode_2_0,
336 : Unicode_2_1_2,
337 : Unicode_3_0,
338 : Unicode_3_1,
339 : Unicode_3_2,
340 : Unicode_4_0,
341 : Unicode_4_1,
342 : Unicode_5_0,
343 : Unicode_5_1,
344 : Unicode_5_2,
345 : Unicode_6_0,
346 : Unicode_6_1,
347 : Unicode_6_2,
348 : Unicode_6_3
349 : };
350 : // ****** WHEN ADDING FUNCTIONS, CONSIDER ADDING TO QCharRef TOO
351 :
352 : inline Category category() const { return QChar::category(ucs); }
353 : inline Direction direction() const { return QChar::direction(ucs); }
354 : inline JoiningType joiningType() const { return QChar::joiningType(ucs); }
355 : #if QT_DEPRECATED_SINCE(5, 3)
356 : QT_DEPRECATED inline Joining joining() const
357 : {
358 : switch (QChar::joiningType(ucs)) {
359 : case QChar::Joining_Causing: return QChar::Center;
360 : case QChar::Joining_Dual: return QChar::Dual;
361 : case QChar::Joining_Right: return QChar::Right;
362 : case QChar::Joining_None:
363 : case QChar::Joining_Left:
364 : case QChar::Joining_Transparent:
365 : default: return QChar::OtherJoining;
366 : }
367 : }
368 : #endif
369 : inline unsigned char combiningClass() const { return QChar::combiningClass(ucs); }
370 :
371 : inline QChar mirroredChar() const { return QChar::mirroredChar(ucs); }
372 : inline bool hasMirrored() const { return QChar::hasMirrored(ucs); }
373 :
374 : QString decomposition() const;
375 : inline Decomposition decompositionTag() const { return QChar::decompositionTag(ucs); }
376 :
377 : inline int digitValue() const { return QChar::digitValue(ucs); }
378 : inline QChar toLower() const { return QChar::toLower(ucs); }
379 : inline QChar toUpper() const { return QChar::toUpper(ucs); }
380 : inline QChar toTitleCase() const { return QChar::toTitleCase(ucs); }
381 : inline QChar toCaseFolded() const { return QChar::toCaseFolded(ucs); }
382 :
383 : inline Script script() const { return QChar::script(ucs); }
384 :
385 : inline UnicodeVersion unicodeVersion() const { return QChar::unicodeVersion(ucs); }
386 :
387 : #if QT_DEPRECATED_SINCE(5, 0)
388 : QT_DEPRECATED inline char toAscii() const { return toLatin1(); }
389 : #endif
390 : inline char toLatin1() const;
391 0 : inline ushort unicode() const { return ucs; }
392 5 : inline ushort &unicode() { return ucs; }
393 :
394 : #if QT_DEPRECATED_SINCE(5, 0)
395 : QT_DEPRECATED static inline QChar fromAscii(char c)
396 : { return fromLatin1(c); }
397 : #endif
398 : static inline QChar fromLatin1(char c);
399 :
400 : inline bool isNull() const { return ucs == 0; }
401 :
402 : inline bool isPrint() const { return QChar::isPrint(ucs); }
403 : inline bool isSpace() const { return QChar::isSpace(ucs); }
404 : inline bool isMark() const { return QChar::isMark(ucs); }
405 : inline bool isPunct() const { return QChar::isPunct(ucs); }
406 : inline bool isSymbol() const { return QChar::isSymbol(ucs); }
407 : inline bool isLetter() const { return QChar::isLetter(ucs); }
408 : inline bool isNumber() const { return QChar::isNumber(ucs); }
409 : inline bool isLetterOrNumber() const { return QChar::isLetterOrNumber(ucs); }
410 : inline bool isDigit() const { return QChar::isDigit(ucs); }
411 : inline bool isLower() const { return QChar::isLower(ucs); }
412 : inline bool isUpper() const { return QChar::isUpper(ucs); }
413 : inline bool isTitleCase() const { return QChar::isTitleCase(ucs); }
414 :
415 : inline bool isNonCharacter() const { return QChar::isNonCharacter(ucs); }
416 : inline bool isHighSurrogate() const { return QChar::isHighSurrogate(ucs); }
417 : inline bool isLowSurrogate() const { return QChar::isLowSurrogate(ucs); }
418 : inline bool isSurrogate() const { return QChar::isSurrogate(ucs); }
419 :
420 : inline uchar cell() const { return uchar(ucs & 0xff); }
421 : inline uchar row() const { return uchar((ucs>>8)&0xff); }
422 : inline void setCell(uchar cell);
423 : inline void setRow(uchar row);
424 :
425 : static inline bool isNonCharacter(uint ucs4) {
426 : return ucs4 >= 0xfdd0 && (ucs4 <= 0xfdef || (ucs4 & 0xfffe) == 0xfffe);
427 : }
428 : static inline bool isHighSurrogate(uint ucs4) {
429 : return ((ucs4 & 0xfffffc00) == 0xd800);
430 : }
431 : static inline bool isLowSurrogate(uint ucs4) {
432 : return ((ucs4 & 0xfffffc00) == 0xdc00);
433 : }
434 : static inline bool isSurrogate(uint ucs4) {
435 : return (ucs4 - 0xd800u < 2048u);
436 : }
437 : static inline bool requiresSurrogates(uint ucs4) {
438 : return (ucs4 >= 0x10000);
439 : }
440 : static inline uint surrogateToUcs4(ushort high, ushort low) {
441 : return (uint(high)<<10) + low - 0x35fdc00;
442 : }
443 : static inline uint surrogateToUcs4(QChar high, QChar low) {
444 : return surrogateToUcs4(high.unicode(), low.unicode());
445 : }
446 : static inline ushort highSurrogate(uint ucs4) {
447 : return ushort((ucs4>>10) + 0xd7c0);
448 : }
449 : static inline ushort lowSurrogate(uint ucs4) {
450 : return ushort(ucs4%0x400 + 0xdc00);
451 : }
452 :
453 : static Category QT_FASTCALL category(uint ucs4);
454 : static Direction QT_FASTCALL direction(uint ucs4);
455 : static JoiningType QT_FASTCALL joiningType(uint ucs4);
456 : #if QT_DEPRECATED_SINCE(5, 3)
457 : QT_DEPRECATED static Joining QT_FASTCALL joining(uint ucs4);
458 : #endif
459 : static unsigned char QT_FASTCALL combiningClass(uint ucs4);
460 :
461 : static uint QT_FASTCALL mirroredChar(uint ucs4);
462 : static bool QT_FASTCALL hasMirrored(uint ucs4);
463 :
464 : static QString QT_FASTCALL decomposition(uint ucs4);
465 : static Decomposition QT_FASTCALL decompositionTag(uint ucs4);
466 :
467 : static int QT_FASTCALL digitValue(uint ucs4);
468 : static uint QT_FASTCALL toLower(uint ucs4);
469 : static uint QT_FASTCALL toUpper(uint ucs4);
470 : static uint QT_FASTCALL toTitleCase(uint ucs4);
471 : static uint QT_FASTCALL toCaseFolded(uint ucs4);
472 :
473 : static Script QT_FASTCALL script(uint ucs4);
474 :
475 : static UnicodeVersion QT_FASTCALL unicodeVersion(uint ucs4);
476 :
477 : static UnicodeVersion QT_FASTCALL currentUnicodeVersion();
478 :
479 : static bool QT_FASTCALL isPrint(uint ucs4);
480 : static inline bool isSpace(uint ucs4);
481 : static bool QT_FASTCALL isMark(uint ucs4);
482 : static bool QT_FASTCALL isPunct(uint ucs4);
483 : static bool QT_FASTCALL isSymbol(uint ucs4);
484 : static inline bool isLetter(uint ucs4);
485 : static inline bool isNumber(uint ucs4);
486 : static inline bool isLetterOrNumber(uint ucs4);
487 : static inline bool isDigit(uint ucs4);
488 : static inline bool isLower(uint ucs4);
489 : static inline bool isUpper(uint ucs4);
490 : static inline bool isTitleCase(uint ucs4);
491 :
492 : private:
493 : static bool QT_FASTCALL isSpace_helper(uint ucs4);
494 : static bool QT_FASTCALL isLetter_helper(uint ucs4);
495 : static bool QT_FASTCALL isNumber_helper(uint ucs4);
496 : static bool QT_FASTCALL isLetterOrNumber_helper(uint ucs4);
497 :
498 : #ifdef QT_NO_CAST_FROM_ASCII
499 : QChar(char c);
500 : QChar(uchar c);
501 : #endif
502 : ushort ucs;
503 : };
504 :
505 : Q_DECLARE_TYPEINFO(QChar, Q_MOVABLE_TYPE);
506 :
507 : inline char QChar::toLatin1() const { return ucs > 0xff ? '\0' : char(ucs); }
508 : inline QChar QChar::fromLatin1(char c) { return QChar(ushort(uchar(c))); }
509 :
510 : inline void QChar::setCell(uchar acell)
511 : { ucs = ushort((ucs & 0xff00) + acell); }
512 : inline void QChar::setRow(uchar arow)
513 : { ucs = ushort((ushort(arow)<<8) + (ucs&0xff)); }
514 :
515 : inline bool QChar::isSpace(uint ucs4)
516 : {
517 : // note that [0x09..0x0d] + 0x85 are exceptional Cc-s and must be handled explicitly
518 : return ucs4 == 0x20 || (ucs4 <= 0x0d && ucs4 >= 0x09)
519 : || (ucs4 > 127 && (ucs4 == 0x85 || ucs4 == 0xa0 || QChar::isSpace_helper(ucs4)));
520 : }
521 : inline bool QChar::isLetter(uint ucs4)
522 : {
523 : return (ucs4 >= 'A' && ucs4 <= 'z' && (ucs4 >= 'a' || ucs4 <= 'Z'))
524 : || (ucs4 > 127 && QChar::isLetter_helper(ucs4));
525 : }
526 : inline bool QChar::isNumber(uint ucs4)
527 : { return (ucs4 <= '9' && ucs4 >= '0') || (ucs4 > 127 && QChar::isNumber_helper(ucs4)); }
528 : inline bool QChar::isLetterOrNumber(uint ucs4)
529 : {
530 : return (ucs4 >= 'A' && ucs4 <= 'z' && (ucs4 >= 'a' || ucs4 <= 'Z'))
531 : || (ucs4 >= '0' && ucs4 <= '9')
532 : || (ucs4 > 127 && QChar::isLetterOrNumber_helper(ucs4));
533 : }
534 : inline bool QChar::isDigit(uint ucs4)
535 : { return (ucs4 <= '9' && ucs4 >= '0') || (ucs4 > 127 && QChar::category(ucs4) == Number_DecimalDigit); }
536 : inline bool QChar::isLower(uint ucs4)
537 : { return (ucs4 <= 'z' && ucs4 >= 'a') || (ucs4 > 127 && QChar::category(ucs4) == Letter_Lowercase); }
538 : inline bool QChar::isUpper(uint ucs4)
539 : { return (ucs4 <= 'Z' && ucs4 >= 'A') || (ucs4 > 127 && QChar::category(ucs4) == Letter_Uppercase); }
540 : inline bool QChar::isTitleCase(uint ucs4)
541 : { return ucs4 > 127 && QChar::category(ucs4) == Letter_Titlecase; }
542 :
543 : inline bool operator==(QChar c1, QChar c2) { return c1.unicode() == c2.unicode(); }
544 : inline bool operator!=(QChar c1, QChar c2) { return c1.unicode() != c2.unicode(); }
545 : inline bool operator<=(QChar c1, QChar c2) { return c1.unicode() <= c2.unicode(); }
546 : inline bool operator>=(QChar c1, QChar c2) { return c1.unicode() >= c2.unicode(); }
547 : inline bool operator<(QChar c1, QChar c2) { return c1.unicode() < c2.unicode(); }
548 : inline bool operator>(QChar c1, QChar c2) { return c1.unicode() > c2.unicode(); }
549 :
550 : #ifndef QT_NO_DATASTREAM
551 : Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QChar);
552 : Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QChar &);
553 : #endif
554 :
555 : QT_END_NAMESPACE
556 :
557 : #endif // QCHAR_H
|