lib/standard/ropes: Added a final forward iterator on the chars.
authorLucas Bajolet <r4pass@hotmail.com>
Thu, 5 Jun 2014 14:30:00 +0000 (10:30 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Thu, 5 Jun 2014 18:17:07 +0000 (14:17 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/ropes.nit

index 9da5d60..f4221b0 100644 (file)
@@ -477,3 +477,47 @@ class SubstringsIterator
 
 end
 
+class RopeCharIterator
+       super IndexedIterator[Char]
+
+       var substrings: IndexedIterator[Text]
+
+       var pos: Int
+
+       var max: Int
+
+       var substr_iter: IndexedIterator[Char]
+
+       init(tgt: Rope, from: Int)
+       do
+               substrings = tgt.substrings_from(from)
+               max = tgt.length - 1
+               if not substrings.is_ok then
+                       pos = tgt.length
+                       return
+               end
+               pos = from
+               substr_iter = substrings.item.chars.iterator
+       end
+
+       redef fun item do return substr_iter.item
+
+       redef fun is_ok do return pos <= max
+
+       redef fun index do return pos
+
+       redef fun next
+       do
+               pos += 1
+               if substr_iter.is_ok then
+                       substr_iter.next
+               end
+               if not substr_iter.is_ok then
+                       substrings.next
+                       if substrings.is_ok then
+                               substr_iter = substrings.item.chars.iterator
+                       end
+               end
+       end
+end
+