lib/standard/file: Preparing substrings method, to be used by write method in an...
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 9 Jun 2014 15:58:16 +0000 (11:58 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 9 Jun 2014 15:58:16 +0000 (11:58 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/ropes.nit
lib/standard/string.nit

index 37ac23e..e43508e 100644 (file)
@@ -121,7 +121,7 @@ abstract class Rope
        private fun leaves(from: Int): LeavesIterator do return new LeavesIterator(self, from)
 
        # Iterator on the substrings from 0, in forward order
-       fun substrings: IndexedIterator[Text] do return new SubstringsIterator(self, 0)
+       redef fun substrings do return new SubstringsIterator(self, 0)
 
        # Iterator on the substrings, starting at position `from`, in forward order
        fun substrings_from(from: Int): IndexedIterator[Text] do return new SubstringsIterator(self, from)
index 59a286d..d522714 100644 (file)
@@ -59,6 +59,9 @@ abstract class Text
        # In this case, `from += count` and `count -= from`.
        fun substring(from: Int, count: Int): SELFTYPE is abstract
 
+       # Iterates on the substrings of self if any
+       fun substrings: Iterator[Text] is abstract
+
        # Concatenates `o` to `self`
        #
        #     assert "hello" + "world"  == "helloworld"
@@ -608,7 +611,23 @@ abstract class String
        redef type SELFTYPE: String
 
        redef fun to_s do return self
+end
+
+private class FlatSubstringsIter
+       super Iterator[FlatText]
+
+       var tgt: nullable FlatText
+
+       init(tgt: FlatText) do self.tgt = tgt
 
+       redef fun item do
+               assert is_ok
+               return tgt.as(not null)
+       end
+
+       redef fun is_ok do return tgt != null
+
+       redef fun next do tgt = null
 end
 
 # Immutable strings of characters.
@@ -876,6 +895,8 @@ class FlatString
 
                return hash_cache.as(not null)
        end
+
+       redef fun substrings do return new FlatSubstringsIter(self)
 end
 
 private class FlatStringReverseIterator
@@ -1011,6 +1032,8 @@ class FlatBuffer
 
        private var capacity: Int = 0
 
+       redef fun substrings do return new FlatSubstringsIter(self)
+
        redef fun []=(index, item)
        do
                is_dirty = true