intl/icu/source/extra/scrptrun/scrptrun.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial