Skip to content

Commit

Permalink
Regex: fix invalid #inspect result against %r{\/} (crystal-lang#5841)
Browse files Browse the repository at this point in the history
`p %r{\/}` shows `/\\//`. It is invalid regexp.
  • Loading branch information
makenowjust authored and RX14 committed Mar 20, 2018
1 parent 502ef40 commit 2d93603
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions spec/std/regex_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ describe "Regex" do

it "does inspect with slash" do
%r(/).inspect.should eq("/\\//")
%r(\/).inspect.should eq("/\\//")
end

it "does to_s with slash" do
%r(/).to_s.should eq("(?-imsx:\\/)")
%r(\/).to_s.should eq("(?-imsx:\\/)")
end

it "doesn't crash when PCRE tries to free some memory (#771)" do
Expand Down
10 changes: 8 additions & 2 deletions src/regex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,18 @@ class Regex
end

private def append_source(io)
source.each_char do |char|
if char == '/'
reader = Char::Reader.new(source)
while reader.has_next?
case char = reader.current_char
when '\\'
io << '\\'
io << reader.next_char
when '/'
io << "\\/"
else
io << char
end
reader.next_char
end
end

Expand Down

0 comments on commit 2d93603

Please sign in to comment.