lib/string_exp/utf8_no_index: Added char_at, does the same as [] but with UnicodeChar.
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 4 Aug 2014 16:54:03 +0000 (12:54 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Thu, 7 Aug 2014 17:00:57 +0000 (13:00 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/string_experimentations/utf8_noindex.nit

index fd544bf..6e4d625 100644 (file)
@@ -197,6 +197,34 @@ redef class FlatString
                return length;
        `}
 
+       private fun byte_index(index: Int): Int
+       do
+               assert index >= 0
+               assert index < length
+               var ns_i = index_from
+               var my_i = 0
+               while my_i != index do
+                       if items[ns_i].ascii.bin_and(0x80) == 0 then
+                               ns_i += 1
+                       else if items[ns_i].ascii.bin_and(0xE0) == 0xC0 then
+                               ns_i += 2
+                       else if items[ns_i].ascii.bin_and(0xF0) == 0xE0 then
+                               ns_i += 3
+                       else if items[ns_i].ascii.bin_and(0xF7) == 0xF0 then
+                               ns_i += 4
+                       else
+                               ns_i += 1
+                       end
+                       my_i += 1
+               end
+               return ns_i
+       end
+
+       fun char_at(pos: Int): UnicodeChar
+       do
+               return new UnicodeChar.from_ns(items, byte_index(pos))
+       end
+
        private init with_bytelen(items: NativeString, index_from: Int, index_to: Int, bytelen: Int)
        do
                self.items = items