lib/re: fix offset of submatches and clarify variables name
[nit.git] / lib / core / re.nit
index 99421f8..33ab76c 100644 (file)
@@ -272,6 +272,9 @@ class Regex
 
        private fun get_error(errcode: Int): String
        do
+               var native = native
+               assert native != null
+
                # Error, should be out of memory but we cover any possible error anyway
                var error_cstr = native.regerror(errcode)
 
@@ -291,6 +294,9 @@ class Regex
                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 eflags = gather_eflags
                var res = native.regexec_match_only(text.to_cstring, eflags)
@@ -320,6 +326,9 @@ class Regex
                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
                text = text.to_s
                var cstr = text.substring_from(from).to_cstring
@@ -346,6 +355,7 @@ class Regex
        #     assert "el+o".to_re.search_in("hello world", 0).from == 1
        #     assert "l+".to_re.search_in("hello world", 3).from == 3
        #     assert "z".to_re.search_in("hello world", 0) == null
+       #     assert "cd(e)".to_re.search_in("abcdef", 2)[1].to_s == "e"
        redef fun search_in(text, from)
        do
                assert not optimize_has
@@ -353,6 +363,9 @@ class Regex
                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
                text = text.to_s
                var sub = text.substring_from(from)
@@ -366,19 +379,19 @@ class Regex
 
                # Found one?
                if res == 0 then
-                       var bso = bstr.byte_to_char_index(native_match.rm_so)
-                       var ln = bstr.byte_to_char_index(native_match.rm_eo - native_match.rm_so - 1)
+                       var first_char = bstr.byte_to_char_index(native_match.rm_so)
+                       var last_char = bstr.byte_to_char_index(native_match.rm_eo - native_match.rm_so - 1)
                        var match = new Match(text,
-                               from + bso,
-                               ln + 1)
+                               from + first_char,
+                               last_char + 1)
 
                        # Add sub expressions
                        for i in [1 .. nsub] do
-                               bso = bstr.byte_to_char_index(native_match[i].rm_so)
-                               ln = bstr.byte_to_char_index(native_match[i].rm_eo - native_match[i].rm_so - 1)
+                               first_char = bstr.byte_to_char_index(native_match[i].rm_so)
+                               last_char = bstr.byte_to_char_index(native_match[i].rm_eo - native_match[i].rm_so - 1)
                                match.subs.add new Match( text,
-                                       bso ,
-                                       ln + 1)
+                                       from + first_char,
+                                       last_char + 1)
                        end
 
                        return match
@@ -404,6 +417,9 @@ class Regex
                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
                text = text.to_s
                var cstr = text.to_cstring