21 JSC::RegExpFlags jscFlags = JSC::NoFlags;
22 if (flags & CompiledData::RegExp::RegExp_Global)
23 jscFlags =
static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagGlobal);
24 if (flags & CompiledData::RegExp::RegExp_IgnoreCase)
25 jscFlags =
static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagIgnoreCase);
26 if (flags & CompiledData::RegExp::RegExp_Multiline)
27 jscFlags =
static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagMultiline);
28 if (flags & CompiledData::RegExp::RegExp_Unicode)
29 jscFlags =
static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagUnicode);
30 if (flags & CompiledData::RegExp::RegExp_Sticky)
31 jscFlags =
static_cast<JSC::RegExpFlags>(jscFlags | JSC::FlagSticky);
45uint RegExp::match(
const QString &string,
int start, uint *matchOffsets)
48 return JSC::Yarr::offsetNoMatch;
53 auto regenerateByteCode = [](Heap::RegExp *regexp) {
54 JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
55 JSC::Yarr::YarrPattern yarrPattern(WTF::String(*regexp->pattern), jscFlags(regexp->flags),
59 Q_ASSERT(error == JSC::Yarr::ErrorCode::NoError);
61 regexp->byteCode = JSC::Yarr::byteCompile(
63 regexp->internalClass->engine->bumperPointerAllocator).release();
66 auto removeJitCode = [](Heap::RegExp *regexp) {
67 delete regexp->jitCode;
68 regexp->jitCode =
nullptr;
69 regexp->jitFailed =
true;
72 auto removeByteCode = [](Heap::RegExp *regexp) {
73 delete regexp->byteCode;
74 regexp->byteCode =
nullptr;
80 const bool longString = string.length() > LongStringJitThreshold;
82 priv->interpreterCallCount += LongStringJitBoost;
84 if (priv->internalClass->engine->canJIT(priv)) {
87 JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
88 JSC::Yarr::YarrPattern yarrPattern(
89 WTF::String(*priv->pattern), jscFlags(priv->flags), error);
90 if (!yarrPattern.m_containsBackreferences) {
91 priv->jitCode =
new JSC::Yarr::YarrCodeBlock;
92 JSC::VM *vm =
static_cast<JSC::VM *>(priv->internalClass->engine);
93 JSC::Yarr::jitCompile(yarrPattern, JSC::Yarr::Char16, vm, *priv->jitCode);
96 if (!priv->hasValidJITCode()) {
98 regenerateByteCode(priv);
100 }
else if (!longString) {
103 ++priv->interpreterCallCount;
108 WTF::String s(string);
111 if (priv->hasValidJITCode()) {
112 static const uint offsetJITFail = std::numeric_limits<
unsigned>::max() - 1;
113 uint ret = JSC::Yarr::offsetNoMatch;
114#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
116 ret = uint(priv->jitCode->execute(s.characters16(), start, s.size(),
117 (
int*)matchOffsets, buffer, 8192).start);
119 ret = uint(priv->jitCode->execute(s.characters16(), start, s.length(),
120 (
int*)matchOffsets).start);
122 if (ret != offsetJITFail)
127 Q_ASSERT(!priv->byteCode);
128 regenerateByteCode(priv);
132 return JSC::Yarr::interpret(byteCode(), s.characters16(), string.size(), start, matchOffsets);
135QString RegExp::getSubstitution(
const QString &matched,
const QString &str,
int position,
const Value *captures,
int nCaptures,
const QString &replacement)
139 int matchedLength = matched.size();
140 Q_ASSERT(position >= 0 && position <= str.size());
141 int tailPos = position + matchedLength;
143 for (
int i = 0; i < replacement.size(); ++i) {
144 QChar ch = replacement.at(i);
145 if (seenDollar >= 0) {
146 if (ch.unicode() ==
'$') {
147 result += QLatin1Char(
'$');
148 }
else if (ch.unicode() ==
'&') {
150 }
else if (ch.unicode() ==
'`') {
151 result += str.left(position);
152 }
else if (ch.unicode() ==
'\'') {
153 result += str.mid(tailPos);
154 }
else if (ch.unicode() >=
'0' && ch.unicode() <=
'9') {
155 int n = ch.unicode() -
'0';
156 if (i + 1 < replacement.size()) {
157 ch = replacement.at(i + 1);
158 if (ch.unicode() >=
'0' && ch.unicode() <=
'9') {
159 n = n*10 + (ch.unicode() -
'0');
163 if (n > 0 && n <= nCaptures) {
164 String *s = captures[n].stringValue();
166 result += s->toQString();
168 for (
int j = seenDollar; j <= i; ++j)
169 result += replacement.at(j);
172 result += QLatin1Char(
'$');
177 if (ch == QLatin1Char(
'$')) {
185 result += QLatin1Char(
'$');
189Heap::RegExp *RegExp::create(
190 ExecutionEngine *engine,
const QString &pattern, CompiledData::RegExp::Flags flags)
192 RegExpCacheKey key(pattern, flags);
194 RegExpCache *cache = engine->regExpCache;
196 cache = engine->regExpCache =
new RegExpCache;
198 QV4::WeakValue &cachedValue = (*cache)[key];
199 if (QV4::RegExp *result = cachedValue.as<RegExp>())
203 Scoped<RegExp> result(scope, engine->memoryManager->alloc<RegExp>(engine, pattern, flags));
205 result->d()->cache = cache;
206 cachedValue.set(engine, result);
211void Heap::RegExp::init(ExecutionEngine *engine,
const QString &pattern, uint flags)
214 this->pattern =
new QString(pattern);
217 JSC::Yarr::ErrorCode error = JSC::Yarr::ErrorCode::NoError;
218 JSC::Yarr::YarrPattern yarrPattern(WTF::String(pattern), jscFlags(flags), error);
219 if (error != JSC::Yarr::ErrorCode::NoError)
221 subPatternCount = yarrPattern.m_numSubpatterns;
223 byteCode = JSC::Yarr::byteCompile(yarrPattern, internalClass->engine->bumperPointerAllocator).release();