2 *******************************************************************************
4 * Copyright (c) 1995-2013 International Business Machines Corporation and others
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, and/or sell copies of the
12 * Software, and to permit persons to whom the Software is furnished to do so,
13 * provided that the above copyright notice(s) and this permission notice appear
14 * in all copies of the Software and that both the above copyright notice(s) and
15 * this permission notice appear in supporting documentation.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
20 * NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE
21 * LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
22 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
24 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Except as contained in this notice, the name of a copyright holder shall not be
27 * used in advertising or otherwise to promote the sale, use or other dealings in
28 * this Software without prior written authorization of the copyright holder.
30 *******************************************************************************
31 * file name: scrptrun.cpp
33 * created on: 10/17/2001
34 * created by: Eric R. Mader
37 #include "unicode/utypes.h"
38 #include "unicode/uscript.h"
42 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
44 const char ScriptRun::fgClassID
=0;
46 UChar32
ScriptRun::pairedChars
[] = {
47 0x0028, 0x0029, // ascii paired punctuation
51 0x00ab, 0x00bb, // guillemets
52 0x2018, 0x2019, // general punctuation
55 0x3008, 0x3009, // chinese paired punctuation
66 const int32_t ScriptRun::pairedCharCount
= ARRAY_SIZE(pairedChars
);
67 const int32_t ScriptRun::pairedCharPower
= 1 << highBit(pairedCharCount
);
68 const int32_t ScriptRun::pairedCharExtra
= pairedCharCount
- pairedCharPower
;
70 int8_t ScriptRun::highBit(int32_t value
)
78 if (value
>= 1 << 16) {
83 if (value
>= 1 << 8) {
88 if (value
>= 1 << 4) {
93 if (value
>= 1 << 2) {
98 if (value
>= 1 << 1) {
106 int32_t ScriptRun::getPairIndex(UChar32 ch
)
108 int32_t probe
= pairedCharPower
;
111 if (ch
>= pairedChars
[pairedCharExtra
]) {
112 index
= pairedCharExtra
;
115 while (probe
> (1 << 0)) {
118 if (ch
>= pairedChars
[index
+ probe
]) {
123 if (pairedChars
[index
] != ch
) {
130 UBool
ScriptRun::sameScript(int32_t scriptOne
, int32_t scriptTwo
)
132 return scriptOne
<= USCRIPT_INHERITED
|| scriptTwo
<= USCRIPT_INHERITED
|| scriptOne
== scriptTwo
;
135 UBool
ScriptRun::next()
137 int32_t startSP
= parenSP
; // used to find the first new open character
138 UErrorCode error
= U_ZERO_ERROR
;
140 // if we've fallen off the end of the text, we're done
141 if (scriptEnd
>= charLimit
) {
145 scriptCode
= USCRIPT_COMMON
;
147 for (scriptStart
= scriptEnd
; scriptEnd
< charLimit
; scriptEnd
+= 1) {
148 UChar high
= charArray
[scriptEnd
];
151 // if the character is a high surrogate and it's not the last one
152 // in the text, see if it's followed by a low surrogate
153 if (high
>= 0xD800 && high
<= 0xDBFF && scriptEnd
< charLimit
- 1)
155 UChar low
= charArray
[scriptEnd
+ 1];
157 // if it is followed by a low surrogate,
158 // consume it and form the full character
159 if (low
>= 0xDC00 && low
<= 0xDFFF) {
160 ch
= (high
- 0xD800) * 0x0400 + low
- 0xDC00 + 0x10000;
165 UScriptCode sc
= uscript_getScript(ch
, &error
);
166 int32_t pairIndex
= getPairIndex(ch
);
168 // Paired character handling:
170 // if it's an open character, push it onto the stack.
171 // if it's a close character, find the matching open on the
172 // stack, and use that script code. Any non-matching open
173 // characters above it on the stack will be poped.
174 if (pairIndex
>= 0) {
175 if ((pairIndex
& 1) == 0) {
176 parenStack
[++parenSP
].pairIndex
= pairIndex
;
177 parenStack
[parenSP
].scriptCode
= scriptCode
;
178 } else if (parenSP
>= 0) {
179 int32_t pi
= pairIndex
& ~1;
181 while (parenSP
>= 0 && parenStack
[parenSP
].pairIndex
!= pi
) {
185 if (parenSP
< startSP
) {
190 sc
= parenStack
[parenSP
].scriptCode
;
195 if (sameScript(scriptCode
, sc
)) {
196 if (scriptCode
<= USCRIPT_INHERITED
&& sc
> USCRIPT_INHERITED
) {
199 // now that we have a final script code, fix any open
200 // characters we pushed before we knew the script code.
201 while (startSP
< parenSP
) {
202 parenStack
[++startSP
].scriptCode
= scriptCode
;
206 // if this character is a close paired character,
207 // pop it from the stack
208 if (pairIndex
>= 0 && (pairIndex
& 1) != 0 && parenSP
>= 0) {
213 // if the run broke on a surrogate pair,
214 // end it before the high surrogate