]>
git.saurik.com Git - apple/icu.git/blob - icuSources/extra/scrptrun/scrptrun.cpp
0a438c316a686bf5a5ac6af2af041ce9f94b50a4
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1999-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: scrptrun.cpp
12 * created on: 10/17/2001
13 * created by: Eric R. Mader
16 #include "unicode/utypes.h"
17 #include "unicode/uscript.h"
24 const char ScriptRun::fgClassID
=0;
26 UChar32
ScriptRun::pairedChars
[] = {
27 0x0028, 0x0029, // ascii paired punctuation
31 0x00ab, 0x00bb, // guillemets
32 0x2018, 0x2019, // general punctuation
35 0x3008, 0x3009, // chinese paired punctuation
46 const int32_t ScriptRun::pairedCharCount
= UPRV_LENGTHOF(pairedChars
);
47 const int32_t ScriptRun::pairedCharPower
= 1 << highBit(pairedCharCount
);
48 const int32_t ScriptRun::pairedCharExtra
= pairedCharCount
- pairedCharPower
;
50 int8_t ScriptRun::highBit(int32_t value
)
58 if (value
>= 1 << 16) {
63 if (value
>= 1 << 8) {
68 if (value
>= 1 << 4) {
73 if (value
>= 1 << 2) {
78 if (value
>= 1 << 1) {
86 int32_t ScriptRun::getPairIndex(UChar32 ch
)
88 int32_t probe
= pairedCharPower
;
91 if (ch
>= pairedChars
[pairedCharExtra
]) {
92 index
= pairedCharExtra
;
95 while (probe
> (1 << 0)) {
98 if (ch
>= pairedChars
[index
+ probe
]) {
103 if (pairedChars
[index
] != ch
) {
110 UBool
ScriptRun::sameScript(int32_t scriptOne
, int32_t scriptTwo
)
112 return scriptOne
<= USCRIPT_INHERITED
|| scriptTwo
<= USCRIPT_INHERITED
|| scriptOne
== scriptTwo
;
115 UBool
ScriptRun::next()
117 int32_t startSP
= parenSP
; // used to find the first new open character
118 UErrorCode error
= U_ZERO_ERROR
;
120 // if we've fallen off the end of the text, we're done
121 if (scriptEnd
>= charLimit
) {
125 scriptCode
= USCRIPT_COMMON
;
127 for (scriptStart
= scriptEnd
; scriptEnd
< charLimit
; scriptEnd
+= 1) {
128 UChar high
= charArray
[scriptEnd
];
131 // if the character is a high surrogate and it's not the last one
132 // in the text, see if it's followed by a low surrogate
133 if (high
>= 0xD800 && high
<= 0xDBFF && scriptEnd
< charLimit
- 1)
135 UChar low
= charArray
[scriptEnd
+ 1];
137 // if it is followed by a low surrogate,
138 // consume it and form the full character
139 if (low
>= 0xDC00 && low
<= 0xDFFF) {
140 ch
= (high
- 0xD800) * 0x0400 + low
- 0xDC00 + 0x10000;
145 UScriptCode sc
= uscript_getScript(ch
, &error
);
146 int32_t pairIndex
= getPairIndex(ch
);
148 // Paired character handling:
150 // if it's an open character, push it onto the stack.
151 // if it's a close character, find the matching open on the
152 // stack, and use that script code. Any non-matching open
153 // characters above it on the stack will be poped.
154 if (pairIndex
>= 0) {
155 if ((pairIndex
& 1) == 0) {
156 parenStack
[++parenSP
].pairIndex
= pairIndex
;
157 parenStack
[parenSP
].scriptCode
= scriptCode
;
158 } else if (parenSP
>= 0) {
159 int32_t pi
= pairIndex
& ~1;
161 while (parenSP
>= 0 && parenStack
[parenSP
].pairIndex
!= pi
) {
165 if (parenSP
< startSP
) {
170 sc
= parenStack
[parenSP
].scriptCode
;
175 if (sameScript(scriptCode
, sc
)) {
176 if (scriptCode
<= USCRIPT_INHERITED
&& sc
> USCRIPT_INHERITED
) {
179 // now that we have a final script code, fix any open
180 // characters we pushed before we knew the script code.
181 while (startSP
< parenSP
) {
182 parenStack
[++startSP
].scriptCode
= scriptCode
;
186 // if this character is a close paired character,
187 // pop it from the stack
188 if (pairIndex
>= 0 && (pairIndex
& 1) != 0 && parenSP
>= 0) {
193 // if the run broke on a surrogate pair,
194 // end it before the high surrogate