# Cache of a single `regmatch_t` to prevent many calls to `malloc`
private var native_match: NativeMatchArray is lazy do
native_match_is_init = true
- return new NativeMatchArray.malloc(native.re_nsub+1)
+ return new NativeMatchArray.malloc(native.as(not null).re_nsub+1)
end
private var native_match_is_init = false
var bfrom = native_match.rm_so + bytefrom
var bto = native_match.rm_eo - 1 + bytefrom
var cpos = cstr.byte_to_char_index_cached(bfrom, charfrom, bytefrom)
- var len = cstr.utf8_length(bfrom, bto)
+ var len = cstr.utf8_length(bfrom, bto - bfrom + 1)
var match = new Match(rets, cpos, len)
+ var subs = match.subs
# Add sub expressions
for i in [1 .. nsub] do
- if native_match[i].rm_so < 0 then continue
+ if native_match[i].rm_so < 0 then
+ subs.add null
+ continue
+ end
var sub_bfrom = native_match[i].rm_so + bytefrom
var sub_bto = native_match[i].rm_eo - 1 + bytefrom
var sub_cpos = cstr.byte_to_char_index_cached(sub_bfrom, cpos, bfrom)
- var sub_len = cstr.utf8_length(sub_bfrom, sub_bto)
- match.subs.add(new Match(rets, sub_cpos, sub_len))
+ var sub_len = cstr.utf8_length(sub_bfrom, sub_bto - sub_bfrom + 1)
+ subs.add(new Match(rets, sub_cpos, sub_len))
end
return match
var bfrom = native_match.rm_so + bytesub
var bto = native_match.rm_eo - 1 + bytesub
var cstart = cstr.byte_to_char_index_cached(bfrom, charsub, bytesub)
- var len = cstr.utf8_length(bfrom, bto)
+ var len = cstr.utf8_length(bfrom, bto - bfrom + 1)
var match = new Match(rets, cstart, len)
matches.add match
+ var subs = match.subs
# Add sub expressions
for i in [1 .. nsub] do
- if native_match[i].rm_so < 0 then continue
+ if native_match[i].rm_so < 0 then
+ subs.add null
+ continue
+ end
var sub_bfrom = native_match[i].rm_so + bytesub
var sub_bto = native_match[i].rm_eo - 1 + bytesub
var sub_cstart = cstr.byte_to_char_index_cached(sub_bfrom, cstart, bfrom)
- var sub_len = cstr.utf8_length(sub_bfrom, sub_bto)
- match.subs.add(new Match(rets, sub_cstart, sub_len))
+ var sub_len = cstr.utf8_length(sub_bfrom, sub_bto - sub_bfrom + 1)
+ subs.add(new Match(rets, sub_cstart, sub_len))
end
bytesub = bto + 1
# assert match.subs.length == 1
# assert match.subs.first.to_s == "d eee"
# ~~~
- var subs = new Array[Match] is lazy
+ var subs = new Array[nullable Match] is lazy
# Get the `n`th expression in this match
#
# assert match[0].to_s == "c d eee f"
# assert match[1].to_s == "d eee"
# ~~~
- fun [](n: Int): Match do
+ fun [](n: Int): nullable Match do
if n == 0 then return self
assert n > 0 and n <= subs.length
return subs[n-1]