lib/core/text: Deferred utf8_length to native for FlatString
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 8 Sep 2015 18:18:46 +0000 (14:18 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 8 Sep 2015 18:18:46 +0000 (14:18 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/text/flat.nit
lib/core/text/native.nit

index 6e82fee..c8b6ecd 100644 (file)
@@ -197,15 +197,7 @@ class FlatString
 
        redef var length is lazy do
                if _bytelen == 0 then return 0
-               var st = _first_byte
-               var its = _items
-               var ln = 0
-               var lst = _last_byte
-               while st <= lst do
-                       st += its.length_of_char_at(st)
-                       ln += 1
-               end
-               return ln
+               return _items.utf8_length(_first_byte, _last_byte)
        end
 
        redef fun reversed
index 11c8d34..5a74cd1 100644 (file)
@@ -173,4 +173,16 @@ extern class NativeString `{ char* `}
                if length_of_char_at(stpos) >= (endpos - stpos + 1) then return pos
                return endpos
        end
+
+       # Number of UTF-8 characters in `self` between positions `from` and `to`
+       fun utf8_length(from, to: Int): Int do
+               var st = from
+               var lst = to
+               var ln = 0
+               while st <= lst do
+                       st += length_of_char_at(st)
+                       ln += 1
+               end
+               return ln
+       end
 end