Merge: Give top-level methods some rules
[nit.git] / lib / standard / ropes.nit
index e43508e..45c1cbd 100644 (file)
@@ -265,7 +265,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)
@@ -289,8 +289,8 @@ class RopeString
                        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)
+                       cct.right = new Leaf(r_half.as(FlatString))
+                       last_concat.left = new Leaf(l_half.as(FlatString))
                        if str isa FlatString then last_concat.right = new Leaf(str) else last_concat.right = str.as(RopeString).root
                        cct.left = last_concat
                        last_concat = cct
@@ -312,10 +312,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))
@@ -364,7 +364,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 Leaf(lf.str.substring(offset,len).as(FlatString)) else lf = new Leaf(lf.str.substring_from(offset).as(FlatString))
 
                var nod: RopeNode = lf
 
@@ -383,7 +383,7 @@ class RopeString
                path = ret.node_at(len-1)
 
                offset = path.offset
-               nod = new Leaf(path.leaf.str.substring(0, offset+1))
+               nod = new Leaf(path.leaf.str.substring(0, offset+1).as(FlatString))
 
                for i in path.stack.reverse_iterator do
                        if i.left then continue
@@ -400,6 +400,26 @@ 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]