blob: 58fa38725a182124616146ba73e4198181b5a20f [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
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000028#ifndef RegExpKey_h
29#define RegExpKey_h
30
barraclough@apple.com12812932011-03-09 23:04:27 +000031#include <wtf/text/StringHash.h>
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +000032#include <wtf/text/WTFString.h>
barraclough@apple.com12812932011-03-09 23:04:27 +000033
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000034namespace JSC {
35
barraclough@apple.com12812932011-03-09 23:04:27 +000036enum RegExpFlags {
37 NoFlags = 0,
38 FlagGlobal = 1,
39 FlagIgnoreCase = 2,
40 FlagMultiline = 4,
barraclough@apple.com745949b2011-03-10 02:22:50 +000041 InvalidFlags = 8,
42 DeletedValueFlags = -1
barraclough@apple.com12812932011-03-09 23:04:27 +000043};
44
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000045struct RegExpKey {
barraclough@apple.com12812932011-03-09 23:04:27 +000046 RegExpFlags flagsValue;
barraclough@apple.comee2085b2010-08-11 00:16:38 +000047 RefPtr<StringImpl> pattern;
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000048
49 RegExpKey()
barraclough@apple.com12812932011-03-09 23:04:27 +000050 : flagsValue(NoFlags)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000051 {
52 }
53
barraclough@apple.com12812932011-03-09 23:04:27 +000054 RegExpKey(RegExpFlags flags)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000055 : flagsValue(flags)
56 {
57 }
58
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +000059 RegExpKey(RegExpFlags flags, const String& pattern)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000060 : flagsValue(flags)
barraclough@apple.comee2085b2010-08-11 00:16:38 +000061 , pattern(pattern.impl())
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000062 {
63 }
64
barraclough@apple.com12812932011-03-09 23:04:27 +000065 RegExpKey(RegExpFlags flags, const PassRefPtr<StringImpl> pattern)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000066 : flagsValue(flags)
67 , pattern(pattern)
68 {
69 }
70
barraclough@apple.com12812932011-03-09 23:04:27 +000071 RegExpKey(RegExpFlags flags, const RefPtr<StringImpl>& pattern)
barraclough@apple.comf1dafcf2010-08-13 01:05:10 +000072 : flagsValue(flags)
73 , pattern(pattern)
74 {
75 }
andersca@apple.com569282a2012-10-19 17:09:42 +000076
77 friend inline bool operator==(const RegExpKey& a, const RegExpKey& b);
78
79 struct Hash {
80 static unsigned hash(const RegExpKey& key) { return key.pattern->hash(); }
81 static bool equal(const RegExpKey& a, const RegExpKey& b) { return a == b; }
82 static const bool safeToCompareToEmptyOrDeleted = false;
83 };
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000084};
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000085
andersca@apple.com569282a2012-10-19 17:09:42 +000086inline bool operator==(const RegExpKey& a, const RegExpKey& b)
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +000087{
88 if (a.flagsValue != b.flagsValue)
89 return false;
90 if (!a.pattern)
91 return !b.pattern;
92 if (!b.pattern)
93 return false;
94 return equal(a.pattern.get(), b.pattern.get());
95}
96
andersca@apple.com8cf52fe2010-07-16 20:47:46 +000097} // namespace JSC
98
99namespace WTF {
100template<typename T> struct DefaultHash;
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +0000101
102template<> struct DefaultHash<JSC::RegExpKey> {
andersca@apple.com569282a2012-10-19 17:09:42 +0000103 typedef JSC::RegExpKey::Hash Hash;
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +0000104};
105
106template<> struct HashTraits<JSC::RegExpKey> : GenericHashTraits<JSC::RegExpKey> {
commit-queue@webkit.org358724b2012-05-21 15:13:11 +0000107 static const bool emptyValueIsZero = true;
barraclough@apple.com745949b2011-03-10 02:22:50 +0000108 static void constructDeletedValue(JSC::RegExpKey& slot) { slot.flagsValue = JSC::DeletedValueFlags; }
109 static bool isDeletedValue(const JSC::RegExpKey& value) { return value.flagsValue == JSC::DeletedValueFlags; }
zherczeg@webkit.org17dc93a2010-06-22 19:16:57 +0000110};
111} // namespace WTF
112
113#endif // RegExpKey_h