Skip to content

Commit 46e9d81

Browse files
makenowjustMartin Verzilli
authored and
Martin Verzilli
committedJun 16, 2017
MatchData: implement specialized pretty_print
1 parent f026c47 commit 46e9d81

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎spec/std/match_data_spec.cr

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ describe "Regex::MatchData" do
1313
/fox/.match("the fox").to_s.should eq(%(#<Regex::MatchData "fox">))
1414
end
1515

16+
it "does pretty_print" do
17+
/f(o)(x)/.match("the fox").pretty_inspect.should eq(%(#<Regex::MatchData "fox" 1:"o" 2:"x">))
18+
/(?<first>f)(?<second>o(?<third>o(?<fourth>o(?<fifth>o))))/.match("fooooo").pretty_inspect.should eq(%(#<Regex::MatchData
19+
"foooo"
20+
first:"f"
21+
second:"oooo"
22+
third:"ooo"
23+
fourth:"oo"
24+
fifth:"o">))
25+
end
26+
1627
it "does size" do
1728
"Crystal".match(/[p-s]/).not_nil!.size.should eq(1)
1829
"Crystal".match(/r(ys)/).not_nil!.size.should eq(2)

‎src/regex/match_data.cr

+21
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,27 @@ class Regex
303303
io << ">"
304304
end
305305

306+
def pretty_print(pp) : Nil
307+
name_table = @regex.name_table
308+
309+
pp.surround("#<Regex::MatchData", ">", left_break: nil, right_break: nil) do
310+
size.times do |i|
311+
pp.breakable
312+
pp.group do
313+
if i == 0
314+
self[i].pretty_print pp
315+
else
316+
pp.text "#{name_table.fetch(i) { i }}:"
317+
pp.nest do
318+
pp.breakable ""
319+
self[i].pretty_print pp
320+
end
321+
end
322+
end
323+
end
324+
end
325+
end
326+
306327
def dup
307328
self
308329
end

0 commit comments

Comments
 (0)