Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qchar.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
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_HEADER
48
49 QT_BEGIN_NAMESPACE
50
51
52 class QString;
53
54 struct QLatin1Char
55 {
56 public:
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)); }
60
61 private:
62     char ch;
63 };
64
65
66 class Q_CORE_EXPORT QChar {
67 public:
68     enum SpecialCharacter {
69         Null = 0x0000,
70         Nbsp = 0x00a0,
71         ReplacementCharacter = 0xfffd,
72         ObjectReplacementCharacter = 0xfffc,
73         ByteOrderMark = 0xfeff,
74         ByteOrderSwapped = 0xfffe,
75         ParagraphSeparator = 0x2029,
76         LineSeparator = 0x2028
77     };
78
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
87
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) { }
91 #endif
92     // Unicode information
93
94     enum Category
95     {
96         Mark_NonSpacing,          //   Mn
97         Mark_SpacingCombining,    //   Mc
98         Mark_Enclosing,           //   Me
99
100         Number_DecimalDigit,      //   Nd
101         Number_Letter,            //   Nl
102         Number_Other,             //   No
103
104         Separator_Space,          //   Zs
105         Separator_Line,           //   Zl
106         Separator_Paragraph,      //   Zp
107
108         Other_Control,            //   Cc
109         Other_Format,             //   Cf
110         Other_Surrogate,          //   Cs
111         Other_PrivateUse,         //   Co
112         Other_NotAssigned,        //   Cn
113
114         Letter_Uppercase,         //   Lu
115         Letter_Lowercase,         //   Ll
116         Letter_Titlecase,         //   Lt
117         Letter_Modifier,          //   Lm
118         Letter_Other,             //   Lo
119
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
127
128         Symbol_Math,              //   Sm
129         Symbol_Currency,          //   Sc
130         Symbol_Modifier,          //   Sk
131         Symbol_Other              //   So
132     };
133
134     enum Direction
135     {
136         DirL, DirR, DirEN, DirES, DirET, DirAN, DirCS, DirB, DirS, DirWS, DirON,
137         DirLRE, DirLRO, DirAL, DirRLE, DirRLO, DirPDF, DirNSM, DirBN
138     };
139
140     enum Decomposition
141     {
142         NoDecomposition,
143         Canonical,
144         Font,
145         NoBreak,
146         Initial,
147         Medial,
148         Final,
149         Isolated,
150         Circle,
151         Super,
152         Sub,
153         Vertical,
154         Wide,
155         Narrow,
156         Small,
157         Square,
158         Compat,
159         Fraction
160     };
161
162     enum Joining
163     {
164         OtherJoining, Dual, Right, Center
165     };
166
167     enum CombiningClass
168     {
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,
177
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,
186
187         Combining_DoubleBelow             = 233,
188         Combining_DoubleAbove             = 234,
189         Combining_IotaSubscript           = 240
190     };
191
192     enum UnicodeVersion {
193         Unicode_Unassigned,    // ### Qt 5: assign with some constantly big value
194         Unicode_1_1,
195         Unicode_2_0,
196         Unicode_2_1_2,
197         Unicode_3_0,
198         Unicode_3_1,
199         Unicode_3_2,
200         Unicode_4_0,
201         Unicode_4_1,
202         Unicode_5_0
203     };
204     // ****** WHEN ADDING FUNCTIONS, CONSIDER ADDING TO QCharRef TOO
205
206     Category category() const;
207     Direction direction() const;
208     Joining joining() const;
209     bool hasMirrored() const;
210     unsigned char combiningClass() const;
211
212     QChar mirroredChar() const;
213     QString decomposition() const;
214     Decomposition decompositionTag() const;
215
216     int digitValue() const;
217     QChar toLower() const;
218     QChar toUpper() const;
219     QChar toTitleCase() const;
220     QChar toCaseFolded() const;
221
222     UnicodeVersion unicodeVersion() const;
223
224     inline char toAscii() const;
225     inline char toLatin1() const;
226     inline ushort unicode() const { return ucs; }
227     inline ushort &unicode() { return ucs; }
228
229     static inline QChar fromAscii(char c);
230     static inline QChar fromLatin1(char c);
231
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)));
238     }
239     bool isMark() const;
240     inline bool isLetter() const {
241         return (ucs >= 'a' && ucs <= 'z')
242                 || (ucs <= 'Z' && ucs >= 'A')
243                 || (ucs > 127 && isLetter(ucs));
244     }
245     bool isNumber() const;
246     inline bool isLetterOrNumber() const
247     {
248         return (ucs >= 'a' && ucs <= 'z')
249                 || (ucs <= 'Z' && ucs >= 'A')
250                 || (ucs <= '9' && ucs >= '0')
251                 || (ucs > 127 && isLetterOrNumber(ucs));
252     }
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);
259     }
260     inline bool isUpper() const {
261         return (ucs <= 'Z' && ucs >= 'A')
262                 || (ucs > 127 && category(ucs) == Letter_Uppercase);
263     }
264     inline bool isTitleCase() const { return category() == Letter_Titlecase; }
265
266     inline bool isHighSurrogate() const {
267         return ((ucs & 0xfc00) == 0xd800);
268     }
269     inline bool isLowSurrogate() const {
270         return ((ucs & 0xfc00) == 0xdc00);
271     }
272
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);
277
278     static inline bool isHighSurrogate(uint ucs4) {
279         return ((ucs4 & 0xfffffc00) == 0xd800);
280     }
281     static inline bool isLowSurrogate(uint ucs4) {
282         return ((ucs4 & 0xfffffc00) == 0xdc00);
283     }
284     static inline bool requiresSurrogates(uint ucs4) {
285         return (ucs4 >= 0x10000);
286     }
287     static inline uint surrogateToUcs4(ushort high, ushort low) {
288         return (uint(high)<<10) + low - 0x35fdc00;
289     }
290     static inline uint surrogateToUcs4(QChar high, QChar low) {
291         return (uint(high.ucs)<<10) + low.ucs - 0x35fdc00;
292     }
293     static inline ushort highSurrogate(uint ucs4) {
294         return ushort((ucs4>>10) + 0xd7c0);
295     }
296     static inline ushort lowSurrogate(uint ucs4) {
297         return ushort(ucs4%0x400 + 0xdc00);
298     }
299
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);
308
309     static uint QT_FASTCALL mirroredChar(uint ucs4);
310     static ushort QT_FASTCALL mirroredChar(ushort ucs2);
311     static Decomposition QT_FASTCALL decompositionTag(uint ucs4);
312
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);
323
324     static UnicodeVersion QT_FASTCALL unicodeVersion(uint ucs4);
325     static UnicodeVersion QT_FASTCALL unicodeVersion(ushort ucs2);
326
327     static UnicodeVersion QT_FASTCALL currentUnicodeVersion();
328
329     static QString QT_FASTCALL decomposition(uint ucs4);
330
331 private:
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);
336
337 #ifdef QT_NO_CAST_FROM_ASCII
338     QChar(char c);
339     QChar(uchar c);
340 #endif
341     ushort ucs;
342 };
343
344 Q_DECLARE_TYPEINFO(QChar, Q_MOVABLE_TYPE);
345
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))); }
350
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)); }
355
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(); }
362
363 #ifndef QT_NO_DATASTREAM
364 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QChar);
365 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QChar &);
366 #endif
367
368 QT_END_NAMESPACE
369
370 QT_END_HEADER
371
372 #endif // QCHAR_H