tests: fix savs of json related tests
[nit.git] / lib / standard / string.nit
index ce15ffd..57bb8d6 100644 (file)
@@ -19,6 +19,7 @@ intrude import collection # FIXME should be collection::array
 
 `{
 #include <stdio.h>
+#include <string.h>
 `}
 
 ###############################################################################
@@ -32,16 +33,13 @@ abstract class Text
 
        redef type OTHER: Text
 
-       # Type of the view on self (.chars)
-       type SELFVIEW: StringCharView
-
        # Type of self (used for factorization of several methods, ex : substring_from, empty...)
        type SELFTYPE: Text
 
        # Gets a view on the chars of the Text object
        #
        #     assert "hello".chars.to_a == ['h', 'e', 'l', 'l', 'o']
-       fun chars: SELFVIEW is abstract
+       fun chars: SequenceRead[Char] is abstract
 
        # Number of characters contained in self.
        #
@@ -574,7 +572,7 @@ end
 
 # Abstract class for the SequenceRead compatible
 # views on String and Buffer objects
-abstract class StringCharView
+private abstract class StringCharView
        super SequenceRead[Char]
 
        type SELFTYPE: Text
@@ -592,48 +590,12 @@ abstract class StringCharView
 
        redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
 
-       # Gets a new Iterator starting at position `pos`
-       #
-       #     var iter = "abcd".chars.iterator_from(2)
-       #     assert iter.to_a == ['c', 'd']
-       fun iterator_from(pos: Int): IndexedIterator[Char] is abstract
-
-       # Gets an iterator starting at the end and going backwards
-       #
-       #     var reviter = "hello".chars.reverse_iterator
-       #     assert reviter.to_a == ['o', 'l', 'l', 'e', 'h']
-       fun reverse_iterator: IndexedIterator[Char] do return self.reverse_iterator_from(self.length - 1)
-
-       # Gets an iterator on the chars of self starting from `pos`
-       #
-       #     var reviter = "hello".chars.reverse_iterator_from(2)
-       #     assert reviter.to_a == ['l', 'e', 'h']
-       fun reverse_iterator_from(pos: Int): IndexedIterator[Char] is abstract
-
-       redef fun has(c: Char): Bool
-       do
-               for i in self do
-                       if i == c then return true
-               end
-               return false
-       end
-
-       redef fun ==(other)
-       do
-               if other == null then return false
-               if not other isa StringCharView then return false
-               var other_chars = other.iterator
-               for i in self do
-                       if i != other_chars.item then return false
-                       other_chars.next
-               end
-               return true
-       end
+       redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
 end
 
 # View on Buffer objects, extends Sequence
 # for mutation operations
-abstract class BufferCharView
+private abstract class BufferCharView
        super StringCharView
        super Sequence[Char]
 
@@ -663,7 +625,7 @@ class FlatString
        # Indes in _items of the last item of the string
        private var index_to: Int
 
-       redef var chars: SELFVIEW = new FlatStringCharView(self)
+       redef var chars: SequenceRead[Char] = new FlatStringCharView(self)
 
        ################################################
        #       AbstractString specific methods        #
@@ -996,7 +958,6 @@ end
 abstract class Buffer
        super Text
 
-       redef type SELFVIEW: BufferCharView
        redef type SELFTYPE: Buffer
 
        # Specific implementations MUST set this to `true` in order to invalidate caches
@@ -1038,6 +999,9 @@ abstract class Buffer
                return super
        end
 
+       # In Buffers, the internal sequence of character is mutable
+       # Thus, `chars` can be used to modify the buffer.
+       redef fun chars: Sequence[Char] is abstract
 end
 
 # Mutable strings of characters.
@@ -1047,7 +1011,7 @@ class FlatBuffer
 
        redef type SELFTYPE: FlatBuffer
 
-       redef var chars: SELFVIEW = new FlatBufferCharView(self)
+       redef var chars: Sequence[Char] = new FlatBufferCharView(self)
 
        private var capacity: Int
 
@@ -1362,6 +1326,15 @@ redef class Bool
 end
 
 redef class Int
+
+       # Wrapper of strerror C function
+       private fun strerror_ext: NativeString is extern `{
+               return strerror(recv);
+       `}
+
+       # Returns a string describing error number
+       fun strerror: String do return strerror_ext.to_s
+
        # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
        # assume < to_c max const of char
        private fun fill_buffer(s: Buffer, base: Int, signed: Bool)
@@ -1552,7 +1525,7 @@ redef class Collection[E]
        #
        #     assert [1, 2, 3].join(":")         == "1:2:3"
        #     assert [1..3].join(":")            == "1:2:3"
-       fun join(sep: String): String
+       fun join(sep: Text): String
        do
                if is_empty then return ""