1 // Copyright (C) 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"
22 const char ScriptRun::fgClassID=0;
24 UChar32 ScriptRun::pairedChars[] = {
25 0x0028, 0x0029, // ascii paired punctuation
29 0x00ab, 0x00bb, // guillemets
30 0x2018, 0x2019, // general punctuation
33 0x3008, 0x3009, // chinese paired punctuation
44 const int32_t ScriptRun::pairedCharCount = UPRV_LENGTHOF(pairedChars);
45 const int32_t ScriptRun::pairedCharPower = 1 << highBit(pairedCharCount);
46 const int32_t ScriptRun::pairedCharExtra = pairedCharCount - pairedCharPower;
48 int8_t ScriptRun::highBit(int32_t value)
56 if (value >= 1 << 16) {
61 if (value >= 1 << 8) {
66 if (value >= 1 << 4) {
71 if (value >= 1 << 2) {
76 if (value >= 1 << 1) {
84 int32_t ScriptRun::getPairIndex(UChar32 ch)
86 int32_t probe = pairedCharPower;
89 if (ch >= pairedChars[pairedCharExtra]) {
90 index = pairedCharExtra;
93 while (probe > (1 << 0)) {
96 if (ch >= pairedChars[index + probe]) {
101 if (pairedChars[index] != ch) {
108 UBool ScriptRun::sameScript(int32_t scriptOne, int32_t scriptTwo)
110 return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo;
113 UBool ScriptRun::next()
115 int32_t startSP = parenSP; // used to find the first new open character
116 UErrorCode error = U_ZERO_ERROR;
118 // if we've fallen off the end of the text, we're done
119 if (scriptEnd >= charLimit) {
123 scriptCode = USCRIPT_COMMON;
125 for (scriptStart = scriptEnd; scriptEnd < charLimit; scriptEnd += 1) {
126 UChar high = charArray[scriptEnd];
129 // if the character is a high surrogate and it's not the last one
130 // in the text, see if it's followed by a low surrogate
131 if (high >= 0xD800 && high <= 0xDBFF && scriptEnd < charLimit - 1)
133 UChar low = charArray[scriptEnd + 1];
135 // if it is followed by a low surrogate,
136 // consume it and form the full character
137 if (low >= 0xDC00 && low <= 0xDFFF) {
138 ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000;
143 UScriptCode sc = uscript_getScript(ch, &error);
144 int32_t pairIndex = getPairIndex(ch);
146 // Paired character handling:
148 // if it's an open character, push it onto the stack.
149 // if it's a close character, find the matching open on the
150 // stack, and use that script code. Any non-matching open
151 // characters above it on the stack will be poped.
152 if (pairIndex >= 0) {
153 if ((pairIndex & 1) == 0) {
154 parenStack[++parenSP].pairIndex = pairIndex;
155 parenStack[parenSP].scriptCode = scriptCode;
156 } else if (parenSP >= 0) {
157 int32_t pi = pairIndex & ~1;
159 while (parenSP >= 0 && parenStack[parenSP].pairIndex != pi) {
163 if (parenSP < startSP) {
168 sc = parenStack[parenSP].scriptCode;
173 if (sameScript(scriptCode, sc)) {
174 if (scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) {
177 // now that we have a final script code, fix any open
178 // characters we pushed before we knew the script code.
179 while (startSP < parenSP) {
180 parenStack[++startSP].scriptCode = scriptCode;
184 // if this character is a close paired character,
185 // pop it from the stack
186 if (pairIndex >= 0 && (pairIndex & 1) != 0 && parenSP >= 0) {
191 // if the run broke on a surrogate pair,
192 // end it before the high surrogate