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