Skip to content

Commit 93ff1fd

Browse files
author
Ary Borenszweig
committed
Merge branch 'will-regex-nils'
2 parents e4301ba + 48af679 commit 93ff1fd

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

spec/std/regex_spec.cr

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ describe "Regex" do
4949
$~["g2"].should eq("ba")
5050
end
5151

52-
it "capture empty group" do
53-
("foo" =~ /(?<g1>.*)foo/).should eq(0)
52+
it "captures empty group" do
53+
("foo" =~ /(?<g1>z?)foo/).should eq(0)
54+
$~[1].should eq("")
5455
$~["g1"].should eq("")
5556
end
5657

58+
it "raises exception on optional empty group" do
59+
("foo" =~ /(?<g1>z)?foo/).should eq(0)
60+
expect_raises(Exception) { $~[1] }
61+
expect_raises(Exception) { $~["g1"] }
62+
end
63+
5764
it "raises exception when named group doesn't exist" do
5865
("foo" =~ /foo/).should eq(0)
5966
expect_raises(ArgumentError) { $~["group"] }
@@ -73,10 +80,17 @@ describe "Regex" do
7380
end
7481

7582
it "capture empty group" do
76-
("foo" =~ /(?<g1>.*)foo/).should eq(0)
83+
("foo" =~ /(?<g1>z?)foo/).should eq(0)
84+
$~[1]?.should eq("")
7785
$~["g1"]?.should eq("")
7886
end
7987

88+
it "capture optional empty group" do
89+
("foo" =~ /(?<g1>z)?foo/).should eq(0)
90+
$~[1]?.should be_nil
91+
$~["g1"]?.should be_nil
92+
end
93+
8094
it "returns nil exception when named group doesn't exist" do
8195
("foo" =~ /foo/).should eq(0)
8296
$~["group"]?.should be_nil
@@ -101,6 +115,7 @@ describe "Regex" do
101115
/f(?<lettero>o)(?<letterx>x)/.match("the fox").to_s.should eq(%(#<MatchData "fox" lettero:"o" letterx:"x">))
102116
/fox/.match("the fox").to_s.should eq(%(#<MatchData "fox">))
103117
/f(o)(x)/.match("the fox").inspect.should eq(%(#<MatchData "fox" 1:"o" 2:"x">))
118+
/f(o)(x)?/.match("the fort").inspect.should eq(%(#<MatchData "fo" 1:"o" 2:nil>))
104119
/fox/.match("the fox").inspect.should eq(%(#<MatchData "fox">))
105120
end
106121

src/regex/lib_pcre.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ lib LibPCRE
77
fun exec = pcre_exec(code : Pcre, extra : PcreExtra, subject : UInt8*, length : Int32, offset : Int32, options : Int32,
88
ovector : Int32*, ovecsize : Int32) : Int32
99
fun full_info = pcre_fullinfo(code : Pcre, extra : PcreExtra, what : Int32, where : Int32*) : Int32
10-
fun get_named_substring = pcre_get_named_substring(code : Pcre, subject : UInt8*, ovector : Int32*, string_count : Int32, string_name : UInt8*, string_ptr : UInt8**) : Int32
10+
fun get_stringnumber = pcre_get_stringnumber(code : Pcre, string_name : UInt8*) : Int32
1111

1212
INFO_CAPTURECOUNT = 2
1313
INFO_NAMEENTRYSIZE = 7

src/regex/match_data.cr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class MatchData
2929

3030
start = @ovector[n * 2]
3131
finish = @ovector[n * 2 + 1]
32+
return if start < 0
3233
@string.byte_slice(start, finish - start)
3334
end
3435

@@ -39,9 +40,9 @@ class MatchData
3940
end
4041

4142
def []?(group_name : String)
42-
ret = LibPCRE.get_named_substring(@code, @string, @ovector, @length + 1, group_name, out value)
43+
ret = LibPCRE.get_stringnumber(@code, group_name)
4344
return if ret < 0
44-
String.new(value)
45+
self[ret]?
4546
end
4647

4748
def [](group_name : String)
@@ -67,7 +68,7 @@ class MatchData
6768
io << " " if i > 0
6869
io << name_table.fetch(i + 1) { i + 1 }
6970
io << ":"
70-
self[i + 1].inspect(io)
71+
self[i + 1]?.inspect(io)
7172
end
7273
end
7374
io << ">"

src/uri.cr

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ class URI
3939
def self.parse(string : String)
4040
case string
4141
when /\A(?<scheme>.*):\/\/(?<host>[^:\/\?]*)(:(?<port>\d*))?(?<path>\/[^?]*)?(\?(?<qs>.*))?\Z/
42-
scheme = $1
43-
host = $2
44-
port = $4.empty? ? nil : $4.to_i
45-
path = $5.empty? ? nil : $5
46-
query = $7.empty? ? nil : $7
42+
m = $~
43+
scheme = m[1]
44+
host = m[2]
45+
port = m[4]?.try(&.to_i)
46+
path = m[5]?
47+
query = m[7]?
4748
else
4849
if question_index = string.index '?'
4950
path = string[0 ... question_index]

0 commit comments

Comments
 (0)