]>
git.saurik.com Git - apple/icu.git/blob - icuSources/extra/scrptrun/scrptrun.cpp
2 *******************************************************************************
4 * Copyright (C) 1999-2001, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: scrptrun.cpp
10 * created on: 10/17/2001
11 * created by: Eric R. Mader
14 #include "unicode/utypes.h"
15 #include "unicode/uscript.h"
19 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
21 const char ScriptRun::fgClassID
=0;
23 UChar32
ScriptRun::pairedChars
[] = {
24 0x0028, 0x0029, // ascii paired punctuation
28 0x00ab, 0x00bb, // guillemets
29 0x2018, 0x2019, // general punctuation
32 0x3008, 0x3009, // chinese paired punctuation
43 const int32_t ScriptRun::pairedCharCount
= ARRAY_SIZE(pairedChars
);
44 const int32_t ScriptRun::pairedCharPower
= 1 << highBit(pairedCharCount
);
45 const int32_t ScriptRun::pairedCharExtra
= pairedCharCount
- pairedCharPower
;
47 int8_t ScriptRun::highBit(int32_t value
)
55 if (value
>= 1 << 16) {
60 if (value
>= 1 << 8) {
65 if (value
>= 1 << 4) {
70 if (value
>= 1 << 2) {
75 if (value
>= 1 << 1) {
83 int32_t ScriptRun::getPairIndex(UChar32 ch
)
85 int32_t probe
= pairedCharPower
;
88 if (ch
>= pairedChars
[pairedCharExtra
]) {
89 index
= pairedCharExtra
;
92 while (probe
> (1 << 0)) {
95 if (ch
>= pairedChars
[index
+ probe
]) {
100 if (pairedChars
[index
] != ch
) {
107 UBool
ScriptRun::sameScript(int32_t scriptOne
, int32_t scriptTwo
)
109 return scriptOne
<= USCRIPT_INHERITED
|| scriptTwo
<= USCRIPT_INHERITED
|| scriptOne
== scriptTwo
;
112 UBool
ScriptRun::next()
114 int32_t startSP
= parenSP
; // used to find the first new open character
115 UErrorCode error
= U_ZERO_ERROR
;
117 // if we've fallen off the end of the text, we're done
118 if (scriptEnd
>= charLimit
) {
122 scriptCode
= USCRIPT_COMMON
;
124 for (scriptStart
= scriptEnd
; scriptEnd
< charLimit
; scriptEnd
+= 1) {
125 UChar high
= charArray
[scriptEnd
];
128 // if the character is a high surrogate and it's not the last one
129 // in the text, see if it's followed by a low surrogate
130 if (high
>= 0xD800 && high
<= 0xDBFF && scriptEnd
< charLimit
- 1)
132 UChar low
= charArray
[scriptEnd
+ 1];
134 // if it is followed by a low surrogate,
135 // consume it and form the full character
136 if (low
>= 0xDC00 && low
<= 0xDFFF) {
137 ch
= (high
- 0xD800) * 0x0400 + low
- 0xDC00 + 0x10000;
142 UScriptCode sc
= uscript_getScript(ch
, &error
);
143 int32_t pairIndex
= getPairIndex(ch
);
145 // Paired character handling:
147 // if it's an open character, push it onto the stack.
148 // if it's a close character, find the matching open on the
149 // stack, and use that script code. Any non-matching open
150 // characters above it on the stack will be poped.
151 if (pairIndex
>= 0) {
152 if ((pairIndex
& 1) == 0) {
153 parenStack
[++parenSP
].pairIndex
= pairIndex
;
154 parenStack
[parenSP
].scriptCode
= scriptCode
;
155 } else if (parenSP
>= 0) {
156 int32_t pi
= pairIndex
& ~1;
158 while (parenSP
>= 0 && parenStack
[parenSP
].pairIndex
!= pi
) {
162 if (parenSP
< startSP
) {
167 sc
= parenStack
[parenSP
].scriptCode
;
172 if (sameScript(scriptCode
, sc
)) {
173 if (scriptCode
<= USCRIPT_INHERITED
&& sc
> USCRIPT_INHERITED
) {
176 // now that we have a final script code, fix any open
177 // characters we pushed before we knew the script code.
178 while (startSP
< parenSP
) {
179 parenStack
[++startSP
].scriptCode
= scriptCode
;
183 // if this character is a close paired character,
184 // pop it from the stack
185 if (pairIndex
>= 0 && (pairIndex
& 1) != 0 && parenSP
>= 0) {
190 // if the run broke on a surrogate pair,
191 // end it before the high surrogate