lib/core: Added `to_s_unsafe` method to `NativeString` which does not cleans a `Nativ...
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 8 Dec 2015 18:17:01 +0000 (13:17 -0500)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 29 Dec 2015 04:49:28 +0000 (23:49 -0500)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/text/abstract_text.nit
lib/core/text/flat.nit
lib/core/text/ropes.nit

index fc8ec5b..8b9b68b 100644 (file)
@@ -852,7 +852,7 @@ abstract class Text
                        l += 1
                end
 
-               return buf.to_s_with_length(l)
+               return buf.to_s_unsafe(l)
        end
 
        # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
@@ -1062,7 +1062,7 @@ abstract class Text
        #
        #       var ns = new NativeString(8)
        #       "Text is String".copy_to_native(ns, 8, 2, 0)
-       #       assert ns.to_s_with_length(8) == "xt is St"
+       #       assert ns.to_s_unsafe(8) == "xt is St"
        #
        fun copy_to_native(dest: NativeString, n, src_offset, dest_offset: Int) do
                var mypos = src_offset
@@ -1541,7 +1541,7 @@ redef class Byte
                var ns = new NativeString(nslen + 1)
                ns[nslen] = 0u8
                native_byte_to_s(ns, nslen + 1)
-               return ns.to_s_with_length(nslen)
+               return ns.to_s_unsafe(nslen)
        end
 end
 
@@ -1702,7 +1702,7 @@ redef class Char
                var ln = u8char_len
                var ns = new NativeString(ln + 1)
                u8char_tos(ns, ln)
-               return ns.to_s_with_length(ln)
+               return ns.to_s_unsafe(ln)
        end
 
        # Returns `self` escaped to UTF-16
@@ -2001,6 +2001,12 @@ redef class NativeString
        # Returns `self` as a String of `length`.
        fun to_s_with_length(length: Int): String is abstract
 
+       # Returns a new instance of `String` with self as `_items`
+       #
+       # /!\: Does not clean the items for compliance with UTF-8,
+       # Use only if you know what you are doing
+       fun to_s_unsafe(len: nullable Int): String is abstract
+
        # Returns `self` as a String with `bytelen` and `length` set
        #
        # SEE: `abstract_text::Text` for more infos on the difference
index bedf03c..6e87257 100644 (file)
@@ -274,7 +274,7 @@ redef class FlatText
                        end
                        pos += 1
                end
-               return nns.to_s_with_length(nlen)
+               return nns.to_s_unsafe(nlen)
        end
 
        redef fun [](index) do return _items.char_at(char_to_byte_index(index))
@@ -1081,6 +1081,11 @@ redef class NativeString
                return new FlatString.full(self, bytelen, 0, unilen)
        end
 
+       redef fun to_s_unsafe(len) do
+               if len == null then len = cstring_length
+               return new FlatString.with_infos(self, len, 0)
+       end
+
        # Returns `self` as a new String.
        redef fun to_s_with_copy: FlatString
        do
index 64a424d..6d42bac 100644 (file)
@@ -425,12 +425,12 @@ class RopeBuffer
                                var rem = count - subpos
                                var nns = new NativeString(rem)
                                ns.copy_to(nns, rem, dumped, 0)
-                               return new RopeBuffer.from(l + nns.to_s_with_length(rem))
+                               return new RopeBuffer.from(l + nns.to_s_unsafe(rem))
                        end
                else
                        var nns = new NativeString(count)
                        ns.copy_to(nns, count, dumped, 0)
-                       return new RopeBuffer.from(nns.to_s_with_length(count))
+                       return new RopeBuffer.from(nns.to_s_unsafe(count))
                end
        end