Return null
on success and an Error
otherwise.
This method is always called by get_match
and has_match
, but the user
should call it to check for errors.
assert "ab".to_re.compile == null
assert "[ab".to_re.compile.message == "Unmatched [ or [^"
# Compile the regular expression, if needed
#
# Return `null` on success and an `Error` otherwise.
#
# This method is always called by `get_match` and `has_match`, but the user
# should call it to check for errors.
#
# assert "ab".to_re.compile == null
# assert "[ab".to_re.compile.message == "Unmatched [ or [^"
fun compile: nullable Error
do
var cflags = 0
if extended then cflags |= flag_extended
if ignore_case then cflags |= flag_icase
if optimize_has then cflags |= flag_nosub
if newline then cflags |= flag_newline
var native = self.native
var need_compilation = native == null or cflags != cflags_cache or string != string_cache
if need_compilation then
# Initial allocation
if native == null then
native = new NativeRegex.malloc
self.native = native
end
var res = native.regcomp(string.to_cstring, cflags)
# All is good
if res == 0 then
# Update the cache
self.native = native
# We store these to know if we need to recompile or not
self.cflags_cache = cflags
self.string_cache = string
return null
end
var error_cstr = native.regerror(res)
# We leave it to the lib to decide how to allocate the string that we keep
var error_str = error_cstr.to_s
error_cstr.free
return new Error(error_str)
end
return null
end
lib/core/re.nit:197,2--249,4