blob: d01e40ab599ee20d651beb245960a5c91f5d69c5 [file] [log] [blame]
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +00001/*
2 * Copyright (C) 2010 University of Szeged
3 * Copyright (C) 2010 Renata Hodovan (hodovan@inf.u-szeged.hu)
4 * 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 UNIVERSITY OF SZEGED ``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 UNIVERSITY OF SZEGED 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 */
27
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000028#pragma once
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000029
barraclough@apple.com12812932011-03-09 23:04:27 +000030#include <wtf/text/StringHash.h>
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +000031#include <wtf/text/WTFString.h>
barraclough@apple.com12812932011-03-09 23:04:27 +000032
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000033namespace JSC {
34
utatane.tea@gmail.com60928202018-07-07 20:29:31 +000035enum RegExpFlags : int8_t {
barraclough@apple.com12812932011-03-09 23:04:27 +000036 NoFlags = 0,
37 FlagGlobal = 1,
38 FlagIgnoreCase = 2,
39 FlagMultiline = 4,
msaboff@apple.com3f194652016-03-09 20:11:46 +000040 FlagSticky = 8,
41 FlagUnicode = 16,
msaboff@apple.coma021c7d2017-08-24 21:14:43 +000042 FlagDotAll = 32,
43 InvalidFlags = 64,
barraclough@apple.com745949b2011-03-10 02:22:50 +000044 DeletedValueFlags = -1
barraclough@apple.com12812932011-03-09 23:04:27 +000045};
46
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000047struct RegExpKey {
barraclough@apple.com12812932011-03-09 23:04:27 +000048 RegExpFlags flagsValue;
barraclough@apple.comee2085b2010-08-11 00:16:38 +000049 RefPtr<StringImpl> pattern;
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000050
51 RegExpKey()
barraclough@apple.com12812932011-03-09 23:04:27 +000052 : flagsValue(NoFlags)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000053 {
54 }
55
barraclough@apple.com12812932011-03-09 23:04:27 +000056 RegExpKey(RegExpFlags flags)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000057 : flagsValue(flags)
58 {
59 }
60
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +000061 RegExpKey(RegExpFlags flags, const String& pattern)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000062 : flagsValue(flags)
barraclough@apple.comee2085b2010-08-11 00:16:38 +000063 , pattern(pattern.impl())
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000064 {
65 }
66
utatane.tea@gmail.com2551c4b2017-02-15 17:09:22 +000067 RegExpKey(RegExpFlags flags, RefPtr<StringImpl>&& pattern)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000068 : flagsValue(flags)
utatane.tea@gmail.com2551c4b2017-02-15 17:09:22 +000069 , pattern(WTFMove(pattern))
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000070 {
71 }
72
barraclough@apple.com12812932011-03-09 23:04:27 +000073 RegExpKey(RegExpFlags flags, const RefPtr<StringImpl>& pattern)
barraclough@apple.comf1dafcf2010-08-13 01:05:10 +000074 : flagsValue(flags)
75 , pattern(pattern)
76 {
77 }
andersca@apple.com569282a2012-10-19 17:09:42 +000078
79 friend inline bool operator==(const RegExpKey& a, const RegExpKey& b);
80
81 struct Hash {
82 static unsigned hash(const RegExpKey& key) { return key.pattern->hash(); }
83 static bool equal(const RegExpKey& a, const RegExpKey& b) { return a == b; }
84 static const bool safeToCompareToEmptyOrDeleted = false;
85 };
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000086};
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000087
andersca@apple.com569282a2012-10-19 17:09:42 +000088inline bool operator==(const RegExpKey& a, const RegExpKey& b)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000089{
90 if (a.flagsValue != b.flagsValue)
91 return false;
92 if (!a.pattern)
93 return !b.pattern;
94 if (!b.pattern)
95 return false;
96 return equal(a.pattern.get(), b.pattern.get());
97}
98
andersca@apple.com8cf52fe2010-07-16 20:47:46 +000099} // namespace JSC
100
101namespace WTF {
102template<typename T> struct DefaultHash;
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +0000103
104template<> struct DefaultHash<JSC::RegExpKey> {
andersca@apple.com569282a2012-10-19 17:09:42 +0000105 typedef JSC::RegExpKey::Hash Hash;
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +0000106};
107
108template<> struct HashTraits<JSC::RegExpKey> : GenericHashTraits<JSC::RegExpKey> {
commit-queue@webkit.org358724b2012-05-21 15:13:11 +0000109 static const bool emptyValueIsZero = true;
barraclough@apple.com745949b2011-03-10 02:22:50 +0000110 static void constructDeletedValue(JSC::RegExpKey& slot) { slot.flagsValue = JSC::DeletedValueFlags; }
111 static bool isDeletedValue(const JSC::RegExpKey& value) { return value.flagsValue == JSC::DeletedValueFlags; }
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +0000112};
113} // namespace WTF