Skip to content

Commit 1083694

Browse files
committed
JSC::Yarr::Flags in RegularExpression
https://bugs.webkit.org/show_bug.cgi?id=255982 rdar://108836530 Reviewed by Ross Kirsling. Replace the MultilineMode, TextCaseSensitivity, and UnicodeMode enums with JSC::Yarr:Flags and add an assert that prevents using the other flags mainly to ensure folks use them consciously. Also remove the JSC::Yarr prefix when it's not needed. * Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp: (Inspector::ContentSearchUtilities::createRegularExpressionForSearchString): * Source/JavaScriptCore/yarr/RegularExpression.cpp: (JSC::Yarr::RegularExpression::Private::compile): * Source/JavaScriptCore/yarr/RegularExpression.h: * Source/WebCore/html/BaseTextInputType.cpp: (WebCore::BaseTextInputType::patternMismatch const): * Source/WebCore/html/EmailInputType.cpp: (WebCore::isValidEmailAddress): * Source/WebCore/inspector/InspectorStyleSheet.cpp: (WebCore::selectorsFromSource): * Source/WebCore/page/LocalFrame.cpp: (WebCore::createRegExpForLabels): * Source/WebCore/platform/graphics/avfoundation/CDMPrivateMediaSourceAVFObjC.mm: (WebCore::CDMPrivateMediaSourceAVFObjC::parseKeySystem): * Source/WebCore/platform/mac/StringUtilities.mm: (WebCore::stringMatchesWildcardString): * Source/WebKitLegacy/mac/WebView/WebHTMLRepresentation.mm: (regExpForLabels): Canonical link: https://commits.webkit.org/264136@main
1 parent b53214b commit 1083694

10 files changed

Lines changed: 30 additions & 42 deletions

File tree

‎Source/JavaScriptCore/inspector/ContentSearchUtilities.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ RegularExpression createRegularExpressionForSearchString(const String& searchStr
143143
pattern = escapeStringForRegularExpressionSource(searchString);
144144
break;
145145
}
146-
return RegularExpression(pattern, caseSensitive ? TextCaseSensitive : TextCaseInsensitive);
146+
return caseSensitive ? RegularExpression(pattern) : RegularExpression(pattern, { Flags::IgnoreCase });
147147
}
148148

149149
int countRegularExpressionMatches(const RegularExpression& regex, const String& content)

‎Source/JavaScriptCore/yarr/RegularExpression.cpp‎

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "RegularExpression.h"
3030

3131
#include "Yarr.h"
32-
#include "YarrFlags.h"
3332
#include "YarrInterpreter.h"
3433
#include <wtf/Assertions.h>
3534
#include <wtf/BumpPointerAllocator.h>
@@ -38,52 +37,43 @@ namespace JSC { namespace Yarr {
3837

3938
class RegularExpression::Private : public RefCounted<RegularExpression::Private> {
4039
public:
41-
static Ref<Private> create(StringView pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode)
40+
static Ref<Private> create(StringView pattern, OptionSet<Flags> flags)
4241
{
43-
return adoptRef(*new Private(pattern, caseSensitivity, multilineMode, unicodeMode));
42+
return adoptRef(*new Private(pattern, flags));
4443
}
4544

4645
private:
47-
Private(StringView pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode)
48-
: m_regExpByteCode(compile(pattern, caseSensitivity, multilineMode, unicodeMode))
46+
Private(StringView pattern, OptionSet<Flags> flags)
47+
: m_regExpByteCode(compile(pattern, flags))
4948
{
5049
}
5150

52-
std::unique_ptr<JSC::Yarr::BytecodePattern> compile(StringView patternString, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode)
51+
std::unique_ptr<BytecodePattern> compile(StringView patternString, OptionSet<Flags> flags)
5352
{
54-
OptionSet<JSC::Yarr::Flags> flags;
53+
ASSERT(!(flags - OptionSet<Flags> { Flags::IgnoreCase, Flags::Multiline, Flags::Unicode }));
5554

56-
if (caseSensitivity == TextCaseInsensitive)
57-
flags.add(Flags::IgnoreCase);
58-
59-
if (multilineMode == MultilineEnabled)
60-
flags.add(Flags::Multiline);
61-
62-
if (unicodeMode == UnicodeAwareMode)
63-
flags.add(Flags::Unicode);
64-
65-
JSC::Yarr::YarrPattern pattern(patternString, flags, m_constructionErrorCode);
66-
if (JSC::Yarr::hasError(m_constructionErrorCode)) {
67-
LOG_ERROR("RegularExpression: YARR compile failed with '%s'", JSC::Yarr::errorMessage(m_constructionErrorCode).characters());
55+
YarrPattern pattern(patternString, flags, m_constructionErrorCode);
56+
if (hasError(m_constructionErrorCode)) {
57+
LOG_ERROR("RegularExpression: YARR compile failed with '%s'", errorMessage(m_constructionErrorCode).characters());
6858
return nullptr;
6959
}
7060

7161
m_numSubpatterns = pattern.m_numSubpatterns;
7262

73-
return JSC::Yarr::byteCompile(pattern, &m_regexAllocator, m_constructionErrorCode);
63+
return byteCompile(pattern, &m_regexAllocator, m_constructionErrorCode);
7464
}
7565

76-
JSC::Yarr::ErrorCode m_constructionErrorCode { Yarr::ErrorCode::NoError };
66+
ErrorCode m_constructionErrorCode { Yarr::ErrorCode::NoError };
7767
BumpPointerAllocator m_regexAllocator;
7868

7969
public:
8070
int lastMatchLength { -1 };
8171
unsigned m_numSubpatterns;
82-
std::unique_ptr<JSC::Yarr::BytecodePattern> m_regExpByteCode;
72+
std::unique_ptr<BytecodePattern> m_regExpByteCode;
8373
};
8474

85-
RegularExpression::RegularExpression(StringView pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode)
86-
: d(Private::create(pattern, caseSensitivity, multilineMode, unicodeMode))
75+
RegularExpression::RegularExpression(StringView pattern, OptionSet<Flags> flags)
76+
: d(Private::create(pattern, flags))
8777
{
8878
}
8979

@@ -119,18 +109,18 @@ int RegularExpression::match(StringView str, int startFrom, int* matchLength) co
119109

120110
ASSERT(offsetVector);
121111
for (unsigned j = 0, i = 0; i < d->m_numSubpatterns + 1; j += 2, i++)
122-
offsetVector[j] = JSC::Yarr::offsetNoMatch;
112+
offsetVector[j] = offsetNoMatch;
123113

124114
unsigned result;
125115
if (str.length() <= INT_MAX)
126-
result = JSC::Yarr::interpret(d->m_regExpByteCode.get(), str, startFrom, offsetVector);
116+
result = interpret(d->m_regExpByteCode.get(), str, startFrom, offsetVector);
127117
else {
128118
// This code can't handle unsigned offsets. Limit our processing to strings with offsets that
129119
// can be represented as ints.
130-
result = JSC::Yarr::offsetNoMatch;
120+
result = offsetNoMatch;
131121
}
132122

133-
if (result == JSC::Yarr::offsetNoMatch) {
123+
if (result == offsetNoMatch) {
134124
d->lastMatchLength = -1;
135125
return -1;
136126
}

‎Source/JavaScriptCore/yarr/RegularExpression.h‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@
2525

2626
#pragma once
2727

28+
#include "YarrFlags.h"
29+
#include <wtf/OptionSet.h>
2830
#include <wtf/text/WTFString.h>
2931

3032
namespace JSC { namespace Yarr {
3133

32-
enum MultilineMode { MultilineDisabled, MultilineEnabled };
33-
enum TextCaseSensitivity { TextCaseSensitive, TextCaseInsensitive };
34-
enum UnicodeMode { UnicodeUnawareMode, UnicodeAwareMode };
35-
3634
class JS_EXPORT_PRIVATE RegularExpression {
3735
WTF_MAKE_FAST_ALLOCATED;
3836
public:
39-
explicit RegularExpression(StringView, TextCaseSensitivity = TextCaseSensitive, MultilineMode = MultilineDisabled, UnicodeMode = UnicodeUnawareMode);
37+
explicit RegularExpression(StringView, OptionSet<Flags> = { });
4038
~RegularExpression();
4139

4240
RegularExpression(const RegularExpression&);

‎Source/WebCore/html/BaseTextInputType.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ bool BaseTextInputType::patternMismatch(const String& value) const
4040
// FIXME: We should execute RegExp parser first to check validity instead of creating an actual RegularExpression.
4141
// https://bugs.webkit.org/show_bug.cgi?id=183361
4242
const AtomString& rawPattern = element()->attributeWithoutSynchronization(patternAttr);
43-
if (rawPattern.isNull() || value.isEmpty() || !JSC::Yarr::RegularExpression(rawPattern, JSC::Yarr::TextCaseSensitive, JSC::Yarr::MultilineDisabled, JSC::Yarr::UnicodeAwareMode).isValid())
43+
if (rawPattern.isNull() || value.isEmpty() || !JSC::Yarr::RegularExpression(rawPattern, { JSC::Yarr::Flags::Unicode }).isValid())
4444
return false;
4545

4646
String pattern = makeString("^(?:", rawPattern, ")$");
47-
JSC::Yarr::RegularExpression regex(pattern, JSC::Yarr::TextCaseSensitive, JSC::Yarr::MultilineDisabled, JSC::Yarr::UnicodeAwareMode);
47+
JSC::Yarr::RegularExpression regex(pattern, { JSC::Yarr::Flags::Unicode });
4848
auto valuePatternMismatch = [&regex](auto& value) {
4949
int matchLength = 0;
5050
int valueLength = value.length();

‎Source/WebCore/html/EmailInputType.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static bool isValidEmailAddress(const String& address)
4747
if (!addressLength)
4848
return false;
4949

50-
static NeverDestroyed<const JSC::Yarr::RegularExpression> regExp(StringView { emailPattern }, JSC::Yarr::TextCaseInsensitive);
50+
static NeverDestroyed<const JSC::Yarr::RegularExpression> regExp(StringView { emailPattern }, OptionSet<JSC::Yarr::Flags> { JSC::Yarr::Flags::IgnoreCase });
5151

5252
int matchLength;
5353
int matchOffset = regExp.get().match(address, 0, &matchLength);

‎Source/WebCore/inspector/InspectorStyleSheet.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ static Ref<Protocol::CSS::CSSSelector> buildObjectForSelectorHelper(const String
13041304

13051305
static Ref<JSON::ArrayOf<Protocol::CSS::CSSSelector>> selectorsFromSource(const CSSRuleSourceData* sourceData, const String& sheetText, const Vector<const CSSSelector*> selectors)
13061306
{
1307-
static NeverDestroyed<JSC::Yarr::RegularExpression> comment("/\\*[^]*?\\*/"_s, JSC::Yarr::TextCaseSensitive, JSC::Yarr::MultilineEnabled);
1307+
static NeverDestroyed<JSC::Yarr::RegularExpression> comment("/\\*[^]*?\\*/"_s, OptionSet<JSC::Yarr::Flags> { JSC::Yarr::Flags::Multiline });
13081308

13091309
auto result = JSON::ArrayOf<Protocol::CSS::CSSSelector>::create();
13101310
unsigned selectorIndex = 0;

‎Source/WebCore/page/LocalFrame.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static JSC::Yarr::RegularExpression createRegExpForLabels(const Vector<String>&
398398
pattern.append(i ? "|" : "", startsWithWordCharacter ? "\\b" : "", label, endsWithWordCharacter ? "\\b" : "");
399399
}
400400
pattern.append(')');
401-
return JSC::Yarr::RegularExpression(pattern.toString(), JSC::Yarr::TextCaseInsensitive);
401+
return JSC::Yarr::RegularExpression(pattern.toString(), { JSC::Yarr::Flags::IgnoreCase });
402402
}
403403

404404
String LocalFrame::searchForLabelsAboveCell(const JSC::Yarr::RegularExpression& regExp, HTMLTableCellElement* cell, size_t* resultDistanceFromStartOfCell)

‎Source/WebCore/platform/graphics/avfoundation/CDMPrivateMediaSourceAVFObjC.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
auto CDMPrivateMediaSourceAVFObjC::parseKeySystem(const String& keySystem) -> std::optional<KeySystemParameters>
4747
{
48-
static NeverDestroyed<RegularExpression> keySystemRE("^com\\.apple\\.fps\\.[23]_\\d+(?:,\\d+)*$"_s, JSC::Yarr::TextCaseInsensitive);
48+
static NeverDestroyed<RegularExpression> keySystemRE("^com\\.apple\\.fps\\.[23]_\\d+(?:,\\d+)*$"_s, OptionSet<JSC::Yarr::Flags> { JSC::Yarr::Flags::IgnoreCase });
4949

5050
if (keySystemRE.get().match(keySystem) < 0)
5151
return std::nullopt;

‎Source/WebCore/platform/mac/StringUtilities.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static String wildcardRegexPatternString(const String& string)
5656

5757
bool stringMatchesWildcardString(const String& string, const String& wildcardString)
5858
{
59-
return JSC::Yarr::RegularExpression(wildcardRegexPatternString(wildcardString), JSC::Yarr::TextCaseInsensitive).match(string) != -1;
59+
return JSC::Yarr::RegularExpression(wildcardRegexPatternString(wildcardString), { JSC::Yarr::Flags::IgnoreCase }).match(string) != -1;
6060
}
6161

6262
}

‎Source/WebKitLegacy/mac/WebView/WebHTMLRepresentation.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ - (NSArray *)controlsInForm:(DOMElement *)form
385385
pattern.append(i ? "|" : "", startsWithWordCharacter ? "\\b" : "", label, endsWithWordCharacter ? "\\b" : "");
386386
}
387387
pattern.append(')');
388-
result = new RegularExpression(pattern.toString(), JSC::Yarr::TextCaseInsensitive);
388+
result = new RegularExpression(pattern.toString(), { JSC::Yarr::Flags::IgnoreCase });
389389
}
390390

391391
// add regexp to the cache, making sure it is at the front for LRU ordering

0 commit comments

Comments
 (0)