From 1484bf7479677991d928b18a63bd0a3087902cfc Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 25 Mar 2014 15:39:42 -0400 Subject: [PATCH] stdlib/strings: Generalized to_cstring to all Text representations. Signed-off-by: Lucas Bajolet --- lib/standard/string.nit | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/standard/string.nit b/lib/standard/string.nit index e47399b..75aba13 100644 --- a/lib/standard/string.nit +++ b/lib/standard/string.nit @@ -121,6 +121,9 @@ abstract class Text return last_index_of_from(c, length - 1) end + # Return a null terminated char * + fun to_cstring: NativeString do return flatten.to_cstring + # The index of the last occurrence of an element starting from pos (in reverse order). # Example : # assert "/etc/bin/test/test.nit".last_index_of_from('/', length-1) == 13 @@ -712,7 +715,7 @@ class FlatString end # Return a null terminated char * - fun to_cstring: NativeString + redef fun to_cstring: NativeString do if index_from > 0 or index_to != items.cstring_length - 1 then var newItems = calloc_string(length + 1) @@ -1022,6 +1025,14 @@ class FlatBuffer return a.to_s_with_length(length) end + redef fun to_cstring + do + var new_native = calloc_string(length + 1) + new_native[length] = '\0' + items.copy_to(new_native, length, 0, 0) + return new_native + end + # Create a new empty string. init do -- 1.7.9.5