mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:27:35 +00:00
LibJS: Hook up the 'v' (unicodeSets) RegExp flag
This commit is contained in:
parent
598dc74a76
commit
f4b26b0cea
8 changed files with 80 additions and 30 deletions
|
@ -16,7 +16,7 @@ namespace JS {
|
|||
|
||||
Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(StringView flags)
|
||||
{
|
||||
bool d = false, g = false, i = false, m = false, s = false, u = false, y = false;
|
||||
bool d = false, g = false, i = false, m = false, s = false, u = false, y = false, v = false;
|
||||
auto options = RegExpObject::default_flags;
|
||||
|
||||
for (auto ch : flags) {
|
||||
|
@ -68,6 +68,12 @@ Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(Str
|
|||
options |= (regex::ECMAScriptFlags)regex::AllFlags::Internal_Stateful;
|
||||
options |= regex::ECMAScriptFlags::Sticky;
|
||||
break;
|
||||
case 'v':
|
||||
if (v)
|
||||
return String::formatted(ErrorType::RegExpObjectRepeatedFlag.message(), ch);
|
||||
v = true;
|
||||
options |= regex::ECMAScriptFlags::UnicodeSets;
|
||||
break;
|
||||
default:
|
||||
return String::formatted(ErrorType::RegExpObjectBadFlag.message(), ch);
|
||||
}
|
||||
|
@ -76,8 +82,11 @@ Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(Str
|
|||
return options;
|
||||
}
|
||||
|
||||
String parse_regex_pattern(StringView pattern, bool unicode)
|
||||
ErrorOr<String, ParseRegexPatternError> parse_regex_pattern(StringView pattern, bool unicode, bool unicode_sets)
|
||||
{
|
||||
if (unicode && unicode_sets)
|
||||
return ParseRegexPatternError { String::formatted(ErrorType::RegExpObjectIncompatibleFlags.message(), 'u', 'v') };
|
||||
|
||||
auto utf16_pattern = AK::utf8_to_utf16(pattern);
|
||||
Utf16View utf16_pattern_view { utf16_pattern };
|
||||
StringBuilder builder;
|
||||
|
@ -85,7 +94,7 @@ String parse_regex_pattern(StringView pattern, bool unicode)
|
|||
// If the Unicode flag is set, append each code point to the pattern. Otherwise, append each
|
||||
// code unit. But unlike the spec, multi-byte code units must be escaped for LibRegex to parse.
|
||||
for (size_t i = 0; i < utf16_pattern_view.length_in_code_units();) {
|
||||
if (unicode) {
|
||||
if (unicode || unicode_sets) {
|
||||
auto code_point = code_point_at(utf16_pattern_view, i);
|
||||
builder.append_code_point(code_point.code_point);
|
||||
i += code_point.code_unit_count;
|
||||
|
@ -104,6 +113,15 @@ String parse_regex_pattern(StringView pattern, bool unicode)
|
|||
return builder.build();
|
||||
}
|
||||
|
||||
ThrowCompletionOr<String> parse_regex_pattern(StringView pattern, VM& vm, GlobalObject& global_object, bool unicode, bool unicode_sets)
|
||||
{
|
||||
auto result = parse_regex_pattern(pattern, unicode, unicode_sets);
|
||||
if (result.is_error())
|
||||
return vm.throw_completion<JS::SyntaxError>(global_object, result.release_error().error);
|
||||
|
||||
return result.release_value();
|
||||
}
|
||||
|
||||
RegExpObject* RegExpObject::create(GlobalObject& global_object)
|
||||
{
|
||||
return global_object.heap().allocate<RegExpObject>(global_object, *global_object.regexp_prototype());
|
||||
|
@ -156,7 +174,8 @@ ThrowCompletionOr<RegExpObject*> RegExpObject::regexp_initialize(GlobalObject& g
|
|||
} else {
|
||||
original_pattern = TRY(pattern.to_string(global_object));
|
||||
bool unicode = f.find('u').has_value();
|
||||
parsed_pattern = parse_regex_pattern(original_pattern, unicode);
|
||||
bool unicode_sets = f.find('v').has_value();
|
||||
parsed_pattern = TRY(parse_regex_pattern(original_pattern, vm, global_object, unicode, unicode_sets));
|
||||
}
|
||||
|
||||
auto parsed_flags_or_error = regex_flags_from_string(f);
|
||||
|
@ -181,7 +200,7 @@ String RegExpObject::escape_regexp_pattern() const
|
|||
{
|
||||
if (m_pattern.is_empty())
|
||||
return "(?:)";
|
||||
// FIXME: Check u flag and escape accordingly
|
||||
// FIXME: Check the 'u' and 'v' flags and escape accordingly
|
||||
return m_pattern.replace("\n"sv, "\\n"sv, ReplaceMode::All).replace("\r"sv, "\\r"sv, ReplaceMode::All).replace(LINE_SEPARATOR_STRING, "\\u2028"sv, ReplaceMode::All).replace(PARAGRAPH_SEPARATOR_STRING, "\\u2029"sv, ReplaceMode::All).replace("/"sv, "\\/"sv, ReplaceMode::All);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue