1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39#ifndef QV4REGEXP_H
40#define QV4REGEXP_H
41
42//
43// W A R N I N G
44// -------------
45//
46// This file is not part of the Qt API. It exists purely as an
47// implementation detail. This header file may change from version to
48// version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include <QString>
54#include <QVector>
55
56#include <wtf/RefPtr.h>
57#include <wtf/FastAllocBase.h>
58#include <wtf/BumpPointerAllocator.h>
59
60#include <limits.h>
61
62#include <yarr/Yarr.h>
63#include <yarr/YarrInterpreter.h>
64#include <yarr/YarrJIT.h>
65
66#include "qv4managed_p.h"
67#include "qv4engine_p.h"
68
69QT_BEGIN_NAMESPACE
70
71namespace QV4 {
72
73struct ExecutionEngine;
74struct RegExpCacheKey;
75
76namespace Heap {
77
78struct RegExp : Base {
79 void init(ExecutionEngine *engine, const QString& pattern, uint flags);
80 void destroy();
81
82 QString *pattern;
83 JSC::Yarr::BytecodePattern *byteCode;
84#if ENABLE(YARR_JIT)
85 JSC::Yarr::YarrCodeBlock *jitCode;
86#endif
87 bool hasValidJITCode() const {
88#if ENABLE(YARR_JIT)
89 return jitCode && !jitCode->failureReason().has_value() && jitCode->has16BitCode();
90#else
91 return false;
92#endif
93 }
94
95 bool ignoreCase() const { return flags & CompiledData::RegExp::RegExp_IgnoreCase; }
96 bool multiLine() const { return flags & CompiledData::RegExp::RegExp_Multiline; }
97 bool global() const { return flags & CompiledData::RegExp::RegExp_Global; }
98 bool unicode() const { return flags & CompiledData::RegExp::RegExp_Unicode; }
99 bool sticky() const { return flags & CompiledData::RegExp::RegExp_Sticky; }
100
101 RegExpCache *cache;
102 int subPatternCount;
103 uint flags;
104 bool valid;
105
106 QString flagsAsString() const;
107 int captureCount() const { return subPatternCount + 1; }
108};
109Q_STATIC_ASSERT(std::is_trivial< RegExp >::value);
110
111}
112
113struct RegExp : public Managed
114{
115 V4_MANAGED(RegExp, Managed)
116 Q_MANAGED_TYPE(RegExp)
117 V4_NEEDS_DESTROY
118 V4_INTERNALCLASS(RegExp)
119
120 QString pattern() const { return *d()->pattern; }
121 JSC::Yarr::BytecodePattern *byteCode() { return d()->byteCode; }
122#if ENABLE(YARR_JIT)
123 JSC::Yarr::YarrCodeBlock *jitCode() const { return d()->jitCode; }
124#endif
125 RegExpCache *cache() const { return d()->cache; }
126 int subPatternCount() const { return d()->subPatternCount; }
127 bool ignoreCase() const { return d()->ignoreCase(); }
128 bool multiLine() const { return d()->multiLine(); }
129 bool global() const { return d()->global(); }
130 bool unicode() const { return d()->unicode(); }
131 bool sticky() const { return d()->sticky(); }
132
133 static Heap::RegExp *create(ExecutionEngine* engine, const QString& pattern, uint flags = CompiledData::RegExp::RegExp_NoFlags);
134
135 bool isValid() const { return d()->valid; }
136
137 uint match(const QString& string, int start, uint *matchOffsets);
138
139 int captureCount() const { return subPatternCount() + 1; }
140
141 static QString getSubstitution(const QString &matched, const QString &str, int position, const Value *captures, int nCaptures, const QString &replacement);
142
143 friend class RegExpCache;
144};
145
146struct RegExpCacheKey
147{
148 RegExpCacheKey(const QString &pattern, uint flags)
149 : pattern(pattern), flags(flags)
150 { }
151 explicit inline RegExpCacheKey(const RegExp::Data *re);
152
153 bool operator==(const RegExpCacheKey &other) const
154 { return pattern == other.pattern && flags == other.flags;; }
155 bool operator!=(const RegExpCacheKey &other) const
156 { return !operator==(other); }
157
158 QString pattern;
159 uint flags;
160};
161
162inline RegExpCacheKey::RegExpCacheKey(const RegExp::Data *re)
163 : pattern(*re->pattern)
164 , flags(re->flags)
165{}
166
167inline uint qHash(const RegExpCacheKey& key, uint seed = 0) Q_DECL_NOTHROW
168{ return qHash(key: key.pattern, seed); }
169
170class RegExpCache : public QHash<RegExpCacheKey, WeakValue>
171{
172public:
173 ~RegExpCache();
174};
175
176
177
178}
179
180QT_END_NAMESPACE
181
182#endif // QV4REGEXP_H
183

source code of qtdeclarative/src/qml/jsruntime/qv4regexp_p.h