js/src/yarr/YarrCanonicalizeUCS2.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 *
michael@0 4 * Copyright (C) 2012 Apple Inc. All rights reserved.
michael@0 5 *
michael@0 6 * Redistribution and use in source and binary forms, with or without
michael@0 7 * modification, are permitted provided that the following conditions
michael@0 8 * are met:
michael@0 9 * 1. Redistributions of source code must retain the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer.
michael@0 11 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 12 * notice, this list of conditions and the following disclaimer in the
michael@0 13 * documentation and/or other materials provided with the distribution.
michael@0 14 *
michael@0 15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
michael@0 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
michael@0 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 26 */
michael@0 27
michael@0 28 #ifndef yarr_YarrCanonicalizeUCS2_h
michael@0 29 #define yarr_YarrCanonicalizeUCS2_h
michael@0 30
michael@0 31 #include <stdint.h>
michael@0 32
michael@0 33 #include "yarr/wtfbridge.h"
michael@0 34
michael@0 35 namespace JSC { namespace Yarr {
michael@0 36
michael@0 37 // This set of data (autogenerated using YarrCanonicalizeUCS2.js into YarrCanonicalizeUCS2.cpp)
michael@0 38 // provides information for each UCS2 code point as to the set of code points that it should
michael@0 39 // match under the ES5.1 case insensitive RegExp matching rules, specified in 15.10.2.8.
michael@0 40 enum UCS2CanonicalizationType {
michael@0 41 CanonicalizeUnique, // No canonically equal values, e.g. 0x0.
michael@0 42 CanonicalizeSet, // Value indicates a set in characterSetInfo.
michael@0 43 CanonicalizeRangeLo, // Value is positive delta to pair, E.g. 0x41 has value 0x20, -> 0x61.
michael@0 44 CanonicalizeRangeHi, // Value is positive delta to pair, E.g. 0x61 has value 0x20, -> 0x41.
michael@0 45 CanonicalizeAlternatingAligned, // Aligned consequtive pair, e.g. 0x1f4,0x1f5.
michael@0 46 CanonicalizeAlternatingUnaligned // Unaligned consequtive pair, e.g. 0x241,0x242.
michael@0 47 };
michael@0 48 struct UCS2CanonicalizationRange { uint16_t begin, end, value, type; };
michael@0 49 extern const size_t UCS2_CANONICALIZATION_RANGES;
michael@0 50 extern const uint16_t* const characterSetInfo[];
michael@0 51 extern const UCS2CanonicalizationRange rangeInfo[];
michael@0 52
michael@0 53 // This table is similar to the full rangeInfo table, however this maps from UCS2 codepoints to
michael@0 54 // the set of Latin1 codepoints that could match.
michael@0 55 enum LatinCanonicalizationType {
michael@0 56 CanonicalizeLatinSelf, // This character is in the Latin1 range, but has no canonical equivalent in the range.
michael@0 57 CanonicalizeLatinMask0x20, // One of a pair of characters, under the mask 0x20.
michael@0 58 CanonicalizeLatinOther, // This character is not in the Latin1 range, but canonicalizes to another that is.
michael@0 59 CanonicalizeLatinInvalid // Cannot match against Latin1 input.
michael@0 60 };
michael@0 61 struct LatinCanonicalizationRange { uint16_t begin, end, value, type; };
michael@0 62 extern const size_t LATIN_CANONICALIZATION_RANGES;
michael@0 63 extern const LatinCanonicalizationRange latinRangeInfo[];
michael@0 64
michael@0 65 // This searches in log2 time over ~364 entries, so should typically result in 8 compares.
michael@0 66 inline const UCS2CanonicalizationRange* rangeInfoFor(UChar ch)
michael@0 67 {
michael@0 68 const UCS2CanonicalizationRange* info = rangeInfo;
michael@0 69 size_t entries = UCS2_CANONICALIZATION_RANGES;
michael@0 70
michael@0 71 while (true) {
michael@0 72 size_t candidate = entries >> 1;
michael@0 73 const UCS2CanonicalizationRange* candidateInfo = info + candidate;
michael@0 74 if (ch < candidateInfo->begin)
michael@0 75 entries = candidate;
michael@0 76 else if (ch <= candidateInfo->end)
michael@0 77 return candidateInfo;
michael@0 78 else {
michael@0 79 info = candidateInfo + 1;
michael@0 80 entries -= (candidate + 1);
michael@0 81 }
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 // Should only be called for characters that have one canonically matching value.
michael@0 86 inline UChar getCanonicalPair(const UCS2CanonicalizationRange* info, UChar ch)
michael@0 87 {
michael@0 88 ASSERT(ch >= info->begin && ch <= info->end);
michael@0 89 switch (info->type) {
michael@0 90 case CanonicalizeRangeLo:
michael@0 91 return ch + info->value;
michael@0 92 case CanonicalizeRangeHi:
michael@0 93 return ch - info->value;
michael@0 94 case CanonicalizeAlternatingAligned:
michael@0 95 return ch ^ 1;
michael@0 96 case CanonicalizeAlternatingUnaligned:
michael@0 97 return ((ch - 1) ^ 1) + 1;
michael@0 98 default:
michael@0 99 ASSERT_NOT_REACHED();
michael@0 100 }
michael@0 101 ASSERT_NOT_REACHED();
michael@0 102 return 0;
michael@0 103 }
michael@0 104
michael@0 105 // Returns true if no other UCS2 codepoint can match this value.
michael@0 106 inline bool isCanonicallyUnique(UChar ch)
michael@0 107 {
michael@0 108 return rangeInfoFor(ch)->type == CanonicalizeUnique;
michael@0 109 }
michael@0 110
michael@0 111 // Returns true if values are equal, under the canonicalization rules.
michael@0 112 inline bool areCanonicallyEquivalent(UChar a, UChar b)
michael@0 113 {
michael@0 114 const UCS2CanonicalizationRange* info = rangeInfoFor(a);
michael@0 115 switch (info->type) {
michael@0 116 case CanonicalizeUnique:
michael@0 117 return a == b;
michael@0 118 case CanonicalizeSet: {
michael@0 119 for (const uint16_t* set = characterSetInfo[info->value]; (a = *set); ++set) {
michael@0 120 if (a == b)
michael@0 121 return true;
michael@0 122 }
michael@0 123 return false;
michael@0 124 }
michael@0 125 case CanonicalizeRangeLo:
michael@0 126 return (a == b) || (a + info->value == b);
michael@0 127 case CanonicalizeRangeHi:
michael@0 128 return (a == b) || (a - info->value == b);
michael@0 129 case CanonicalizeAlternatingAligned:
michael@0 130 return (a | 1) == (b | 1);
michael@0 131 case CanonicalizeAlternatingUnaligned:
michael@0 132 return ((a - 1) | 1) == ((b - 1) | 1);
michael@0 133 }
michael@0 134
michael@0 135 ASSERT_NOT_REACHED();
michael@0 136 return false;
michael@0 137 }
michael@0 138
michael@0 139 } } // JSC::Yarr
michael@0 140
michael@0 141 #endif /* yarr_YarrCanonicalizeUCS2_h */

mercurial