lib/standard/ropes: API change, Leaves are now abstract for future flexibility.
[nit.git] / lib / standard / ropes.nit
index b1aad2e..54e6050 100644 (file)
@@ -74,11 +74,16 @@ private class Concat
 end
 
 # Leaf of a Rope, contains a FlatString
-private class Leaf
+private abstract class Leaf
        super RopeNode
 
        # Encapsulated FlatString in the leaf node
-       var str: FlatString
+       var str: FlatText
+
+end
+
+private class StringLeaf
+       super Leaf
 
        init(val: FlatString) do
                self.str = val
@@ -104,7 +109,7 @@ abstract class Rope
 
        # Creates a new Rope with `s` as root
        init from(s: String) do
-               if s isa RopeString then root = s.root else root = new Leaf(s.as(FlatString))
+               if s isa RopeString then root = s.root else root = new StringLeaf(s.as(FlatString))
        end
 
        private init from_root(r: RopeNode)
@@ -121,7 +126,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)
@@ -224,6 +229,8 @@ class RopeString
 
        redef fun empty do return once new RopeString.from("")
 
+       redef var chars: SequenceRead[Char] = new RopeStringChars(self)
+
        redef fun reversed
        do
                var ret = empty.as(RopeString)
@@ -263,7 +270,7 @@ class RopeString
        end
 
        # Inserts a String `str` at position `pos`
-       fun insert_at(str: String, pos: Int): RopeString
+       redef fun insert_at(str, pos)
        do
                if str.length == 0 then return self
                if self.length == 0 then return new RopeString.from(str)
@@ -278,18 +285,18 @@ class RopeString
 
                if path.offset == 0 then
                        last_concat.right = path.leaf
-                       if str isa FlatString then last_concat.left = new Leaf(str) else last_concat.left = str.as(RopeString).root
+                       if str isa FlatString then last_concat.left = new StringLeaf(str) else last_concat.left = str.as(RopeString).root
                else if path.offset == path.leaf.length then
-                       if str isa FlatString then last_concat.right = new Leaf(str) else last_concat.right = str.as(RopeString).root
+                       if str isa FlatString then last_concat.right = new StringLeaf(str) else last_concat.right = str.as(RopeString).root
                        last_concat.left = path.leaf
                else
                        var s = path.leaf.str
                        var l_half = s.substring(0, s.length - path.offset)
                        var r_half = s.substring_from(s.length - path.offset)
                        var cct = new Concat
-                       cct.right = new Leaf(r_half)
-                       last_concat.left = new Leaf(l_half)
-                       if str isa FlatString then last_concat.right = new Leaf(str) else last_concat.right = str.as(RopeString).root
+                       cct.right = new StringLeaf(r_half.as(FlatString))
+                       last_concat.left = new StringLeaf(l_half.as(FlatString))
+                       if str isa FlatString then last_concat.right = new StringLeaf(str) else last_concat.right = str.as(RopeString).root
                        cct.left = last_concat
                        last_concat = cct
                end
@@ -310,10 +317,10 @@ class RopeString
        end
 
        # Adds `s` at the beginning of self
-       fun prepend(s: String): String do return insert_at(s, 0)
+       redef fun prepend(s) do return insert_at(s, 0)
 
        # Adds `s` at the end of self
-       fun append(s: String): String
+       redef fun append(s)
        do
                if self.is_empty then return s
                return new RopeString.from_root(append_to_path(root,s))
@@ -325,12 +332,12 @@ class RopeString
                var cct = new Concat
                if node isa Leaf then
                        cct.left = node
-                       if s isa FlatString then cct.right = new Leaf(s) else cct.right = s.as(RopeString).root
+                       if s isa FlatString then cct.right = new StringLeaf(s) else cct.right = s.as(RopeString).root
                else if node isa Concat then
                        var right = node.right
                        if node.left != null then cct.left = node.left.as(not null)
                        if right == null then
-                               if s isa FlatString then cct.right = new Leaf(s) else cct.right = s.as(RopeString).root
+                               if s isa FlatString then cct.right = new StringLeaf(s) else cct.right = s.as(RopeString).root
                        else
                                cct.right = append_to_path(right, s)
                        end
@@ -362,7 +369,7 @@ class RopeString
                var lf = path.leaf
                var offset = path.offset
 
-               if path.leaf.str.length - offset > len then lf = new Leaf(lf.str.substring(offset,len)) else lf = new Leaf(lf.str.substring_from(offset))
+               if path.leaf.str.length - offset > len then lf = new StringLeaf(lf.str.substring(offset,len).as(FlatString)) else lf = new StringLeaf(lf.str.substring_from(offset).as(FlatString))
 
                var nod: RopeNode = lf
 
@@ -381,7 +388,7 @@ class RopeString
                path = ret.node_at(len-1)
 
                offset = path.offset
-               nod = new Leaf(path.leaf.str.substring(0, offset+1))
+               nod = new StringLeaf(path.leaf.str.substring(0, offset+1).as(FlatString))
 
                for i in path.stack.reverse_iterator do
                        if i.left then continue
@@ -398,6 +405,47 @@ class RopeString
        end
 end
 
+redef class FlatString
+
+       redef fun append(s) do return (new RopeString.from(self)) + s
+
+       redef fun prepend(s) do return (new RopeString.from(self)).prepend(s)
+
+       redef fun insert_at(s, pos)
+       do
+               if pos == 0 then return prepend(s)
+               if pos == length then return append(s)
+
+               var l = substring(0,pos)
+               var r = substring_from(pos)
+               var ret: String = new RopeString.from(l)
+               ret += s
+               return ret + r
+       end
+
+end
+
+private class RopeStringChars
+       super SequenceRead[Char]
+
+       var tgt: Rope
+
+       redef fun [](pos)
+       do
+               assert pos < tgt.length
+               var path = tgt.node_at(pos)
+               return path.leaf.str.chars[path.offset]
+       end
+
+       redef fun iterator do return iterator_from(0)
+
+       redef fun iterator_from(pos) do return new RopeCharIterator(tgt, pos)
+
+       redef fun reverse_iterator do return reverse_iterator_from(tgt.length-1)
+
+       redef fun reverse_iterator_from(pos) do return new ReverseRopeCharIterator(tgt, pos)
+end
+
 # Used to iterate on a Rope
 private class IteratorElement