1/*
2 * Copyright (C) 2010 University of Szeged
3 * Copyright (C) 2010 Renata Hodovan ([email protected])
4 * All rights reserved.
5 * Copyright (C) 2019 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#include "YarrFlags.h"
32#include <wtf/OptionSet.h>
33#include <wtf/text/StringHash.h>
34
35namespace JSC {
36
37struct RegExpKey {
38 OptionSet<Yarr::Flags> flagsValue;
39 RefPtr<StringImpl> pattern;
40
41 RegExpKey()
42 {
43 }
44
45 RegExpKey(OptionSet<Yarr::Flags> flags)
46 : flagsValue(flags)
47 {
48 }
49
50 RegExpKey(OptionSet<Yarr::Flags> flags, const String& pattern)
51 : flagsValue(flags)
52 , pattern(pattern.impl())
53 {
54 }
55
56 RegExpKey(OptionSet<Yarr::Flags> flags, RefPtr<StringImpl>&& pattern)
57 : flagsValue(flags)
58 , pattern(WTFMove(pattern))
59 {
60 }
61
62 RegExpKey(OptionSet<Yarr::Flags> flags, const RefPtr<StringImpl>& pattern)
63 : flagsValue(flags)
64 , pattern(pattern)
65 {
66 }
67
68 friend inline bool operator==(const RegExpKey& a, const RegExpKey& b);
69
70 struct Hash {
71 static unsigned hash(const RegExpKey& key) { return key.pattern->hash(); }
72 static bool equal(const RegExpKey& a, const RegExpKey& b) { return a == b; }
73 static constexpr bool safeToCompareToEmptyOrDeleted = false;
74 };
75};
76
77inline bool operator==(const RegExpKey& a, const RegExpKey& b)
78{
79 if (a.flagsValue != b.flagsValue)
80 return false;
81 if (!a.pattern)
82 return !b.pattern;
83 if (!b.pattern)
84 return false;
85 return equal(a.pattern.get(), b.pattern.get());
86}
87
88} // namespace JSC
89
90namespace WTF {
91template<typename T> struct DefaultHash;
92
93template<> struct DefaultHash<JSC::RegExpKey> {
94 typedef JSC::RegExpKey::Hash Hash;
95};
96
97template<> struct HashTraits<JSC::RegExpKey> : GenericHashTraits<JSC::RegExpKey> {
98 static constexpr bool emptyValueIsZero = true;
99 static void constructDeletedValue(JSC::RegExpKey& slot) { slot.flagsValue = JSC::Yarr::Flags::DeletedValue; }
100 static bool isDeletedValue(const JSC::RegExpKey& value) { return value.flagsValue == JSC::Yarr::Flags::DeletedValue; }
101};
102} // namespace WTF
103