stdlib/strings: Detached the Text block from Collection.
[nit.git] / lib / standard / string.nit
index f33bc0b..de11bac 100644 (file)
@@ -25,18 +25,14 @@ intrude import collection # FIXME should be collection::array
 # String                                                                      #
 ###############################################################################
 
-# Common subclass for String and Buffer
-abstract class AbstractString
-       super AbstractArrayRead[Char]
-
-       readable private var _items: NativeString
+# High-level abstraction for all text representations
+abstract class Text
 
+       # Gets a view on the chars of the Text object
        fun chars: StringCharView is abstract
 
-       # Access a character at `index` in the string.
-       #
-       #     assert "abcd"[2]         == 'c'
-       redef fun [](index) do return _items[index]
+       # Number of characters contained in self.
+       fun length: Int is abstract
 
        # Create a substring.
        #
@@ -48,24 +44,103 @@ abstract class AbstractString
        # A `from` index < 0 will be replaced by 0.
        # Unless a `count` value is > 0 at the same time.
        # In this case, `from += count` and `count -= from`.
-       fun substring(from: Int, count: Int): String
+       fun substring(from: Int, count: Int): String is abstract
+
+       # Is the current Text empty (== "")
+       #       assert "".is_empty
+       #       assert not "foo".is_empty
+       fun is_empty: Bool do return self.length == 0
+
+       # Gets the first char of the Text
+       #
+       # DEPRECATED : Use self.chars.first instead
+       fun first: Char do return self.chars[0]
+
+       # Access a character at `index` in the string.
+       #
+       #     assert "abcd"[2]         == 'c'
+       #
+       # DEPRECATED : Use self.chars.[] instead
+       fun [](index: Int): Char do return self.chars[index]
+
+       # Gets the index of the first occurence of 'c'
+       #
+       # Returns -1 if not found
+       #
+       # DEPRECATED : Use self.chars.index_of instead
+       fun index_of(c: Char): Int
        do
-               assert count >= 0
-               count += from
-               if from < 0 then from = 0
-               if count > length then count = length
-               if from < count then
-                       var r = new Buffer.with_capacity(count - from)
-                       while from < count do
-                               r.chars.push(_items[from])
-                               from += 1
-                       end
-                       return r.to_s
-               else
-                       return ""
+               return index_of_from(c, 0)
+       end
+
+       # Gets the last char of self
+       #
+       # DEPRECATED : Use self.chars.last instead
+       fun last: Char do return self.chars[length-1]
+
+       # Gets the index of the first occurence of ´c´ starting from ´pos´
+       #
+       # Returns -1 if not found
+       #
+       # DEPRECATED : Use self.chars.index_of_from instead
+       fun index_of_from(c: Char, pos: Int): Int
+       do
+               var iter = self.chars.iterator_from(pos)
+               while iter.is_ok do
+                       if iter.item == c then return iter.index
+               end
+               return -1
+       end
+
+       # Gets the last index of char ´c´
+       #
+       # Returns -1 if not found
+       #
+       # DEPRECATED : Use self.chars.last_index_of instead
+       fun last_index_of(c: Char): Int
+       do
+               return last_index_of_from(c, length - 1)
+       end
+
+       # 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
+       #               assert "/etc/bin/test/test.nit".last_index_of_from('/', 12) == 8
+       #
+       # Returns -1 if not found
+       #
+       # DEPRECATED : Use self.chars.last_index_of_from instead
+       fun last_index_of_from(item: Char, pos: Int): Int
+       do
+               var iter = self.chars.reverse_iterator_from(pos)
+               while iter.is_ok do
+                       if iter.item == item then return iter.index
+                       iter.next
                end
+               return -1
+       end
+
+       # Gets an iterator on the chars of self
+       #
+       # DEPRECATED : Use self.chars.iterator instead
+       fun iterator: Iterator[Char]
+       do
+               return self.chars.iterator
        end
 
+       # Is 'c' contained in self ?
+       #
+       # DEPRECATED : Use self.chars.has instead
+       fun has(c: Char): Bool
+       do
+               return self.chars.has(c)
+       end
+
+       # Gets an Array containing the chars of self
+       #
+       # DEPRECATED : Use self.chars.to_a instead
+       fun to_a: Array[Char] do return chars.to_a
+
        # Create a substring from `self` beginning at the `from` position
        #
        #     assert "abcd".substring_from(1)    ==  "bcd"
@@ -85,18 +160,14 @@ abstract class AbstractString
        #     assert "abcd".has_substring("bc",2)            ==  false
        fun has_substring(str: String, pos: Int): Bool
        do
-               var itsindex = str.length - 1
-               var myindex = pos + itsindex
-               var myitems = _items
-               var itsitems = str._items
-               if myindex > length or itsindex > myindex  then return false
-               var its_index_from = str._index_from
-               itsindex += its_index_from
-               while itsindex >= its_index_from do
-                       if myitems[myindex] != itsitems[itsindex] then return false
-                       myindex -= 1
-                       itsindex -= 1
+               var myiter = self.chars.iterator_from(pos)
+               var itsiter = str.iterator
+               while myiter.is_ok and itsiter.is_ok do
+                       if myiter.item != itsiter.item then return false
+                       myiter.next
+                       itsiter.next
                end
+               if itsiter.is_ok then return false
                return true
        end
 
@@ -234,15 +305,6 @@ abstract class AbstractString
                return self.substring(start_pos, end_pos - start_pos + 1)
        end
 
-       redef fun output
-       do
-               var i = 0
-               while i < length do
-                       _items[i].output
-                       i += 1
-               end
-       end
-
        # Mangle a string to be a unique string only made of alphanumeric characters
        fun to_cmangle: String
        do
@@ -309,7 +371,7 @@ abstract class AbstractString
        do
                var b = new Buffer
                for c in escape_to_c do
-                       if chars.has(c) then
+                       if chars.chars.has(c) then
                                b.add('\\')
                        end
                        b.add(c)
@@ -358,6 +420,27 @@ abstract class AbstractString
                end
                return res.to_s
        end
+
+end
+
+# Common subclass for String and Buffer
+abstract class AbstractString
+       super Text
+
+       readable private var _items: NativeString
+
+       redef readable private var _length: Int
+
+       init do end
+
+       redef fun output
+       do
+               var i = 0
+               while i < length do
+                       _items[i].output
+                       i += 1
+               end
+       end
 end
 
 # Abstract class for the SequenceRead compatible
@@ -365,7 +448,7 @@ end
 abstract class StringCharView
        super SequenceRead[Char]
 
-       type SELFTYPE: AbstractString
+       type SELFTYPE: Text
 
        private var target: SELFTYPE
 
@@ -485,36 +568,6 @@ class String
                return new String.with_infos(_items, to - realFrom + 1, realFrom, to)
        end
 
-       redef fun substring_from(from: Int): String
-       do
-               if from > _length then return ""
-               if from < 0 then from = 0
-               return substring(from, _length)
-       end
-
-       redef fun has_substring(str: String, pos: Int): Bool
-       do
-               var itsindex = str._length - 1
-
-               var myindex = pos + itsindex
-               var myitems = _items
-
-               var itsitems = str._items
-
-               if myindex > _length or itsindex > myindex then return false
-
-               var itsindexfrom = str.index_from
-               itsindex += itsindexfrom
-               myindex += index_from
-
-               while itsindex >= itsindexfrom do
-                       if myitems[myindex] != itsitems[itsindex] then return false
-                       myindex -= 1
-                       itsindex -= 1
-               end
-
-               return true
-       end
 
        redef fun to_upper: String
        do
@@ -822,13 +875,15 @@ class Buffer
        super AbstractString
        super Comparable
        super StringCapable
-       super AbstractArray[Char]
 
        redef type OTHER: String
 
        redef var chars: BufferCharView = new FlatBufferCharView(self)
 
-       redef fun []=(index, item)
+       # Modifies the char contained at pos `index`
+       #
+       # DEPRECATED : Use self.chars.[]= instead
+       fun []=(index: Int, item: Char)
        do
                if index == length then
                        add(item)
@@ -838,14 +893,21 @@ class Buffer
                _items[index] = item
        end
 
-       redef fun add(c)
+       # Adds a char `c` at the end of self
+       #
+       # DEPRECATED : Use self.chars.add instead
+       fun add(c: Char)
        do
                if _capacity <= length then enlarge(length + 5)
                _items[length] = c
                _length += 1
        end
 
-       redef fun enlarge(cap)
+       # Clears the buffer
+       fun clear do _length = 0
+
+       # Enlarges the subsequent array containing the chars of self
+       fun enlarge(cap: Int)
        do
                var c = _capacity
                if cap <= c then return
@@ -854,18 +916,7 @@ class Buffer
                _items.copy_to(a, length, 0, 0)
                _items = a
                _capacity = c
-       end
-
-       redef fun append(s)
-       do
-               if s isa String then
-                       var sl = s.length
-                       if _capacity < _length + sl then enlarge(_length + sl)
-                       s.items.copy_to(_items, sl, s._index_from, _length)
-                       _length += sl
-               else
-                       super
-               end
+               items.copy_to(a, length, 0, 0)
        end
 
        redef fun to_s: String
@@ -926,6 +977,15 @@ class Buffer
                _length = 0
        end
 
+       # Adds the content of string `s` at the end of self
+       fun append(s: String)
+       do
+               var sl = s.length
+               if capacity < length + sl then enlarge(length + sl)
+               s.items.copy_to(items, sl, s.index_from, length)
+               _length += sl
+       end
+
        redef fun ==(o)
        do
                if not o isa Buffer then return false
@@ -942,6 +1002,34 @@ class Buffer
        end
 
        readable private var _capacity: Int
+
+       # Copies the content of self in `dest`
+       fun copy(start: Int, len: Int, dest: Buffer, new_start: Int)
+       do
+               var self_chars = self.chars
+               var dest_chars = dest.chars
+               for i in [0..len-1] do
+                       dest_chars[new_start+i] = self_chars[start+i]
+               end
+       end
+
+       redef fun substring(from, count)
+       do
+               assert count >= 0
+               count += from
+               if from < 0 then from = 0
+               if count > length then count = length
+               if from < count then
+                       var r = new Buffer.with_capacity(count - from)
+                       while from < count do
+                               r.chars.push(_items[from])
+                               from += 1
+                       end
+                       return r.to_s
+               else
+                       return ""
+               end
+       end
 end
 
 private class FlatBufferReverseIterator