Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/services/i_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# outside a character class (RFC 9485 lists them as ordinary characters, its
# mappings in section 5 and the JSONPath Compliance Test Suite treat them as
# anchors): such a pattern is unusable (Anchored), like an invalid one
# (Invalid); `\^` counts as well. Differences to Ruby and ECMA-262 that
# (Invalid). An escaped `\^` is a literal ^ (dpp-criteria issue #7); `\$` is
# not valid I-Regexp (use `[$]`). Differences to Ruby and ECMA-262 that
# matter: `.` matches any character except LF and CR; only the escapes \( \) \*
# \+ \- \. \? \[ \\ \] \n \r \t \{ \| \} and the category escapes \p{..} and \P{..} exist (no \d,
# \w, \s); there are no lazy quantifiers, no {,m} and no non-capturing or other
Expand Down Expand Up @@ -155,7 +156,6 @@ def escape_outside

c = take
return category(c) if %w[p P].include?(c)
raise Anchored, "\\^" if c == "^"

char(single_escape(c))
end
Expand Down
11 changes: 9 additions & 2 deletions test/services/i_regexp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ class IRegexpTest < ActiveSupport::TestCase
end

test "^ or $ outside a character class makes the pattern unusable (dpp-criteria 4b17bb8)" do
["^ab.*", ".*bc$", "^[Bb]atter", "59040|PCDS$", "(^a)", "a\\^b"].each do |pattern|
assert_match(/\Acontains (\^|\$|\\\^) outside a character class/, IRegexp.problem(pattern), pattern)
["^ab.*", ".*bc$", "^[Bb]atter", "59040|PCDS$", "(^a)"].each do |pattern|
assert_match(/\Acontains (\^|\$) outside a character class/, IRegexp.problem(pattern), pattern)
refute IRegexp.search?(pattern, pattern.delete("^$\\")), pattern
end
end

test "an escaped \\^ is a literal ^, an escaped \\$ is not I-Regexp (dpp-criteria issue #7)" do
assert_nil IRegexp.problem("a\\^b")
assert IRegexp.match?("a\\^b", "a^b")
refute IRegexp.search?("a\\^b", "ab")
assert_match(/not an I-Regexp escape/, IRegexp.problem("a\\$"))
end

test "^ and $ inside a character class are allowed" do
assert_nil IRegexp.problem("[^a]")
assert_nil IRegexp.problem("[a^]")
Expand Down
Loading