stdlib/strings: Modified constructor for Buffer, now works with any kind of text.
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 25 Mar 2014 19:31:24 +0000 (15:31 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 25 Mar 2014 19:31:24 +0000 (15:31 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/string.nit

index cbd6c09..30abdd9 100644 (file)
@@ -1023,12 +1023,22 @@ class FlatBuffer
                with_capacity(5)
        end
 
-       init from(s: String)
+       init from(s: Text)
        do
                capacity = s.length + 1
                length = s.length
                items = calloc_string(capacity)
-               s.items.copy_to(items, length, s.index_from, 0)
+               if s isa FlatString then
+                       s.items.copy_to(items, length, s.index_from, 0)
+               else if s isa FlatBuffer then
+                       s.items.copy_to(items, length, 0, 0)
+               else
+                       var curr_pos = 0
+                       for i in s.chars do
+                               items[curr_pos] = i
+                               curr_pos += 1
+                       end
+               end
        end
 
        # Create a new empty string with a given capacity.