From 9c0aa830abd04e11fb37e16151a3be2c930c6990 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 8 Dec 2015 13:17:01 -0500 Subject: [PATCH] lib/core: Added `to_s_unsafe` method to `NativeString` which does not cleans a `NativeString` before returning it as a `String` Signed-off-by: Lucas Bajolet --- lib/core/text/abstract_text.nit | 14 ++++++++++---- lib/core/text/flat.nit | 7 ++++++- lib/core/text/ropes.nit | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/core/text/abstract_text.nit b/lib/core/text/abstract_text.nit index fc8ec5b..8b9b68b 100644 --- a/lib/core/text/abstract_text.nit +++ b/lib/core/text/abstract_text.nit @@ -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 diff --git a/lib/core/text/flat.nit b/lib/core/text/flat.nit index bedf03c..6e87257 100644 --- a/lib/core/text/flat.nit +++ b/lib/core/text/flat.nit @@ -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 diff --git a/lib/core/text/ropes.nit b/lib/core/text/ropes.nit index 64a424d..6d42bac 100644 --- a/lib/core/text/ropes.nit +++ b/lib/core/text/ropes.nit @@ -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 -- 1.7.9.5