lib/standard/string: FlatStrings built from NativeString now have free to_cstring.
[nit.git] / lib / standard / string.nit
index 6007588..1319b26 100644 (file)
@@ -178,7 +178,7 @@ abstract class Text
        # As with substring, a `from` index < 0 will be replaced by 0
        fun substring_from(from: Int): SELFTYPE
        do
-               if from > self.length then return empty
+               if from >= self.length then return empty
                if from < 0 then from = 0
                return substring(from, length - from)
        end
@@ -247,8 +247,8 @@ abstract class Text
                var i = 0
                var neg = false
 
-               for c in self.chars
-               do
+               for j in [0..length[ do
+                       var c = chars[j]
                        var v = c.to_i
                        if v > base then
                                if neg then
@@ -278,12 +278,10 @@ abstract class Text
        fun is_numeric: Bool
        do
                var has_point_or_comma = false
-               for i in self.chars
-               do
-                       if not i.is_numeric
-                       then
-                               if (i == '.' or i == ',') and not has_point_or_comma
-                               then
+               for i in [0..length[ do
+                       var c = chars[i]
+                       if not c.is_numeric then
+                               if (c == '.' or c == ',') and not has_point_or_comma then
                                        has_point_or_comma = true
                                else
                                        return false
@@ -300,7 +298,8 @@ abstract class Text
        #     assert "0G".is_hex == false
        fun is_hex: Bool
        do
-               for c in self.chars do
+               for i in [0..length[ do
+                       var c = chars[i]
                        if not (c >= 'a' and c <= 'f') and
                           not (c >= 'A' and c <= 'F') and
                           not (c >= '0' and c <= '9') then return false
@@ -316,7 +315,8 @@ abstract class Text
        #     assert "Hello World".is_upper == false
        fun is_upper: Bool
        do
-               for char in self.chars do 
+               for i in [0..length[ do
+                       var char = chars[i]
                        if char.is_lower then return false
                end
                return true
@@ -329,7 +329,8 @@ abstract class Text
        #     assert "Hello World".is_lower == false
        fun is_lower: Bool
        do
-               for char in self.chars do 
+               for i in [0..length[ do
+                       var char = chars[i]
                        if char.is_upper then return false
                end
                return true
@@ -363,7 +364,7 @@ abstract class Text
                        if iter.item.ascii > 32 then break
                        iter.next
                end
-               if iter.index == length then return self.empty
+               if iter.index < 0 then return self.empty
                return self.substring(0, iter.index + 1)
        end
 
@@ -379,7 +380,8 @@ abstract class Text
        do
                var res = new FlatBuffer
                var underscore = false
-               for c in self.chars do
+               for i in [0..length[ do
+                       var c = chars[i]
                        if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
                                res.add(c)
                                underscore = false
@@ -412,7 +414,8 @@ abstract class Text
        fun escape_to_c: String
        do
                var b = new FlatBuffer
-               for c in self.chars do
+               for i in [0..length[ do
+                       var c = chars[i]
                        if c == '\n' then
                                b.append("\\n")
                        else if c == '\0' then
@@ -464,7 +467,8 @@ abstract class Text
        do
                var res = new FlatBuffer.with_capacity(self.length)
                var was_slash = false
-               for c in chars do
+               for i in [0..length[ do
+                       var c = chars[i]
                        if not was_slash then
                                if c == '\\' then
                                        was_slash = true
@@ -498,7 +502,8 @@ abstract class Text
        do
                var buf = new FlatBuffer
 
-               for c in self.chars do
+               for i in [0..length[ do
+                       var c = chars[i]
                        if (c >= '0' and c <= '9') or
                           (c >= 'a' and c <= 'z') or
                           (c >= 'A' and c <= 'Z') or
@@ -636,7 +641,8 @@ abstract class Text
                        # djb2 hash algorithm
                        var h = 5381
 
-                       for char in self.chars do
+                       for i in [0..length[ do
+                               var char = chars[i]
                                h = h.lshift(5) + h + char.ascii
                        end
 
@@ -762,7 +768,8 @@ abstract class String
                var new_str = new FlatBuffer.with_capacity(self.length)
                var is_first_char = true
 
-               for char in self.chars do
+               for i in [0..length[ do
+                       var char = chars[i]
                        if is_first_char then 
                                new_str.add(char.to_lower)
                                is_first_char = false
@@ -800,7 +807,8 @@ abstract class String
                var is_first_char = true
                var follows_us = false
 
-               for char in self.chars do
+               for i in [0..length[ do
+                       var char = chars[i]
                        if is_first_char then
                                new_str.add(char)
                                is_first_char = false
@@ -848,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        #
        ################################################
@@ -954,15 +971,15 @@ class FlatString
 
        redef fun to_cstring: NativeString
        do
-               if real_items != null then return real_items.as(not null)
-               if index_from > 0 or index_to != items.cstring_length - 1 then
+               if real_items != null then
+                       return real_items.as(not null)
+               else
                        var newItems = calloc_string(length + 1)
                        self.items.copy_to(newItems, length, index_from, 0)
                        newItems[length] = '\0'
                        self.real_items = newItems
                        return newItems
                end
-               return items
        end
 
        redef fun ==(other)
@@ -1044,8 +1061,9 @@ class FlatString
                        s.items.copy_to(target_string, its_length, 0, my_length)
                else
                        var curr_pos = my_length
-                       for i in s.chars do
-                               target_string[curr_pos] = i
+                       for i in [0..s.length[ do
+                               var c = s.chars[i]
+                               target_string[curr_pos] = c
                                curr_pos += 1
                        end
                end
@@ -1118,7 +1136,7 @@ private class FlatStringReverseIterator
                curr_pos = pos + tgt.index_from
        end
 
-       redef fun is_ok do return curr_pos >= 0
+       redef fun is_ok do return curr_pos >= target.index_from
 
        redef fun item do return target_items[curr_pos]
 
@@ -1183,6 +1201,14 @@ abstract class Buffer
        # Specific implementations MUST set this to `true` in order to invalidate caches
        protected var is_dirty = true
 
+       # Copy-On-Write flag
+       #
+       # If the `Buffer` was to_s'd, the next in-place altering
+       # operation will cause the current `Buffer` to be re-allocated.
+       #
+       # The flag will then be set at `false`.
+       protected var written = false
+
        # Modifies the char contained at pos `index`
        #
        # DEPRECATED : Use self.chars.[]= instead
@@ -1269,6 +1295,24 @@ class FlatBuffer
 
        redef fun substrings do return new FlatSubstringsIter(self)
 
+       # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
+       #
+       # This happens when an operation modifies the current `Buffer` and
+       # the Copy-On-Write flag `written` is set at true.
+       private fun reset do
+               var nns = new NativeString(capacity)
+               items.copy_to(nns, length, 0, 0)
+               items = nns
+               written = false
+       end
+
+       redef fun [](index)
+       do
+               assert index >= 0
+               assert index  < length
+               return items[index]
+       end
+
        redef fun []=(index, item)
        do
                is_dirty = true
@@ -1276,6 +1320,7 @@ class FlatBuffer
                        add(item)
                        return
                end
+               if written then reset
                assert index >= 0 and index < length
                items[index] = item
        end
@@ -1290,6 +1335,7 @@ class FlatBuffer
 
        redef fun clear do
                is_dirty = true
+               if written then reset
                length = 0
        end
 
@@ -1300,6 +1346,9 @@ class FlatBuffer
                var c = capacity
                if cap <= c then return
                while c <= cap do c = c * 2 + 2
+               # The COW flag can be set at false here, since
+               # it does a copy of the current `Buffer`
+               written = false
                var a = calloc_string(c+1)
                if length > 0 then items.copy_to(a, length, 0, 0)
                items = a
@@ -1308,7 +1357,8 @@ class FlatBuffer
 
        redef fun to_s: String
        do
-               return to_cstring.to_s_with_length(length)
+               written = true
+               return new FlatString.with_infos(items, length, 0, length - 1)
        end
 
        redef fun to_cstring
@@ -1337,8 +1387,9 @@ class FlatBuffer
                        s.items.copy_to(items, length, 0, 0)
                else
                        var curr_pos = 0
-                       for i in s.chars do
-                               items[curr_pos] = i
+                       for i in [0..s.length[ do
+                               var c = s.chars[i]
+                               items[curr_pos] = c
                                curr_pos += 1
                        end
                end
@@ -1366,8 +1417,9 @@ class FlatBuffer
                        s.items.copy_to(items, sl, 0, length)
                else
                        var curr_pos = self.length
-                       for i in s.chars do
-                               items[curr_pos] = i
+                       for i in [0..s.length[ do
+                               var c = s.chars[i]
+                               items[curr_pos] = c
                                curr_pos += 1
                        end
                end
@@ -1404,6 +1456,7 @@ class FlatBuffer
 
        redef fun reverse
        do
+               written = false
                var ns = calloc_string(capacity)
                var si = length - 1
                var ni = 0
@@ -1426,6 +1479,7 @@ class FlatBuffer
 
        redef fun upper
        do
+               if written then reset
                var it = items
                var id = length - 1
                while id >= 0 do
@@ -1436,6 +1490,7 @@ class FlatBuffer
 
        redef fun lower
        do
+               if written then reset
                var it = items
                var id = length - 1
                while id >= 0 do
@@ -1810,18 +1865,51 @@ redef class Collection[E]
 end
 
 redef class Array[E]
+
        # Fast implementation
        redef fun to_s
        do
-               var s = new FlatBuffer
-               var i = 0
                var l = length
+               if l == 0 then return ""
+               if l == 1 then if self[0] == null then return "" else return self[0].to_s
+               var its = _items
+               var na = new NativeArray[String](l)
+               var i = 0
+               var sl = 0
+               var mypos = 0
                while i < l do
-                       var e = self[i]
-                       if e != null then s.append(e.to_s)
+                       var itsi = its[i]
+                       if itsi == null then
+                               i += 1
+                               continue
+                       end
+                       var tmp = itsi.to_s
+                       sl += tmp.length
+                       na[mypos] = tmp
                        i += 1
+                       mypos += 1
                end
-               return s.to_s
+               var ns = new NativeString(sl + 1)
+               ns[sl] = '\0'
+               i = 0
+               var off = 0
+               while i < mypos do
+                       var tmp = na[i]
+                       var tpl = tmp.length
+                       if tmp isa FlatString then
+                               tmp.items.copy_to(ns, tpl, tmp.index_from, off)
+                               off += tpl
+                       else
+                               for j in tmp.substrings do
+                                       var s = j.as(FlatString)
+                                       var slen = s.length
+                                       s.items.copy_to(ns, slen, s.index_from, off)
+                                       off += slen
+                               end
+                       end
+                       i += 1
+               end
+               return ns.to_s_with_length(sl)
        end
 end
 
@@ -1866,7 +1954,8 @@ end
 # Native strings are simple C char *
 extern class NativeString `{ char* `}
        super StringCapable
-
+       # Creates a new NativeString with a capacity of `length`
+       new(length: Int) is intern
        fun [](index: Int): Char is intern
        fun []=(index: Int, item: Char) is intern
        fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
@@ -1889,7 +1978,9 @@ extern class NativeString `{ char* `}
        fun to_s_with_length(length: Int): FlatString
        do
                assert length >= 0
-               return new FlatString.with_infos(self, length, 0, length - 1)
+               var str = new FlatString.with_infos(self, length, 0, length - 1)
+               str.real_items = self
+               return str
        end
 
        fun to_s_with_copy: FlatString
@@ -1897,7 +1988,9 @@ extern class NativeString `{ char* `}
                var length = cstring_length
                var new_self = calloc_string(length + 1)
                copy_to(new_self, length, 0, 0)
-               return new FlatString.with_infos(new_self, length, 0, length - 1)
+               var str = new FlatString.with_infos(new_self, length, 0, length - 1)
+               str.real_items = self
+               return str
        end
 end