lib/standard/string: SELFTYPE now refers to String instead of its implementations.
[nit.git] / lib / standard / string.nit
index 25996c2..13bf585 100644 (file)
@@ -59,6 +59,9 @@ abstract class Text
        # In this case, `from += count` and `count -= from`.
        fun substring(from: Int, count: Int): SELFTYPE is abstract
 
+       # Iterates on the substrings of self if any
+       fun substrings: Iterator[Text] is abstract
+
        # Concatenates `o` to `self`
        #
        #     assert "hello" + "world"  == "helloworld"
@@ -121,6 +124,7 @@ abstract class Text
                var iter = self.chars.iterator_from(pos)
                while iter.is_ok do
                        if iter.item == c then return iter.index
+                       iter.next
                end
                return -1
        end
@@ -531,11 +535,9 @@ abstract class Text
                if hash_cache == null then
                        # djb2 hash algorithm
                        var h = 5381
-                       var i = length - 1
 
                        for char in self.chars do
-                               h = (h * 32) + h + char.ascii
-                               i -= 1
+                               h = h.lshift(5) + h + char.ascii
                        end
 
                        hash_cache = h
@@ -554,7 +556,7 @@ abstract class FlatText
        # Real items, used as cache for to_cstring is called
        private var real_items: nullable NativeString = null
 
-       redef var length: Int
+       redef var length: Int = 0
 
        init do end
 
@@ -609,7 +611,23 @@ abstract class String
        redef type SELFTYPE: String
 
        redef fun to_s do return self
+end
+
+private class FlatSubstringsIter
+       super Iterator[FlatText]
+
+       var tgt: nullable FlatText
+
+       init(tgt: FlatText) do self.tgt = tgt
+
+       redef fun item do
+               assert is_ok
+               return tgt.as(not null)
+       end
+
+       redef fun is_ok do return tgt != null
 
+       redef fun next do tgt = null
 end
 
 # Immutable strings of characters.
@@ -617,8 +635,6 @@ class FlatString
        super FlatText
        super String
 
-       redef type SELFTYPE: FlatString
-
        # Index in _items of the start of the string
        private var index_from: Int
 
@@ -861,18 +877,15 @@ class FlatString
        redef fun hash
        do
                if hash_cache == null then
-                       # djb2 hash algorythm
+                       # djb2 hash algorithm
                        var h = 5381
-                       var i = length - 1
+                       var i = index_from
 
                        var myitems = items
-                       var strStart = index_from
-
-                       i += strStart
 
-                       while i >= strStart do
-                               h = (h * 32) + h + self.items[i].ascii
-                               i -= 1
+                       while i <= index_to do
+                               h = h.lshift(5) + h + myitems[i].ascii
+                               i += 1
                        end
 
                        hash_cache = h
@@ -880,6 +893,8 @@ class FlatString
 
                return hash_cache.as(not null)
        end
+
+       redef fun substrings do return new FlatSubstringsIter(self)
 end
 
 private class FlatStringReverseIterator
@@ -1013,7 +1028,9 @@ class FlatBuffer
 
        redef var chars: Sequence[Char] = new FlatBufferCharView(self)
 
-       private var capacity: Int
+       private var capacity: Int = 0
+
+       redef fun substrings do return new FlatSubstringsIter(self)
 
        redef fun []=(index, item)
        do
@@ -1043,15 +1060,13 @@ class FlatBuffer
 
        redef fun enlarge(cap)
        do
-               is_dirty = true
                var c = capacity
                if cap <= c then return
                while c <= cap do c = c * 2 + 2
                var a = calloc_string(c+1)
-               items.copy_to(a, length, 0, 0)
+               if length > 0 then items.copy_to(a, length, 0, 0)
                items = a
                capacity = c
-               items.copy_to(a, length, 0, 0)
        end
 
        redef fun to_s: String
@@ -1064,7 +1079,7 @@ class FlatBuffer
                if is_dirty then
                        var new_native = calloc_string(length + 1)
                        new_native[length] = '\0'
-                       items.copy_to(new_native, length, 0, 0)
+                       if length > 0 then items.copy_to(new_native, length, 0, 0)
                        real_items = new_native
                        is_dirty = false
                end
@@ -1072,7 +1087,7 @@ class FlatBuffer
        end
 
        # Create a new empty string.
-       init do with_capacity(5)
+       init do end
 
        init from(s: Text)
        do
@@ -1104,6 +1119,7 @@ class FlatBuffer
 
        redef fun append(s)
        do
+               if s.is_empty then return
                is_dirty = true
                var sl = s.length
                if capacity < length + sl then enlarge(length + sl)
@@ -1176,6 +1192,24 @@ class FlatBuffer
                end
                return new_buf
        end
+
+       redef fun to_upper
+       do
+               var new_buf = new FlatBuffer.with_capacity(self.length)
+               for i in self.chars do
+                       new_buf.add(i.to_upper)
+               end
+               return new_buf
+       end
+
+       redef fun to_lower
+       do
+               var new_buf = new FlatBuffer.with_capacity(self.length)
+               for i in self.chars do
+                       new_buf.add(i.to_lower)
+               end
+               return new_buf
+       end
 end
 
 private class FlatBufferReverseIterator
@@ -1190,7 +1224,7 @@ private class FlatBufferReverseIterator
        init with_pos(tgt: FlatBuffer, pos: Int)
        do
                target = tgt
-               target_items = tgt.items
+               if tgt.length > 0 then target_items = tgt.items
                curr_pos = pos
        end
 
@@ -1262,7 +1296,7 @@ private class FlatBufferIterator
        init with_pos(tgt: FlatBuffer, pos: Int)
        do
                target = tgt
-               target_items = tgt.items
+               if tgt.length > 0 then target_items = tgt.items
                curr_pos = pos
        end
 
@@ -1525,7 +1559,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 ""