Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/common/usc_impl.cpp
Line
Count
Source (jump to first uncovered line)
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
0
#define PAREN_STACK_DEPTH 32
23
24
0
#define MOD(sp) ((sp) % PAREN_STACK_DEPTH)
25
0
#define LIMIT_INC(sp) (((sp) < PAREN_STACK_DEPTH)? (sp) + 1 : PAREN_STACK_DEPTH)
26
0
#define INC(sp,count) (MOD((sp) + (count)))
27
0
#define INC1(sp) (INC(sp, 1))
28
0
#define DEC(sp,count) (MOD((sp) + PAREN_STACK_DEPTH - (count)))
29
0
#define DEC1(sp) (DEC(sp, 1))
30
0
#define STACK_IS_EMPTY(scriptRun) ((scriptRun)->pushCount <= 0)
31
0
#define STACK_IS_NOT_EMPTY(scriptRun) (! STACK_IS_EMPTY(scriptRun))
32
0
#define TOP(scriptRun) ((scriptRun)->parenStack[(scriptRun)->parenSP])
33
0
#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
static void push(UScriptRun *scriptRun, int32_t pairIndex, UScriptCode scriptCode)
79
0
{
80
0
    scriptRun->pushCount  = LIMIT_INC(scriptRun->pushCount);
81
0
    scriptRun->fixupCount = LIMIT_INC(scriptRun->fixupCount);
82
0
    
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
static void pop(UScriptRun *scriptRun)
89
0
{
90
0
    if (STACK_IS_EMPTY(scriptRun)) {
91
0
        return;
92
0
    }
93
0
    
94
0
    if (scriptRun->fixupCount > 0) {
95
0
        scriptRun->fixupCount -= 1;
96
0
    }
97
0
    
98
0
    scriptRun->pushCount -= 1;
99
0
    scriptRun->parenSP = DEC1(scriptRun->parenSP);
100
0
    
101
0
    /* If the stack is now empty, reset the stack
102
0
       pointers to their initial values.
103
0
     */
104
0
    if (STACK_IS_EMPTY(scriptRun)) {
105
0
        scriptRun->parenSP = -1;
106
0
    }
107
0
}
108
109
static void fixup(UScriptRun *scriptRun, UScriptCode scriptCode)
110
0
{
111
0
    int32_t fixupSP = DEC(scriptRun->parenSP, scriptRun->fixupCount);
112
0
    
113
0
    while (scriptRun->fixupCount-- > 0) {
114
0
        fixupSP = INC1(fixupSP);
115
0
        scriptRun->parenStack[fixupSP].scriptCode = scriptCode;
116
0
    }
117
0
}
118
119
static int8_t
120
highBit(int32_t value)
121
0
{
122
0
    int8_t bit = 0;
123
0
124
0
    if (value <= 0) {
125
0
        return -32;
126
0
    }
127
0
128
0
    if (value >= 1 << 16) {
129
0
        value >>= 16;
130
0
        bit += 16;
131
0
    }
132
0
133
0
    if (value >= 1 << 8) {
134
0
        value >>= 8;
135
0
        bit += 8;
136
0
    }
137
0
138
0
    if (value >= 1 << 4) {
139
0
        value >>= 4;
140
0
        bit += 4;
141
0
    }
142
0
143
0
    if (value >= 1 << 2) {
144
0
        value >>= 2;
145
0
        bit += 2;
146
0
    }
147
0
148
0
    if (value >= 1 << 1) {
149
0
        //value >>= 1;
150
0
        bit += 1;
151
0
    }
152
0
153
0
    return bit;
154
0
}
155
156
static int32_t
157
getPairIndex(UChar32 ch)
158
0
{
159
0
    int32_t pairedCharCount = UPRV_LENGTHOF(pairedChars);
160
0
    int32_t pairedCharPower = 1 << highBit(pairedCharCount);
161
0
    int32_t pairedCharExtra = pairedCharCount - pairedCharPower;
162
0
163
0
    int32_t probe = pairedCharPower;
164
0
    int32_t pairIndex = 0;
165
0
166
0
    if (ch >= pairedChars[pairedCharExtra]) {
167
0
        pairIndex = pairedCharExtra;
168
0
    }
169
0
170
0
    while (probe > (1 << 0)) {
171
0
        probe >>= 1;
172
0
173
0
        if (ch >= pairedChars[pairIndex + probe]) {
174
0
            pairIndex += probe;
175
0
        }
176
0
    }
177
0
178
0
    if (pairedChars[pairIndex] != ch) {
179
0
        pairIndex = -1;
180
0
    }
181
0
182
0
    return pairIndex;
183
0
}
184
185
static UBool
186
sameScript(UScriptCode scriptOne, UScriptCode scriptTwo)
187
0
{
188
0
    return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
189
0
}
190
191
U_CAPI UScriptRun * U_EXPORT2
192
uscript_openRun(const UChar *src, int32_t length, UErrorCode *pErrorCode)
193
0
{
194
0
    UScriptRun *result = NULL;
195
0
196
0
    if (pErrorCode == NULL || U_FAILURE(*pErrorCode)) {
197
0
        return NULL;
198
0
    }
199
0
200
0
    result = (UScriptRun *)uprv_malloc(sizeof (UScriptRun));
201
0
202
0
    if (result == NULL) {
203
0
        *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
204
0
        return NULL;
205
0
    }
206
0
207
0
    uscript_setRunText(result, src, length, pErrorCode);
208
0
209
0
    /* 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
0
    }
214
0
215
0
    return result;
216
0
}
217
218
U_CAPI void U_EXPORT2
219
uscript_closeRun(UScriptRun *scriptRun)
220
0
{
221
0
    if (scriptRun != NULL) {
222
0
        uprv_free(scriptRun);
223
0
    }
224
0
}
225
226
U_CAPI void U_EXPORT2
227
uscript_resetRun(UScriptRun *scriptRun)
228
0
{
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
0
    }
237
0
}
238
239
U_CAPI void U_EXPORT2
240
uscript_setRunText(UScriptRun *scriptRun, const UChar *src, int32_t length, UErrorCode *pErrorCode)
241
0
{
242
0
    if (pErrorCode == NULL || U_FAILURE(*pErrorCode)) {
243
0
        return;
244
0
    }
245
0
246
0
    if (scriptRun == NULL || length < 0 || ((src == NULL) != (length == 0))) {
247
0
        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
248
0
        return;
249
0
    }
250
0
251
0
    scriptRun->textArray  = src;
252
0
    scriptRun->textLength = length;
253
0
254
0
    uscript_resetRun(scriptRun);
255
0
}
256
257
U_CAPI UBool U_EXPORT2
258
uscript_nextRun(UScriptRun *scriptRun, int32_t *pRunStart, int32_t *pRunLimit, UScriptCode *pRunScript)
259
0
{
260
0
    UErrorCode error = U_ZERO_ERROR;
261
0
262
0
    /* 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
0
    }
266
0
    
267
0
    SYNC_FIXUP(scriptRun);
268
0
    scriptRun->scriptCode = USCRIPT_COMMON;
269
0
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
0
        UScriptCode sc;
274
0
        int32_t pairIndex;
275
0
276
0
        /*
277
0
         * if the character is a high surrogate and it's not the last one
278
0
         * in the text, see if it's followed by a low surrogate
279
0
         */
280
0
        if (high >= 0xD800 && high <= 0xDBFF && scriptRun->scriptLimit < scriptRun->textLength - 1) {
281
0
            UChar low = scriptRun->textArray[scriptRun->scriptLimit + 1];
282
0
283
0
            /*
284
0
             * if it is followed by a low surrogate,
285
0
             * consume it and form the full character
286
0
             */
287
0
            if (low >= 0xDC00 && low <= 0xDFFF) {
288
0
                ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000;
289
0
                scriptRun->scriptLimit += 1;
290
0
            }
291
0
        }
292
0
293
0
        sc = uscript_getScript(ch, &error);
294
0
        pairIndex = getPairIndex(ch);
295
0
296
0
        /*
297
0
         * Paired character handling:
298
0
         *
299
0
         * if it's an open character, push it onto the stack.
300
0
         * if it's a close character, find the matching open on the
301
0
         * stack, and use that script code. Any non-matching open
302
0
         * characters above it on the stack will be poped.
303
0
         */
304
0
        if (pairIndex >= 0) {
305
0
            if ((pairIndex & 1) == 0) {
306
0
                push(scriptRun, pairIndex, scriptRun->scriptCode);
307
0
            } else {
308
0
                int32_t pi = pairIndex & ~1;
309
0
310
0
                while (STACK_IS_NOT_EMPTY(scriptRun) && TOP(scriptRun).pairIndex != pi) {
311
0
                    pop(scriptRun);
312
0
                }
313
0
314
0
                if (STACK_IS_NOT_EMPTY(scriptRun)) {
315
0
                    sc = TOP(scriptRun).scriptCode;
316
0
                }
317
0
            }
318
0
        }
319
0
320
0
        if (sameScript(scriptRun->scriptCode, sc)) {
321
0
            if (scriptRun->scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
322
0
                scriptRun->scriptCode = sc;
323
0
324
0
                fixup(scriptRun, scriptRun->scriptCode);
325
0
            }
326
0
327
0
            /*
328
0
             * if this character is a close paired character,
329
0
             * pop the matching open character from the stack
330
0
             */
331
0
            if (pairIndex >= 0 && (pairIndex & 1) != 0) {
332
0
                pop(scriptRun);
333
0
            }
334
0
        } else {
335
0
            /*
336
0
             * if the run broke on a surrogate pair,
337
0
             * end it before the high surrogate
338
0
             */
339
0
            if (ch >= 0x10000) {
340
0
                scriptRun->scriptLimit -= 1;
341
0
            }
342
0
343
0
            break;
344
0
        }
345
0
    }
346
0
347
0
348
0
    if (pRunStart != NULL) {
349
0
        *pRunStart = scriptRun->scriptStart;
350
0
    }
351
0
352
0
    if (pRunLimit != NULL) {
353
0
        *pRunLimit = scriptRun->scriptLimit;
354
0
    }
355
0
356
0
    if (pRunScript != NULL) {
357
0
        *pRunScript = scriptRun->scriptCode;
358
0
    }
359
0
360
0
    return TRUE;
361
0
}