michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * Copyright (C) 2012 Apple Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY michael@0: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef yarr_YarrCanonicalizeUCS2_h michael@0: #define yarr_YarrCanonicalizeUCS2_h michael@0: michael@0: #include michael@0: michael@0: #include "yarr/wtfbridge.h" michael@0: michael@0: namespace JSC { namespace Yarr { michael@0: michael@0: // This set of data (autogenerated using YarrCanonicalizeUCS2.js into YarrCanonicalizeUCS2.cpp) michael@0: // provides information for each UCS2 code point as to the set of code points that it should michael@0: // match under the ES5.1 case insensitive RegExp matching rules, specified in 15.10.2.8. michael@0: enum UCS2CanonicalizationType { michael@0: CanonicalizeUnique, // No canonically equal values, e.g. 0x0. michael@0: CanonicalizeSet, // Value indicates a set in characterSetInfo. michael@0: CanonicalizeRangeLo, // Value is positive delta to pair, E.g. 0x41 has value 0x20, -> 0x61. michael@0: CanonicalizeRangeHi, // Value is positive delta to pair, E.g. 0x61 has value 0x20, -> 0x41. michael@0: CanonicalizeAlternatingAligned, // Aligned consequtive pair, e.g. 0x1f4,0x1f5. michael@0: CanonicalizeAlternatingUnaligned // Unaligned consequtive pair, e.g. 0x241,0x242. michael@0: }; michael@0: struct UCS2CanonicalizationRange { uint16_t begin, end, value, type; }; michael@0: extern const size_t UCS2_CANONICALIZATION_RANGES; michael@0: extern const uint16_t* const characterSetInfo[]; michael@0: extern const UCS2CanonicalizationRange rangeInfo[]; michael@0: michael@0: // This table is similar to the full rangeInfo table, however this maps from UCS2 codepoints to michael@0: // the set of Latin1 codepoints that could match. michael@0: enum LatinCanonicalizationType { michael@0: CanonicalizeLatinSelf, // This character is in the Latin1 range, but has no canonical equivalent in the range. michael@0: CanonicalizeLatinMask0x20, // One of a pair of characters, under the mask 0x20. michael@0: CanonicalizeLatinOther, // This character is not in the Latin1 range, but canonicalizes to another that is. michael@0: CanonicalizeLatinInvalid // Cannot match against Latin1 input. michael@0: }; michael@0: struct LatinCanonicalizationRange { uint16_t begin, end, value, type; }; michael@0: extern const size_t LATIN_CANONICALIZATION_RANGES; michael@0: extern const LatinCanonicalizationRange latinRangeInfo[]; michael@0: michael@0: // This searches in log2 time over ~364 entries, so should typically result in 8 compares. michael@0: inline const UCS2CanonicalizationRange* rangeInfoFor(UChar ch) michael@0: { michael@0: const UCS2CanonicalizationRange* info = rangeInfo; michael@0: size_t entries = UCS2_CANONICALIZATION_RANGES; michael@0: michael@0: while (true) { michael@0: size_t candidate = entries >> 1; michael@0: const UCS2CanonicalizationRange* candidateInfo = info + candidate; michael@0: if (ch < candidateInfo->begin) michael@0: entries = candidate; michael@0: else if (ch <= candidateInfo->end) michael@0: return candidateInfo; michael@0: else { michael@0: info = candidateInfo + 1; michael@0: entries -= (candidate + 1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Should only be called for characters that have one canonically matching value. michael@0: inline UChar getCanonicalPair(const UCS2CanonicalizationRange* info, UChar ch) michael@0: { michael@0: ASSERT(ch >= info->begin && ch <= info->end); michael@0: switch (info->type) { michael@0: case CanonicalizeRangeLo: michael@0: return ch + info->value; michael@0: case CanonicalizeRangeHi: michael@0: return ch - info->value; michael@0: case CanonicalizeAlternatingAligned: michael@0: return ch ^ 1; michael@0: case CanonicalizeAlternatingUnaligned: michael@0: return ((ch - 1) ^ 1) + 1; michael@0: default: michael@0: ASSERT_NOT_REACHED(); michael@0: } michael@0: ASSERT_NOT_REACHED(); michael@0: return 0; michael@0: } michael@0: michael@0: // Returns true if no other UCS2 codepoint can match this value. michael@0: inline bool isCanonicallyUnique(UChar ch) michael@0: { michael@0: return rangeInfoFor(ch)->type == CanonicalizeUnique; michael@0: } michael@0: michael@0: // Returns true if values are equal, under the canonicalization rules. michael@0: inline bool areCanonicallyEquivalent(UChar a, UChar b) michael@0: { michael@0: const UCS2CanonicalizationRange* info = rangeInfoFor(a); michael@0: switch (info->type) { michael@0: case CanonicalizeUnique: michael@0: return a == b; michael@0: case CanonicalizeSet: { michael@0: for (const uint16_t* set = characterSetInfo[info->value]; (a = *set); ++set) { michael@0: if (a == b) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: case CanonicalizeRangeLo: michael@0: return (a == b) || (a + info->value == b); michael@0: case CanonicalizeRangeHi: michael@0: return (a == b) || (a - info->value == b); michael@0: case CanonicalizeAlternatingAligned: michael@0: return (a | 1) == (b | 1); michael@0: case CanonicalizeAlternatingUnaligned: michael@0: return ((a - 1) | 1) == ((b - 1) | 1); michael@0: } michael@0: michael@0: ASSERT_NOT_REACHED(); michael@0: return false; michael@0: } michael@0: michael@0: } } // JSC::Yarr michael@0: michael@0: #endif /* yarr_YarrCanonicalizeUCS2_h */