lib/bufferized_ropes: Added lazy operations for substring and to_s on FlatBuffer.
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 23 Jun 2014 15:26:25 +0000 (11:26 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Thu, 24 Jul 2014 14:05:05 +0000 (10:05 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/bufferized_ropes.nit

index 0d9353e..c2d6f20 100644 (file)
@@ -25,6 +25,45 @@ private class BufferLeaf
 
 end
 
+redef class FlatText
+
+       # Creates a substring, only without any copy overhead for Buffers
+       # The call to lazy_substring ensures the creation of a FlatString, which is required for Leaves.
+       private fun lazy_substring(from: Int, length: Int): FlatString is abstract
+
+       # Same as substring_from, but without copy of the data for Buffers.
+       private fun lazy_substring_from(from: Int): FlatString is abstract
+end
+
+redef class FlatBuffer
+
+       # Same as to_s, only will not copy self before returning a String.
+       private fun lazy_to_s(len: Int): FlatString
+       do
+               return new FlatString.with_infos(items, len, 0, length - 1)
+       end
+
+       redef fun lazy_substring(from,length)
+       do
+               return new FlatString.with_infos(items, length, from, from + length - 1)
+       end
+
+       redef fun lazy_substring_from(from)
+       do
+               var newlen = length - from
+               return new FlatString.with_infos(items, newlen, from, from + newlen - 1)
+       end
+
+end
+
+redef class FlatString
+
+       redef fun lazy_substring(from, len) do return substring(from,len).as(FlatString)
+
+       redef fun lazy_substring_from(from) do return substring_from(from).as(FlatString)
+
+end
+
 redef class Rope
 
        # Empty Rope