lib/bufferized_ropes: Redef to_leaf in concat to work with BufferLeaf
authorLucas Bajolet <r4pass@hotmail.com>
Thu, 24 Jul 2014 14:17:47 +0000 (10:17 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Thu, 24 Jul 2014 14:22:11 +0000 (10:22 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/bufferized_ropes.nit

index 5893e10..5f49b72 100644 (file)
@@ -25,6 +25,28 @@ private class BufferLeaf
 
 end
 
+redef class Concat
+       redef fun to_leaf
+       do
+               if left == null then
+                       if right == null then return new StringLeaf("".as(FlatString))
+                       return right.to_leaf
+               end
+               if right == null then return left.as(not null).to_leaf
+               if left.length + right.length < buf_len then
+                       var b = new FlatBuffer.with_capacity(buf_len)
+                       b.append(left.to_leaf.str)
+                       b.append(right.to_leaf.str)
+                       return new BufferLeaf(b)
+               else
+                       var b = new FlatBuffer.with_capacity(left.length + right.length)
+                       b.append(left.to_leaf.str)
+                       b.append(right.to_leaf.str)
+                       return new StringLeaf(b.lazy_to_s(b.length))
+               end
+       end
+end
+
 redef class FlatText
 
        # Creates a substring, only without any copy overhead for Buffers