core :: Pattern :: search_all_in
self
occurrences into s
.assert 'l'.search_all_in("hello world").length == 3
assert 'z'.search_all_in("hello world").length == 0
Note: Is used by String::search_all
.
# Search all `self` occurrences into `s`.
#
# assert 'l'.search_all_in("hello world").length == 3
# assert 'z'.search_all_in("hello world").length == 0
#
# Note: Is used by `String::search_all`.
protected fun search_all_in(s: Text): Array[Match]
do
var res = new Array[Match] # Result
var match = search_in(s, 0)
while match != null do
res.add(match)
match = search_in(s, match.after)
end
return res
end
lib/core/text/string_search.nit:45,2--60,4
# require: not optimize_has
#
# assert "ab".to_re.search_all_in("abbab").join(", ") == "ab, ab"
# assert "b+".to_re.search_all_in("abbabaabbbbbcab").join(", ") == "bb, b, bbbbb, b"
redef fun search_all_in(text)
do
assert not optimize_has
var comp_res = compile
assert comp_res == null else "Regex compilation failed with: {comp_res.message}\n".output
var native = native
assert native != null
# Actually execute
var cstr = text.to_cstring
var subcstr = cstr
var rets = cstr.to_s_unsafe(text.byte_length, copy=false)
var eflags = gather_eflags
var eflags_or_notbol = eflags | flag_notbol
var native_match = self.native_match
var matches = new Array[Match]
var nsub = native.re_nsub
var res = native.regexec(subcstr, nsub + 1, native_match, eflags)
var bytesub = 0
var charsub = 0
while res == 0 do
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 - 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
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 - sub_bfrom + 1)
subs.add(new Match(rets, sub_cstart, sub_len))
end
bytesub = bto + 1
charsub = cstart + len
subcstr = cstr.fast_cstring(bytesub)
res = native.regexec(subcstr, nsub + 1, native_match, eflags_or_notbol)
end
# No more match?
if res.is_nomatch then return matches
# Error, should be out of memory but we cover any possible error anyway
var error_str = get_error(res)
"Regex search failed with: {error_str}\n".output
abort
end
lib/core/re.nit:414,2--476,4