lib/string_exp/utf8_noindex: Added two-way iterators for FlatString
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 18 Aug 2014 15:08:35 +0000 (11:08 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 18 Aug 2014 15:08:35 +0000 (11:08 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/string_experimentations/utf8_noindex.nit
tests/sav/utf_noindex_test.res
tests/utf_noindex_test.nit

index c63e93a..cef60f7 100644 (file)
@@ -171,6 +171,91 @@ extern class UnicodeChar `{ uint32_t* `}
        `}
 end
 
+class FlatStringReviter
+       super IndexedIterator[UnicodeChar]
+
+       # The NativeString to iterate upon
+       private var ns: NativeString
+
+       # The position in the string
+       private var pos: Int
+
+       # The position in the native string
+       private var bytepos: Int
+
+       init(s: FlatString) do from(s, s.length - 1)
+
+       init from(s: FlatString, position: Int)
+       do
+               ns = s.items
+               pos = position
+               bytepos = s.byte_index(position)
+       end
+
+       redef fun next
+       do
+               bytepos -= 1
+               while ns[bytepos].ascii.bin_and(0xC0) == 0x80 do
+                       bytepos -= 1
+               end
+               pos -= 1
+       end
+
+       redef fun index do return pos
+
+       redef fun item do return new UnicodeChar.from_ns(ns, bytepos)
+
+       redef fun is_ok do return pos >= 0
+end
+
+class FlatStringIter
+       super IndexedIterator[UnicodeChar]
+
+       private var ns: NativeString
+
+       private var pos: Int
+
+       private var bytepos: Int
+
+       private var slen: Int
+
+       private var it: UnicodeChar
+
+       private var is_created: Bool
+
+       init(s: FlatString) do from(s, 0)
+
+       init from(s: FlatString, position: Int) do
+               ns = s.items
+               pos = position
+               bytepos = s.byte_index(position)
+               slen = s.length
+       end
+
+       redef fun index do return pos
+
+       redef fun is_ok do return pos < slen
+
+       redef fun item do
+               if not is_created then
+                       it = new UnicodeChar.from_ns(ns, bytepos)
+                       is_created = true
+               end
+               return it
+       end
+
+       redef fun next
+       do
+               if not is_created then
+                       it = new UnicodeChar.from_ns(ns, bytepos)
+               end
+               is_created = false
+               var pace = it.len
+               pos += 1
+               bytepos += pace
+       end
+end
+
 redef class FlatString
 
        redef type OTHER: FlatString
index 5877a29..b5e3e16 100644 (file)
@@ -4,6 +4,10 @@
 すでa語A本日a 𐍆,A ᓂ . ᓀ 界世a𐍃ーЖロaハ
 ー𐍃a世
 世a𐍃ー
+世a𐍃ー
+ー𐍃a世
+すでa語A本日a 𐍆,A ᓂ . ᓀ 界世a𐍃ーЖロaハ
+ハaロЖー𐍃a世界 ᓀ . ᓂ A,𐍆 a日本A語aです
 ハAロЖー𐍃A世界 ᓀ . ᓂ A,𐍆 A日本A語Aです
 ハaロЖー𐍃a世界 ᓀ . ᓂ a,𐍆 a日本a語aです
 aハロЖー𐍃a世界 ᓀ . ᓂ A,𐍆 a日本A語aです
index a731499..5353bdb 100644 (file)
@@ -46,6 +46,34 @@ for i in [0 .. xx.length[ do xx.char_at(i).output
 
 '\n'.output
 
+var it = new FlatStringReviter(x)
+for i in it do
+       i.output
+end
+
+'\n'.output
+
+var it2 = new FlatStringIter(x)
+for i in it2 do
+       i.output
+end
+
+'\n'.output
+
+it = new FlatStringReviter(str)
+for i in it do
+       i.output
+end
+
+'\n'.output
+
+it2 = new FlatStringIter(str)
+for i in it2 do
+       i.output
+end
+
+'\n'.output
+
 assert str * 2 == str + str
 
 assert x * 2 == x + x