lib/string: redefine [] for FlatString and FlatBuffer without using chars
authorJean Privat <jean@pryen.org>
Wed, 27 Aug 2014 02:11:56 +0000 (22:11 -0400)
committerJean Privat <jean@pryen.org>
Wed, 27 Aug 2014 02:11:56 +0000 (22:11 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/string.nit

index 5a0c039..e4423d3 100644 (file)
@@ -856,6 +856,15 @@ class FlatString
 
        redef var chars: SequenceRead[Char] = new FlatStringCharView(self)
 
+       redef fun [](index)
+       do
+               # Check that the index (+ index_from) is not larger than indexTo
+               # In other terms, if the index is valid
+               assert index >= 0
+               assert (index + index_from) <= index_to
+               return items[index + index_from]
+       end
+
        ################################################
        #       AbstractString specific methods        #
        ################################################
@@ -1278,6 +1287,13 @@ class FlatBuffer
 
        redef fun substrings do return new FlatSubstringsIter(self)
 
+       redef fun [](index)
+       do
+               assert index >= 0
+               assert index  < length
+               return items[index]
+       end
+
        redef fun []=(index, item)
        do
                is_dirty = true