stdlib/strings: Added reversed service on Text and subclasses.
[nit.git] / lib / standard / string.nit
index 30abdd9..11dece3 100644 (file)
@@ -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
@@ -174,6 +177,9 @@ abstract class Text
                return substring(from, length - from)
        end
 
+       # Returns a reversed version of self
+       fun reversed: SELFTYPE is abstract
+
        # Does self have a substring `str` starting from position `pos`?
        #
        #     assert "abcd".has_substring("bc",1)            ==  true
@@ -453,6 +459,9 @@ abstract class Text
                return self.chars < o.chars
        end
 
+       # Flat representation of self
+       fun flatten: FlatText is abstract
+
 end
 
 # All kinds of array-based text representations.
@@ -473,6 +482,8 @@ abstract class FlatText
                        i += 1
                end
        end
+
+       redef fun flatten do return self
 end
 
 # Abstract class for the SequenceRead compatible
@@ -621,6 +632,19 @@ class FlatString
                return items[index + index_from]
        end
 
+       redef fun reversed
+       do
+               var native = calloc_string(self.length + 1)
+               var reviter = chars.reverse_iterator
+               var pos = 0
+               while reviter.is_ok do
+                       native[pos] = reviter.item
+                       pos += 1
+                       reviter.next
+               end
+               return native.to_s_with_length(self.length)
+       end
+
        redef fun substring(from, count)
        do
                assert count >= 0
@@ -707,7 +731,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)
@@ -1017,6 +1041,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
@@ -1097,6 +1129,17 @@ class FlatBuffer
                end
        end
 
+       redef fun reversed
+       do
+               var new_buf = new FlatBuffer.with_capacity(self.length)
+               var reviter = self.chars.reverse_iterator
+               while reviter.is_ok do
+                       new_buf.add(reviter.item)
+                       reviter.next
+               end
+               return new_buf
+       end
+
        redef fun +(other)
        do
                var new_buf = new FlatBuffer.with_capacity(self.length + other.length)