]>
git.saurik.com Git - apple/icu.git/blob - icuSources/extra/scrptrun/scrptrun.cpp
2 *******************************************************************************
4 * Copyright (C) 1999-2016, 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"
20 const char ScriptRun::fgClassID
=0;
22 UChar32
ScriptRun::pairedChars
[] = {
23 0x0028, 0x0029, // ascii paired punctuation
27 0x00ab, 0x00bb, // guillemets
28 0x2018, 0x2019, // general punctuation
31 0x3008, 0x3009, // chinese paired punctuation
42 const int32_t ScriptRun::pairedCharCount
= UPRV_LENGTHOF(pairedChars
);
43 const int32_t ScriptRun::pairedCharPower
= 1 << highBit(pairedCharCount
);
44 const int32_t ScriptRun::pairedCharExtra
= pairedCharCount
- pairedCharPower
;
46 int8_t ScriptRun::highBit(int32_t value
)
54 if (value
>= 1 << 16) {
59 if (value
>= 1 << 8) {
64 if (value
>= 1 << 4) {
69 if (value
>= 1 << 2) {
74 if (value
>= 1 << 1) {
82 int32_t ScriptRun::getPairIndex(UChar32 ch
)
84 int32_t probe
= pairedCharPower
;
87 if (ch
>= pairedChars
[pairedCharExtra
]) {
88 index
= pairedCharExtra
;
91 while (probe
> (1 << 0)) {
94 if (ch
>= pairedChars
[index
+ probe
]) {
99 if (pairedChars
[index
] != ch
) {
106 UBool
ScriptRun::sameScript(int32_t scriptOne
, int32_t scriptTwo
)
108 return scriptOne
<= USCRIPT_INHERITED
|| scriptTwo
<= USCRIPT_INHERITED
|| scriptOne
== scriptTwo
;
111 UBool
ScriptRun::next()
113 int32_t startSP
= parenSP
; // used to find the first new open character
114 UErrorCode error
= U_ZERO_ERROR
;
116 // if we've fallen off the end of the text, we're done
117 if (scriptEnd
>= charLimit
) {
121 scriptCode
= USCRIPT_COMMON
;
123 for (scriptStart
= scriptEnd
; scriptEnd
< charLimit
; scriptEnd
+= 1) {
124 UChar high
= charArray
[scriptEnd
];
127 // if the character is a high surrogate and it's not the last one
128 // in the text, see if it's followed by a low surrogate
129 if (high
>= 0xD800 && high
<= 0xDBFF && scriptEnd
< charLimit
- 1)
131 UChar low
= charArray
[scriptEnd
+ 1];
133 // if it is followed by a low surrogate,
134 // consume it and form the full character
135 if (low
>= 0xDC00 && low
<= 0xDFFF) {
136 ch
= (high
- 0xD800) * 0x0400 + low
- 0xDC00 + 0x10000;
141 UScriptCode sc
= uscript_getScript(ch
, &error
);
142 int32_t pairIndex
= getPairIndex(ch
);
144 // Paired character handling:
146 // if it's an open character, push it onto the stack.
147 // if it's a close character, find the matching open on the
148 // stack, and use that script code. Any non-matching open
149 // characters above it on the stack will be poped.
150 if (pairIndex
>= 0) {
151 if ((pairIndex
& 1) == 0) {
152 parenStack
[++parenSP
].pairIndex
= pairIndex
;
153 parenStack
[parenSP
].scriptCode
= scriptCode
;
154 } else if (parenSP
>= 0) {
155 int32_t pi
= pairIndex
& ~1;
157 while (parenSP
>= 0 && parenStack
[parenSP
].pairIndex
!= pi
) {
161 if (parenSP
< startSP
) {
166 sc
= parenStack
[parenSP
].scriptCode
;
171 if (sameScript(scriptCode
, sc
)) {
172 if (scriptCode
<= USCRIPT_INHERITED
&& sc
> USCRIPT_INHERITED
) {
175 // now that we have a final script code, fix any open
176 // characters we pushed before we knew the script code.
177 while (startSP
< parenSP
) {
178 parenStack
[++startSP
].scriptCode
= scriptCode
;
182 // if this character is a close paired character,
183 // pop it from the stack
184 if (pairIndex
>= 0 && (pairIndex
& 1) != 0 && parenSP
>= 0) {
189 // if the run broke on a surrogate pair,
190 // end it before the high surrogate