From: Lucas Bajolet Date: Wed, 4 Jun 2014 20:51:48 +0000 (-0400) Subject: lib/standard/ropes: Added append method. X-Git-Tag: v0.6.6~43^2~23 X-Git-Url: http://nitlanguage.org?hp=5a072d3ffac81d62921bc2f14140824e98225f15 lib/standard/ropes: Added append method. Signed-off-by: Lucas Bajolet --- diff --git a/lib/standard/ropes.nit b/lib/standard/ropes.nit index 30966ee..b493de9 100644 --- a/lib/standard/ropes.nit +++ b/lib/standard/ropes.nit @@ -151,5 +151,31 @@ class RopeString redef fun to_s do return self + # Adds `s` at the end of self + fun append(s: String): String + do + if self.is_empty then return s + return new RopeString.from_root(append_to_path(root,s)) + end + + # Builds a new path from root to the rightmost node with s appended + private fun append_to_path(node: RopeNode, s: String): RopeNode + do + 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 + 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 + else + cct.right = append_to_path(right, s) + end + end + return cct + end + end