Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex: fix invalid #inspect result against %r{\/} #5841

Merged
merged 1 commit into from
Mar 20, 2018
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
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