]> git.saurik.com Git - apple/icu.git/blob - icuSources/extra/scrptrun/scrptrun.cpp
ICU-57131.0.1.tar.gz
[apple/icu.git] / icuSources / extra / scrptrun / scrptrun.cpp
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: scrptrun.cpp
9 *
10 * created on: 10/17/2001
11 * created by: Eric R. Mader
12 */
13
14 #include "unicode/utypes.h"
15 #include "unicode/uscript.h"
16
17 #include "cmemory.h"
18 #include "scrptrun.h"
19
20 const char ScriptRun::fgClassID=0;
21
22 UChar32 ScriptRun::pairedChars[] = {
23 0x0028, 0x0029, // ascii paired punctuation
24 0x003c, 0x003e,
25 0x005b, 0x005d,
26 0x007b, 0x007d,
27 0x00ab, 0x00bb, // guillemets
28 0x2018, 0x2019, // general punctuation
29 0x201c, 0x201d,
30 0x2039, 0x203a,
31 0x3008, 0x3009, // chinese paired punctuation
32 0x300a, 0x300b,
33 0x300c, 0x300d,
34 0x300e, 0x300f,
35 0x3010, 0x3011,
36 0x3014, 0x3015,
37 0x3016, 0x3017,
38 0x3018, 0x3019,
39 0x301a, 0x301b
40 };
41
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;
45
46 int8_t ScriptRun::highBit(int32_t value)
47 {
48 if (value <= 0) {
49 return -32;
50 }
51
52 int8_t bit = 0;
53
54 if (value >= 1 << 16) {
55 value >>= 16;
56 bit += 16;
57 }
58
59 if (value >= 1 << 8) {
60 value >>= 8;
61 bit += 8;
62 }
63
64 if (value >= 1 << 4) {
65 value >>= 4;
66 bit += 4;
67 }
68
69 if (value >= 1 << 2) {
70 value >>= 2;
71 bit += 2;
72 }
73
74 if (value >= 1 << 1) {
75 value >>= 1;
76 bit += 1;
77 }
78
79 return bit;
80 }
81
82 int32_t ScriptRun::getPairIndex(UChar32 ch)
83 {
84 int32_t probe = pairedCharPower;
85 int32_t index = 0;
86
87 if (ch >= pairedChars[pairedCharExtra]) {
88 index = pairedCharExtra;
89 }
90
91 while (probe > (1 << 0)) {
92 probe >>= 1;
93
94 if (ch >= pairedChars[index + probe]) {
95 index += probe;
96 }
97 }
98
99 if (pairedChars[index] != ch) {
100 index = -1;
101 }
102
103 return index;
104 }
105
106 UBool ScriptRun::sameScript(int32_t scriptOne, int32_t scriptTwo)
107 {
108 return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
109 }
110
111 UBool ScriptRun::next()
112 {
113 int32_t startSP = parenSP; // used to find the first new open character
114 UErrorCode error = U_ZERO_ERROR;
115
116 // if we've fallen off the end of the text, we're done
117 if (scriptEnd >= charLimit) {
118 return false;
119 }
120
121 scriptCode = USCRIPT_COMMON;
122
123 for (scriptStart = scriptEnd; scriptEnd < charLimit; scriptEnd += 1) {
124 UChar high = charArray[scriptEnd];
125 UChar32 ch = high;
126
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)
130 {
131 UChar low = charArray[scriptEnd + 1];
132
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;
137 scriptEnd += 1;
138 }
139 }
140
141 UScriptCode sc = uscript_getScript(ch, &error);
142 int32_t pairIndex = getPairIndex(ch);
143
144 // Paired character handling:
145 //
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;
156
157 while (parenSP >= 0 && parenStack[parenSP].pairIndex != pi) {
158 parenSP -= 1;
159 }
160
161 if (parenSP < startSP) {
162 startSP = parenSP;
163 }
164
165 if (parenSP >= 0) {
166 sc = parenStack[parenSP].scriptCode;
167 }
168 }
169 }
170
171 if (sameScript(scriptCode, sc)) {
172 if (scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
173 scriptCode = sc;
174
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;
179 }
180 }
181
182 // if this character is a close paired character,
183 // pop it from the stack
184 if (pairIndex >= 0 && (pairIndex & 1) != 0 && parenSP >= 0) {
185 parenSP -= 1;
186 startSP -= 1;
187 }
188 } else {
189 // if the run broke on a surrogate pair,
190 // end it before the high surrogate
191 if (ch >= 0x10000) {
192 scriptEnd -= 1;
193 }
194
195 break;
196 }
197 }
198
199 return true;
200 }
201