Line data Source code
1 : // © 2016 and later: Unicode, Inc. and others.
2 : // License & terms of use: http://www.unicode.org/copyright.html
3 : /*
4 : **********************************************************************
5 : * Copyright (C) 1999-2016, International Business Machines
6 : * Corporation and others. All Rights Reserved.
7 : **********************************************************************
8 : *
9 : * File USC_IMPL.C
10 : *
11 : * Modification History:
12 : *
13 : * Date Name Description
14 : * 07/08/2002 Eric Mader Creation.
15 : ******************************************************************************
16 : */
17 :
18 : #include "unicode/uscript.h"
19 : #include "usc_impl.h"
20 : #include "cmemory.h"
21 :
22 : #define PAREN_STACK_DEPTH 32
23 :
24 : #define MOD(sp) ((sp) % PAREN_STACK_DEPTH)
25 : #define LIMIT_INC(sp) (((sp) < PAREN_STACK_DEPTH)? (sp) + 1 : PAREN_STACK_DEPTH)
26 : #define INC(sp,count) (MOD((sp) + (count)))
27 : #define INC1(sp) (INC(sp, 1))
28 : #define DEC(sp,count) (MOD((sp) + PAREN_STACK_DEPTH - (count)))
29 : #define DEC1(sp) (DEC(sp, 1))
30 : #define STACK_IS_EMPTY(scriptRun) ((scriptRun)->pushCount <= 0)
31 : #define STACK_IS_NOT_EMPTY(scriptRun) (! STACK_IS_EMPTY(scriptRun))
32 : #define TOP(scriptRun) ((scriptRun)->parenStack[(scriptRun)->parenSP])
33 : #define SYNC_FIXUP(scriptRun) ((scriptRun)->fixupCount = 0)
34 :
35 : struct ParenStackEntry
36 : {
37 : int32_t pairIndex;
38 : UScriptCode scriptCode;
39 : };
40 :
41 : struct UScriptRun
42 : {
43 : int32_t textLength;
44 : const UChar *textArray;
45 :
46 : int32_t scriptStart;
47 : int32_t scriptLimit;
48 : UScriptCode scriptCode;
49 :
50 : struct ParenStackEntry parenStack[PAREN_STACK_DEPTH];
51 : int32_t parenSP;
52 : int32_t pushCount;
53 : int32_t fixupCount;
54 : };
55 :
56 : static int8_t highBit(int32_t value);
57 :
58 : static const UChar32 pairedChars[] = {
59 : 0x0028, 0x0029, /* ascii paired punctuation */
60 : 0x003c, 0x003e,
61 : 0x005b, 0x005d,
62 : 0x007b, 0x007d,
63 : 0x00ab, 0x00bb, /* guillemets */
64 : 0x2018, 0x2019, /* general punctuation */
65 : 0x201c, 0x201d,
66 : 0x2039, 0x203a,
67 : 0x3008, 0x3009, /* chinese paired punctuation */
68 : 0x300a, 0x300b,
69 : 0x300c, 0x300d,
70 : 0x300e, 0x300f,
71 : 0x3010, 0x3011,
72 : 0x3014, 0x3015,
73 : 0x3016, 0x3017,
74 : 0x3018, 0x3019,
75 : 0x301a, 0x301b
76 : };
77 :
78 0 : static void push(UScriptRun *scriptRun, int32_t pairIndex, UScriptCode scriptCode)
79 : {
80 0 : scriptRun->pushCount = LIMIT_INC(scriptRun->pushCount);
81 0 : scriptRun->fixupCount = LIMIT_INC(scriptRun->fixupCount);
82 :
83 0 : scriptRun->parenSP = INC1(scriptRun->parenSP);
84 0 : scriptRun->parenStack[scriptRun->parenSP].pairIndex = pairIndex;
85 0 : scriptRun->parenStack[scriptRun->parenSP].scriptCode = scriptCode;
86 0 : }
87 :
88 0 : static void pop(UScriptRun *scriptRun)
89 : {
90 0 : if (STACK_IS_EMPTY(scriptRun)) {
91 0 : return;
92 : }
93 :
94 0 : if (scriptRun->fixupCount > 0) {
95 0 : scriptRun->fixupCount -= 1;
96 : }
97 :
98 0 : scriptRun->pushCount -= 1;
99 0 : scriptRun->parenSP = DEC1(scriptRun->parenSP);
100 :
101 : /* If the stack is now empty, reset the stack
102 : pointers to their initial values.
103 : */
104 0 : if (STACK_IS_EMPTY(scriptRun)) {
105 0 : scriptRun->parenSP = -1;
106 : }
107 : }
108 :
109 0 : static void fixup(UScriptRun *scriptRun, UScriptCode scriptCode)
110 : {
111 0 : int32_t fixupSP = DEC(scriptRun->parenSP, scriptRun->fixupCount);
112 :
113 0 : while (scriptRun->fixupCount-- > 0) {
114 0 : fixupSP = INC1(fixupSP);
115 0 : scriptRun->parenStack[fixupSP].scriptCode = scriptCode;
116 : }
117 0 : }
118 :
119 : static int8_t
120 0 : highBit(int32_t value)
121 : {
122 0 : int8_t bit = 0;
123 :
124 0 : if (value <= 0) {
125 0 : return -32;
126 : }
127 :
128 0 : if (value >= 1 << 16) {
129 0 : value >>= 16;
130 0 : bit += 16;
131 : }
132 :
133 0 : if (value >= 1 << 8) {
134 0 : value >>= 8;
135 0 : bit += 8;
136 : }
137 :
138 0 : if (value >= 1 << 4) {
139 0 : value >>= 4;
140 0 : bit += 4;
141 : }
142 :
143 0 : if (value >= 1 << 2) {
144 0 : value >>= 2;
145 0 : bit += 2;
146 : }
147 :
148 0 : if (value >= 1 << 1) {
149 : //value >>= 1;
150 0 : bit += 1;
151 : }
152 :
153 0 : return bit;
154 : }
155 :
156 : static int32_t
157 0 : getPairIndex(UChar32 ch)
158 : {
159 0 : int32_t pairedCharCount = UPRV_LENGTHOF(pairedChars);
160 0 : int32_t pairedCharPower = 1 << highBit(pairedCharCount);
161 0 : int32_t pairedCharExtra = pairedCharCount - pairedCharPower;
162 :
163 0 : int32_t probe = pairedCharPower;
164 0 : int32_t pairIndex = 0;
165 :
166 0 : if (ch >= pairedChars[pairedCharExtra]) {
167 0 : pairIndex = pairedCharExtra;
168 : }
169 :
170 0 : while (probe > (1 << 0)) {
171 0 : probe >>= 1;
172 :
173 0 : if (ch >= pairedChars[pairIndex + probe]) {
174 0 : pairIndex += probe;
175 : }
176 : }
177 :
178 0 : if (pairedChars[pairIndex] != ch) {
179 0 : pairIndex = -1;
180 : }
181 :
182 0 : return pairIndex;
183 : }
184 :
185 : static UBool
186 0 : sameScript(UScriptCode scriptOne, UScriptCode scriptTwo)
187 : {
188 0 : return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
189 : }
190 :
191 : U_CAPI UScriptRun * U_EXPORT2
192 0 : uscript_openRun(const UChar *src, int32_t length, UErrorCode *pErrorCode)
193 : {
194 0 : UScriptRun *result = NULL;
195 :
196 0 : if (pErrorCode == NULL || U_FAILURE(*pErrorCode)) {
197 0 : return NULL;
198 : }
199 :
200 0 : result = (UScriptRun *)uprv_malloc(sizeof (UScriptRun));
201 :
202 0 : if (result == NULL) {
203 0 : *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
204 0 : return NULL;
205 : }
206 :
207 0 : uscript_setRunText(result, src, length, pErrorCode);
208 :
209 : /* Release the UScriptRun if uscript_setRunText() returns an error */
210 0 : if (U_FAILURE(*pErrorCode)) {
211 0 : uprv_free(result);
212 0 : result = NULL;
213 : }
214 :
215 0 : return result;
216 : }
217 :
218 : U_CAPI void U_EXPORT2
219 0 : uscript_closeRun(UScriptRun *scriptRun)
220 : {
221 0 : if (scriptRun != NULL) {
222 0 : uprv_free(scriptRun);
223 : }
224 0 : }
225 :
226 : U_CAPI void U_EXPORT2
227 0 : uscript_resetRun(UScriptRun *scriptRun)
228 : {
229 0 : if (scriptRun != NULL) {
230 0 : scriptRun->scriptStart = 0;
231 0 : scriptRun->scriptLimit = 0;
232 0 : scriptRun->scriptCode = USCRIPT_INVALID_CODE;
233 0 : scriptRun->parenSP = -1;
234 0 : scriptRun->pushCount = 0;
235 0 : scriptRun->fixupCount = 0;
236 : }
237 0 : }
238 :
239 : U_CAPI void U_EXPORT2
240 0 : uscript_setRunText(UScriptRun *scriptRun, const UChar *src, int32_t length, UErrorCode *pErrorCode)
241 : {
242 0 : if (pErrorCode == NULL || U_FAILURE(*pErrorCode)) {
243 0 : return;
244 : }
245 :
246 0 : if (scriptRun == NULL || length < 0 || ((src == NULL) != (length == 0))) {
247 0 : *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
248 0 : return;
249 : }
250 :
251 0 : scriptRun->textArray = src;
252 0 : scriptRun->textLength = length;
253 :
254 0 : uscript_resetRun(scriptRun);
255 : }
256 :
257 : U_CAPI UBool U_EXPORT2
258 0 : uscript_nextRun(UScriptRun *scriptRun, int32_t *pRunStart, int32_t *pRunLimit, UScriptCode *pRunScript)
259 : {
260 0 : UErrorCode error = U_ZERO_ERROR;
261 :
262 : /* if we've fallen off the end of the text, we're done */
263 0 : if (scriptRun == NULL || scriptRun->scriptLimit >= scriptRun->textLength) {
264 0 : return FALSE;
265 : }
266 :
267 0 : SYNC_FIXUP(scriptRun);
268 0 : scriptRun->scriptCode = USCRIPT_COMMON;
269 :
270 0 : for (scriptRun->scriptStart = scriptRun->scriptLimit; scriptRun->scriptLimit < scriptRun->textLength; scriptRun->scriptLimit += 1) {
271 0 : UChar high = scriptRun->textArray[scriptRun->scriptLimit];
272 0 : UChar32 ch = high;
273 : UScriptCode sc;
274 : int32_t pairIndex;
275 :
276 : /*
277 : * if the character is a high surrogate and it's not the last one
278 : * in the text, see if it's followed by a low surrogate
279 : */
280 0 : if (high >= 0xD800 && high <= 0xDBFF && scriptRun->scriptLimit < scriptRun->textLength - 1) {
281 0 : UChar low = scriptRun->textArray[scriptRun->scriptLimit + 1];
282 :
283 : /*
284 : * if it is followed by a low surrogate,
285 : * consume it and form the full character
286 : */
287 0 : if (low >= 0xDC00 && low <= 0xDFFF) {
288 0 : ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000;
289 0 : scriptRun->scriptLimit += 1;
290 : }
291 : }
292 :
293 0 : sc = uscript_getScript(ch, &error);
294 0 : pairIndex = getPairIndex(ch);
295 :
296 : /*
297 : * Paired character handling:
298 : *
299 : * if it's an open character, push it onto the stack.
300 : * if it's a close character, find the matching open on the
301 : * stack, and use that script code. Any non-matching open
302 : * characters above it on the stack will be poped.
303 : */
304 0 : if (pairIndex >= 0) {
305 0 : if ((pairIndex & 1) == 0) {
306 0 : push(scriptRun, pairIndex, scriptRun->scriptCode);
307 : } else {
308 0 : int32_t pi = pairIndex & ~1;
309 :
310 0 : while (STACK_IS_NOT_EMPTY(scriptRun) && TOP(scriptRun).pairIndex != pi) {
311 0 : pop(scriptRun);
312 : }
313 :
314 0 : if (STACK_IS_NOT_EMPTY(scriptRun)) {
315 0 : sc = TOP(scriptRun).scriptCode;
316 : }
317 : }
318 : }
319 :
320 0 : if (sameScript(scriptRun->scriptCode, sc)) {
321 0 : if (scriptRun->scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
322 0 : scriptRun->scriptCode = sc;
323 :
324 0 : fixup(scriptRun, scriptRun->scriptCode);
325 : }
326 :
327 : /*
328 : * if this character is a close paired character,
329 : * pop the matching open character from the stack
330 : */
331 0 : if (pairIndex >= 0 && (pairIndex & 1) != 0) {
332 0 : pop(scriptRun);
333 : }
334 : } else {
335 : /*
336 : * if the run broke on a surrogate pair,
337 : * end it before the high surrogate
338 : */
339 0 : if (ch >= 0x10000) {
340 0 : scriptRun->scriptLimit -= 1;
341 : }
342 :
343 0 : break;
344 : }
345 : }
346 :
347 :
348 0 : if (pRunStart != NULL) {
349 0 : *pRunStart = scriptRun->scriptStart;
350 : }
351 :
352 0 : if (pRunLimit != NULL) {
353 0 : *pRunLimit = scriptRun->scriptLimit;
354 : }
355 :
356 0 : if (pRunScript != NULL) {
357 0 : *pRunScript = scriptRun->scriptCode;
358 : }
359 :
360 0 : return TRUE;
361 : }
|