michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 1999-2009, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * michael@0: * File USC_IMPL.C michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 07/08/2002 Eric Mader Creation. michael@0: ****************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/uscript.h" michael@0: #include "usc_impl.h" michael@0: #include "cmemory.h" michael@0: michael@0: #define ARRAY_SIZE(array) (sizeof array / sizeof array[0]) michael@0: michael@0: #define PAREN_STACK_DEPTH 32 michael@0: michael@0: #define MOD(sp) ((sp) % PAREN_STACK_DEPTH) michael@0: #define LIMIT_INC(sp) (((sp) < PAREN_STACK_DEPTH)? (sp) + 1 : PAREN_STACK_DEPTH) michael@0: #define INC(sp,count) (MOD((sp) + (count))) michael@0: #define INC1(sp) (INC(sp, 1)) michael@0: #define DEC(sp,count) (MOD((sp) + PAREN_STACK_DEPTH - (count))) michael@0: #define DEC1(sp) (DEC(sp, 1)) michael@0: #define STACK_IS_EMPTY(scriptRun) ((scriptRun)->pushCount <= 0) michael@0: #define STACK_IS_NOT_EMPTY(scriptRun) (! STACK_IS_EMPTY(scriptRun)) michael@0: #define TOP(scriptRun) ((scriptRun)->parenStack[(scriptRun)->parenSP]) michael@0: #define SYNC_FIXUP(scriptRun) ((scriptRun)->fixupCount = 0) michael@0: michael@0: struct ParenStackEntry michael@0: { michael@0: int32_t pairIndex; michael@0: UScriptCode scriptCode; michael@0: }; michael@0: michael@0: struct UScriptRun michael@0: { michael@0: int32_t textLength; michael@0: const UChar *textArray; michael@0: michael@0: int32_t scriptStart; michael@0: int32_t scriptLimit; michael@0: UScriptCode scriptCode; michael@0: michael@0: struct ParenStackEntry parenStack[PAREN_STACK_DEPTH]; michael@0: int32_t parenSP; michael@0: int32_t pushCount; michael@0: int32_t fixupCount; michael@0: }; michael@0: michael@0: static int8_t highBit(int32_t value); michael@0: michael@0: static const UChar32 pairedChars[] = { michael@0: 0x0028, 0x0029, /* ascii paired punctuation */ michael@0: 0x003c, 0x003e, michael@0: 0x005b, 0x005d, michael@0: 0x007b, 0x007d, michael@0: 0x00ab, 0x00bb, /* guillemets */ michael@0: 0x2018, 0x2019, /* general punctuation */ michael@0: 0x201c, 0x201d, michael@0: 0x2039, 0x203a, michael@0: 0x3008, 0x3009, /* chinese paired punctuation */ michael@0: 0x300a, 0x300b, michael@0: 0x300c, 0x300d, michael@0: 0x300e, 0x300f, michael@0: 0x3010, 0x3011, michael@0: 0x3014, 0x3015, michael@0: 0x3016, 0x3017, michael@0: 0x3018, 0x3019, michael@0: 0x301a, 0x301b michael@0: }; michael@0: michael@0: static void push(UScriptRun *scriptRun, int32_t pairIndex, UScriptCode scriptCode) michael@0: { michael@0: scriptRun->pushCount = LIMIT_INC(scriptRun->pushCount); michael@0: scriptRun->fixupCount = LIMIT_INC(scriptRun->fixupCount); michael@0: michael@0: scriptRun->parenSP = INC1(scriptRun->parenSP); michael@0: scriptRun->parenStack[scriptRun->parenSP].pairIndex = pairIndex; michael@0: scriptRun->parenStack[scriptRun->parenSP].scriptCode = scriptCode; michael@0: } michael@0: michael@0: static void pop(UScriptRun *scriptRun) michael@0: { michael@0: if (STACK_IS_EMPTY(scriptRun)) { michael@0: return; michael@0: } michael@0: michael@0: if (scriptRun->fixupCount > 0) { michael@0: scriptRun->fixupCount -= 1; michael@0: } michael@0: michael@0: scriptRun->pushCount -= 1; michael@0: scriptRun->parenSP = DEC1(scriptRun->parenSP); michael@0: michael@0: /* If the stack is now empty, reset the stack michael@0: pointers to their initial values. michael@0: */ michael@0: if (STACK_IS_EMPTY(scriptRun)) { michael@0: scriptRun->parenSP = -1; michael@0: } michael@0: } michael@0: michael@0: static void fixup(UScriptRun *scriptRun, UScriptCode scriptCode) michael@0: { michael@0: int32_t fixupSP = DEC(scriptRun->parenSP, scriptRun->fixupCount); michael@0: michael@0: while (scriptRun->fixupCount-- > 0) { michael@0: fixupSP = INC1(fixupSP); michael@0: scriptRun->parenStack[fixupSP].scriptCode = scriptCode; michael@0: } michael@0: } michael@0: michael@0: static int8_t michael@0: highBit(int32_t value) michael@0: { michael@0: int8_t bit = 0; michael@0: michael@0: if (value <= 0) { michael@0: return -32; michael@0: } michael@0: michael@0: if (value >= 1 << 16) { michael@0: value >>= 16; michael@0: bit += 16; michael@0: } michael@0: michael@0: if (value >= 1 << 8) { michael@0: value >>= 8; michael@0: bit += 8; michael@0: } michael@0: michael@0: if (value >= 1 << 4) { michael@0: value >>= 4; michael@0: bit += 4; michael@0: } michael@0: michael@0: if (value >= 1 << 2) { michael@0: value >>= 2; michael@0: bit += 2; michael@0: } michael@0: michael@0: if (value >= 1 << 1) { michael@0: value >>= 1; michael@0: bit += 1; michael@0: } michael@0: michael@0: return bit; michael@0: } michael@0: michael@0: static int32_t michael@0: getPairIndex(UChar32 ch) michael@0: { michael@0: int32_t pairedCharCount = ARRAY_SIZE(pairedChars); michael@0: int32_t pairedCharPower = 1 << highBit(pairedCharCount); michael@0: int32_t pairedCharExtra = pairedCharCount - pairedCharPower; michael@0: michael@0: int32_t probe = pairedCharPower; michael@0: int32_t pairIndex = 0; michael@0: michael@0: if (ch >= pairedChars[pairedCharExtra]) { michael@0: pairIndex = pairedCharExtra; michael@0: } michael@0: michael@0: while (probe > (1 << 0)) { michael@0: probe >>= 1; michael@0: michael@0: if (ch >= pairedChars[pairIndex + probe]) { michael@0: pairIndex += probe; michael@0: } michael@0: } michael@0: michael@0: if (pairedChars[pairIndex] != ch) { michael@0: pairIndex = -1; michael@0: } michael@0: michael@0: return pairIndex; michael@0: } michael@0: michael@0: static UBool michael@0: sameScript(UScriptCode scriptOne, UScriptCode scriptTwo) michael@0: { michael@0: return scriptOne <= USCRIPT_INHERITED || scriptTwo <= USCRIPT_INHERITED || scriptOne == scriptTwo; michael@0: } michael@0: michael@0: U_CAPI UScriptRun * U_EXPORT2 michael@0: uscript_openRun(const UChar *src, int32_t length, UErrorCode *pErrorCode) michael@0: { michael@0: UScriptRun *result = NULL; michael@0: michael@0: if (pErrorCode == NULL || U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: michael@0: result = uprv_malloc(sizeof (UScriptRun)); michael@0: michael@0: if (result == NULL) { michael@0: *pErrorCode = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: uscript_setRunText(result, src, length, pErrorCode); michael@0: michael@0: /* Release the UScriptRun if uscript_setRunText() returns an error */ michael@0: if (U_FAILURE(*pErrorCode)) { michael@0: uprv_free(result); michael@0: result = NULL; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uscript_closeRun(UScriptRun *scriptRun) michael@0: { michael@0: if (scriptRun != NULL) { michael@0: uprv_free(scriptRun); michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uscript_resetRun(UScriptRun *scriptRun) michael@0: { michael@0: if (scriptRun != NULL) { michael@0: scriptRun->scriptStart = 0; michael@0: scriptRun->scriptLimit = 0; michael@0: scriptRun->scriptCode = USCRIPT_INVALID_CODE; michael@0: scriptRun->parenSP = -1; michael@0: scriptRun->pushCount = 0; michael@0: scriptRun->fixupCount = 0; michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uscript_setRunText(UScriptRun *scriptRun, const UChar *src, int32_t length, UErrorCode *pErrorCode) michael@0: { michael@0: if (pErrorCode == NULL || U_FAILURE(*pErrorCode)) { michael@0: return; michael@0: } michael@0: michael@0: if (scriptRun == NULL || length < 0 || ((src == NULL) != (length == 0))) { michael@0: *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: michael@0: scriptRun->textArray = src; michael@0: scriptRun->textLength = length; michael@0: michael@0: uscript_resetRun(scriptRun); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uscript_nextRun(UScriptRun *scriptRun, int32_t *pRunStart, int32_t *pRunLimit, UScriptCode *pRunScript) michael@0: { michael@0: UErrorCode error = U_ZERO_ERROR; michael@0: michael@0: /* if we've fallen off the end of the text, we're done */ michael@0: if (scriptRun == NULL || scriptRun->scriptLimit >= scriptRun->textLength) { michael@0: return FALSE; michael@0: } michael@0: michael@0: SYNC_FIXUP(scriptRun); michael@0: scriptRun->scriptCode = USCRIPT_COMMON; michael@0: michael@0: for (scriptRun->scriptStart = scriptRun->scriptLimit; scriptRun->scriptLimit < scriptRun->textLength; scriptRun->scriptLimit += 1) { michael@0: UChar high = scriptRun->textArray[scriptRun->scriptLimit]; michael@0: UChar32 ch = high; michael@0: UScriptCode sc; michael@0: int32_t pairIndex; michael@0: michael@0: /* michael@0: * if the character is a high surrogate and it's not the last one michael@0: * in the text, see if it's followed by a low surrogate michael@0: */ michael@0: if (high >= 0xD800 && high <= 0xDBFF && scriptRun->scriptLimit < scriptRun->textLength - 1) { michael@0: UChar low = scriptRun->textArray[scriptRun->scriptLimit + 1]; michael@0: michael@0: /* michael@0: * if it is followed by a low surrogate, michael@0: * consume it and form the full character michael@0: */ michael@0: if (low >= 0xDC00 && low <= 0xDFFF) { michael@0: ch = (high - 0xD800) * 0x0400 + low - 0xDC00 + 0x10000; michael@0: scriptRun->scriptLimit += 1; michael@0: } michael@0: } michael@0: michael@0: sc = uscript_getScript(ch, &error); michael@0: pairIndex = getPairIndex(ch); michael@0: michael@0: /* michael@0: * Paired character handling: michael@0: * michael@0: * if it's an open character, push it onto the stack. michael@0: * if it's a close character, find the matching open on the michael@0: * stack, and use that script code. Any non-matching open michael@0: * characters above it on the stack will be poped. michael@0: */ michael@0: if (pairIndex >= 0) { michael@0: if ((pairIndex & 1) == 0) { michael@0: push(scriptRun, pairIndex, scriptRun->scriptCode); michael@0: } else { michael@0: int32_t pi = pairIndex & ~1; michael@0: michael@0: while (STACK_IS_NOT_EMPTY(scriptRun) && TOP(scriptRun).pairIndex != pi) { michael@0: pop(scriptRun); michael@0: } michael@0: michael@0: if (STACK_IS_NOT_EMPTY(scriptRun)) { michael@0: sc = TOP(scriptRun).scriptCode; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (sameScript(scriptRun->scriptCode, sc)) { michael@0: if (scriptRun->scriptCode <= USCRIPT_INHERITED && sc > USCRIPT_INHERITED) { michael@0: scriptRun->scriptCode = sc; michael@0: michael@0: fixup(scriptRun, scriptRun->scriptCode); michael@0: } michael@0: michael@0: /* michael@0: * if this character is a close paired character, michael@0: * pop the matching open character from the stack michael@0: */ michael@0: if (pairIndex >= 0 && (pairIndex & 1) != 0) { michael@0: pop(scriptRun); michael@0: } michael@0: } else { michael@0: /* michael@0: * if the run broke on a surrogate pair, michael@0: * end it before the high surrogate michael@0: */ michael@0: if (ch >= 0x10000) { michael@0: scriptRun->scriptLimit -= 1; michael@0: } michael@0: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: michael@0: if (pRunStart != NULL) { michael@0: *pRunStart = scriptRun->scriptStart; michael@0: } michael@0: michael@0: if (pRunLimit != NULL) { michael@0: *pRunLimit = scriptRun->scriptLimit; michael@0: } michael@0: michael@0: if (pRunScript != NULL) { michael@0: *pRunScript = scriptRun->scriptCode; michael@0: } michael@0: michael@0: return TRUE; michael@0: }