]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/unicode/qt4/UnicodeQt4.h
2 * Copyright (C) 2006 George Staikos <staikos@kde.org>
3 * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com>
4 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #ifndef WTF_UNICODE_QT4_H
24 #define WTF_UNICODE_QT4_H
34 namespace QUnicodeTables
{
37 ushort line_break_class
: 8;
39 ushort combiningClass
:8;
41 signed short digitValue
: 6; /* 5 needed */
42 ushort unicodeVersion
: 4;
43 ushort lowerCaseSpecial
: 1;
44 ushort upperCaseSpecial
: 1;
45 ushort titleCaseSpecial
: 1;
46 ushort caseFoldSpecial
: 1; /* currently unused */
47 signed short mirrorDiff
: 16;
48 signed short lowerCaseDiff
: 16;
49 signed short upperCaseDiff
: 16;
50 signed short titleCaseDiff
: 16;
51 signed short caseFoldDiff
: 16;
53 Q_CORE_EXPORT
const Properties
* QT_FASTCALL
properties(uint ucs4
);
54 Q_CORE_EXPORT
const Properties
* QT_FASTCALL
properties(ushort ucs2
);
58 // ugly hack to make UChar compatible with JSChar in API/JSStringRef.h
59 #if defined(Q_OS_WIN) || COMPILER(WINSCW) || (COMPILER(RVCT) && OS(SYMBIAN))
60 typedef wchar_t UChar
;
62 typedef uint16_t UChar
;
64 typedef uint32_t UChar32
;
66 // some defines from ICU
68 #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
69 #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
70 #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
71 #define U16_GET_SUPPLEMENTARY(lead, trail) \
72 (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
74 #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
75 #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
77 #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
78 #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
79 #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
80 #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
82 #define U16_NEXT(s, i, length, c) { \
84 if(U16_IS_LEAD(c)) { \
86 if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
88 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
93 #define U16_PREV(s, start, i, c) { \
95 if(U16_IS_TRAIL(c)) { \
97 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
99 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
104 #define U_MASK(x) ((uint32_t)1<<(x))
110 LeftToRight
= QChar::DirL
,
111 RightToLeft
= QChar::DirR
,
112 EuropeanNumber
= QChar::DirEN
,
113 EuropeanNumberSeparator
= QChar::DirES
,
114 EuropeanNumberTerminator
= QChar::DirET
,
115 ArabicNumber
= QChar::DirAN
,
116 CommonNumberSeparator
= QChar::DirCS
,
117 BlockSeparator
= QChar::DirB
,
118 SegmentSeparator
= QChar::DirS
,
119 WhiteSpaceNeutral
= QChar::DirWS
,
120 OtherNeutral
= QChar::DirON
,
121 LeftToRightEmbedding
= QChar::DirLRE
,
122 LeftToRightOverride
= QChar::DirLRO
,
123 RightToLeftArabic
= QChar::DirAL
,
124 RightToLeftEmbedding
= QChar::DirRLE
,
125 RightToLeftOverride
= QChar::DirRLO
,
126 PopDirectionalFormat
= QChar::DirPDF
,
127 NonSpacingMark
= QChar::DirNSM
,
128 BoundaryNeutral
= QChar::DirBN
131 enum DecompositionType
{
132 DecompositionNone
= QChar::NoDecomposition
,
133 DecompositionCanonical
= QChar::Canonical
,
134 DecompositionCompat
= QChar::Compat
,
135 DecompositionCircle
= QChar::Circle
,
136 DecompositionFinal
= QChar::Final
,
137 DecompositionFont
= QChar::Font
,
138 DecompositionFraction
= QChar::Fraction
,
139 DecompositionInitial
= QChar::Initial
,
140 DecompositionIsolated
= QChar::Isolated
,
141 DecompositionMedial
= QChar::Medial
,
142 DecompositionNarrow
= QChar::Narrow
,
143 DecompositionNoBreak
= QChar::NoBreak
,
144 DecompositionSmall
= QChar::Small
,
145 DecompositionSquare
= QChar::Square
,
146 DecompositionSub
= QChar::Sub
,
147 DecompositionSuper
= QChar::Super
,
148 DecompositionVertical
= QChar::Vertical
,
149 DecompositionWide
= QChar::Wide
154 Mark_NonSpacing
= U_MASK(QChar::Mark_NonSpacing
),
155 Mark_SpacingCombining
= U_MASK(QChar::Mark_SpacingCombining
),
156 Mark_Enclosing
= U_MASK(QChar::Mark_Enclosing
),
157 Number_DecimalDigit
= U_MASK(QChar::Number_DecimalDigit
),
158 Number_Letter
= U_MASK(QChar::Number_Letter
),
159 Number_Other
= U_MASK(QChar::Number_Other
),
160 Separator_Space
= U_MASK(QChar::Separator_Space
),
161 Separator_Line
= U_MASK(QChar::Separator_Line
),
162 Separator_Paragraph
= U_MASK(QChar::Separator_Paragraph
),
163 Other_Control
= U_MASK(QChar::Other_Control
),
164 Other_Format
= U_MASK(QChar::Other_Format
),
165 Other_Surrogate
= U_MASK(QChar::Other_Surrogate
),
166 Other_PrivateUse
= U_MASK(QChar::Other_PrivateUse
),
167 Other_NotAssigned
= U_MASK(QChar::Other_NotAssigned
),
168 Letter_Uppercase
= U_MASK(QChar::Letter_Uppercase
),
169 Letter_Lowercase
= U_MASK(QChar::Letter_Lowercase
),
170 Letter_Titlecase
= U_MASK(QChar::Letter_Titlecase
),
171 Letter_Modifier
= U_MASK(QChar::Letter_Modifier
),
172 Letter_Other
= U_MASK(QChar::Letter_Other
),
173 Punctuation_Connector
= U_MASK(QChar::Punctuation_Connector
),
174 Punctuation_Dash
= U_MASK(QChar::Punctuation_Dash
),
175 Punctuation_Open
= U_MASK(QChar::Punctuation_Open
),
176 Punctuation_Close
= U_MASK(QChar::Punctuation_Close
),
177 Punctuation_InitialQuote
= U_MASK(QChar::Punctuation_InitialQuote
),
178 Punctuation_FinalQuote
= U_MASK(QChar::Punctuation_FinalQuote
),
179 Punctuation_Other
= U_MASK(QChar::Punctuation_Other
),
180 Symbol_Math
= U_MASK(QChar::Symbol_Math
),
181 Symbol_Currency
= U_MASK(QChar::Symbol_Currency
),
182 Symbol_Modifier
= U_MASK(QChar::Symbol_Modifier
),
183 Symbol_Other
= U_MASK(QChar::Symbol_Other
)
187 // FIXME: handle surrogates correctly in all methods
189 inline UChar32
toLower(UChar32 ch
)
191 return QChar::toLower(ch
);
194 inline int toLower(UChar
* result
, int resultLength
, const UChar
* src
, int srcLength
, bool* error
)
196 const UChar
*e
= src
+ srcLength
;
197 const UChar
*s
= src
;
201 // this avoids one out of bounds check in the loop
202 if (s
< e
&& QChar(*s
).isLowSurrogate()) {
209 while (s
< e
&& (rindex
< uint(resultLength
) || !r
)) {
211 if (QChar(c
).isLowSurrogate() && QChar(*(s
- 1)).isHighSurrogate())
212 c
= QChar::surrogateToUcs4(*(s
- 1), c
);
213 const QUnicodeTables::Properties
*prop
= QUnicodeTables::properties(c
);
214 if (prop
->lowerCaseSpecial
) {
219 qstring
+= QChar(*(s
-1));
220 qstring
+= QChar(*s
);
222 qstring
= qstring
.toLower();
223 for (int i
= 0; i
< qstring
.length(); ++i
) {
224 if (rindex
>= uint(resultLength
)) {
225 needed
+= qstring
.length() - i
;
229 r
[rindex
] = qstring
.at(i
).unicode();
234 r
[rindex
] = *s
+ prop
->lowerCaseDiff
;
241 *error
= (needed
!= 0);
242 if (rindex
< uint(resultLength
))
244 return rindex
+ needed
;
247 inline UChar32
toUpper(UChar32 ch
)
249 return QChar::toUpper(ch
);
252 inline int toUpper(UChar
* result
, int resultLength
, const UChar
* src
, int srcLength
, bool* error
)
254 const UChar
*e
= src
+ srcLength
;
255 const UChar
*s
= src
;
259 // this avoids one out of bounds check in the loop
260 if (s
< e
&& QChar(*s
).isLowSurrogate()) {
267 while (s
< e
&& (rindex
< resultLength
|| !r
)) {
269 if (QChar(c
).isLowSurrogate() && QChar(*(s
- 1)).isHighSurrogate())
270 c
= QChar::surrogateToUcs4(*(s
- 1), c
);
271 const QUnicodeTables::Properties
*prop
= QUnicodeTables::properties(c
);
272 if (prop
->upperCaseSpecial
) {
277 qstring
+= QChar(*(s
-1));
278 qstring
+= QChar(*s
);
280 qstring
= qstring
.toUpper();
281 for (int i
= 0; i
< qstring
.length(); ++i
) {
282 if (rindex
>= resultLength
) {
283 needed
+= qstring
.length() - i
;
287 r
[rindex
] = qstring
.at(i
).unicode();
292 r
[rindex
] = *s
+ prop
->upperCaseDiff
;
299 *error
= (needed
!= 0);
300 if (rindex
< resultLength
)
302 return rindex
+ needed
;
305 inline int toTitleCase(UChar32 c
)
307 return QChar::toTitleCase(c
);
310 inline UChar32
foldCase(UChar32 c
)
312 return QChar::toCaseFolded(c
);
315 inline int foldCase(UChar
* result
, int resultLength
, const UChar
* src
, int srcLength
, bool* error
)
317 // FIXME: handle special casing. Easiest with some low level API in Qt
319 if (resultLength
< srcLength
) {
323 for (int i
= 0; i
< srcLength
; ++i
)
324 result
[i
] = QChar::toCaseFolded(ushort(src
[i
]));
328 inline bool isArabicChar(UChar32 c
)
330 return c
>= 0x0600 && c
<= 0x06FF;
333 inline bool isPrintableChar(UChar32 c
)
335 const uint test
= U_MASK(QChar::Other_Control
) |
336 U_MASK(QChar::Other_NotAssigned
);
337 return !(U_MASK(QChar::category(c
)) & test
);
340 inline bool isSeparatorSpace(UChar32 c
)
342 return QChar::category(c
) == QChar::Separator_Space
;
345 inline bool isPunct(UChar32 c
)
347 const uint test
= U_MASK(QChar::Punctuation_Connector
) |
348 U_MASK(QChar::Punctuation_Dash
) |
349 U_MASK(QChar::Punctuation_Open
) |
350 U_MASK(QChar::Punctuation_Close
) |
351 U_MASK(QChar::Punctuation_InitialQuote
) |
352 U_MASK(QChar::Punctuation_FinalQuote
) |
353 U_MASK(QChar::Punctuation_Other
);
354 return U_MASK(QChar::category(c
)) & test
;
357 inline bool isLower(UChar32 c
)
359 return QChar::category(c
) == QChar::Letter_Lowercase
;
362 inline bool hasLineBreakingPropertyComplexContext(UChar32
)
364 // FIXME: Implement this to return whether the character has line breaking property SA (Complex Context).
368 inline UChar32
mirroredChar(UChar32 c
)
370 return QChar::mirroredChar(c
);
373 inline uint8_t combiningClass(UChar32 c
)
375 return QChar::combiningClass(c
);
378 inline DecompositionType
decompositionType(UChar32 c
)
380 return (DecompositionType
)QChar::decompositionTag(c
);
383 inline int umemcasecmp(const UChar
* a
, const UChar
* b
, int len
)
385 // handle surrogates correctly
386 for (int i
= 0; i
< len
; ++i
) {
387 uint c1
= QChar::toCaseFolded(ushort(a
[i
]));
388 uint c2
= QChar::toCaseFolded(ushort(b
[i
]));
395 inline Direction
direction(UChar32 c
)
397 return (Direction
)QChar::direction(c
);
400 inline CharCategory
category(UChar32 c
)
402 return (CharCategory
) U_MASK(QChar::category(c
));
407 #endif // WTF_UNICODE_QT4_H