Merge: Added `copy_from` service to `NativeString`
authorJean Privat <jean@pryen.org>
Tue, 24 May 2016 23:12:29 +0000 (19:12 -0400)
committerJean Privat <jean@pryen.org>
Tue, 24 May 2016 23:12:29 +0000 (19:12 -0400)
Usability PR, since I got tired of handling indexes and different structures manually, this method abstracts the job of copying the content of a `Text` to a `NativeString` object.

Pull-Request: #2062
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

12 files changed:
lib/core/text/abstract_text.nit
lib/core/text/flat.nit
lib/core/text/ropes.nit
tests/sav/test_copy_to_native.res [new file with mode: 0644]
tests/sav/test_copy_to_native_alt1.res [new file with mode: 0644]
tests/sav/test_copy_to_native_alt2.res [new file with mode: 0644]
tests/sav/test_nativestring_fill_from.res [new file with mode: 0644]
tests/sav/test_nativestring_fill_from_alt1.res [new file with mode: 0644]
tests/sav/test_nativestring_fill_from_alt2.res [new file with mode: 0644]
tests/sav/test_nativestring_fill_from_alt3.res [new file with mode: 0644]
tests/test_copy_to_native.nit [new file with mode: 0644]
tests/test_nativestring_fill_from.nit [new file with mode: 0644]

index c9d1279..d7dfa48 100644 (file)
@@ -2160,6 +2160,11 @@ redef class NativeString
        # SEE: `abstract_text::Text` for more info on the difference
        # between `Text::bytelen` and `Text::length`.
        fun to_s_full(bytelen, unilen: Int): String is abstract
+
+       # Copies the content of `src` to `self`
+       #
+       # NOTE: `self` must be large enough to withold `self.bytelen` bytes
+       fun fill_from(src: Text) do src.copy_to_native(self, src.bytelen, 0, 0)
 end
 
 redef class NativeArray[E]
index 2fa0bcb..9e70321 100644 (file)
@@ -369,6 +369,10 @@ redef class FlatText
                end
                return res
        end
+
+       redef fun copy_to_native(dst, n, src_off, dst_off) do
+               _items.copy_to(dst, n, first_byte + src_off, dst_off)
+       end
 end
 
 # Immutable strings of characters.
index f370e49..0bf97b8 100644 (file)
@@ -221,23 +221,16 @@ private class Concat
        end
 
        redef fun copy_to_native(dest, n, src_offset, dest_offset) do
-               var subs = new RopeSubstrings.from(self, src_offset)
-               var st = src_offset - subs.pos
-               var off = dest_offset
-               while n > 0 do
-                       var it = subs.item
-                       if n > it.length then
-                               var cplen = it.length - st
-                               it._items.copy_to(dest, cplen, st, off)
-                               off += cplen
-                               n -= cplen
-                       else
-                               it._items.copy_to(dest, n, st, off)
-                               n = 0
-                       end
-                       subs.next
-                       st = 0
+               var l = _left
+               if src_offset < l.bytelen then
+                       var lcopy = l.bytelen - src_offset
+                       lcopy = if lcopy > n then n else lcopy
+                       l.copy_to_native(dest, lcopy, src_offset, dest_offset)
+                       dest_offset += lcopy
+                       n -= lcopy
+                       src_offset = 0
                end
+               _right.copy_to_native(dest, n, src_offset, dest_offset)
        end
 
        # Returns a balanced version of `self`
diff --git a/tests/sav/test_copy_to_native.res b/tests/sav/test_copy_to_native.res
new file mode 100644 (file)
index 0000000..764d170
--- /dev/null
@@ -0,0 +1 @@
+gâštr
diff --git a/tests/sav/test_copy_to_native_alt1.res b/tests/sav/test_copy_to_native_alt1.res
new file mode 100644 (file)
index 0000000..785cb5d
--- /dev/null
@@ -0,0 +1 @@
+gâštr%D
diff --git a/tests/sav/test_copy_to_native_alt2.res b/tests/sav/test_copy_to_native_alt2.res
new file mode 100644 (file)
index 0000000..ec88d9a
--- /dev/null
@@ -0,0 +1 @@
+gâštré
diff --git a/tests/sav/test_nativestring_fill_from.res b/tests/sav/test_nativestring_fill_from.res
new file mode 100644 (file)
index 0000000..2c26c70
--- /dev/null
@@ -0,0 +1 @@
+S&éstr
diff --git a/tests/sav/test_nativestring_fill_from_alt1.res b/tests/sav/test_nativestring_fill_from_alt1.res
new file mode 100644 (file)
index 0000000..0667158
--- /dev/null
@@ -0,0 +1 @@
+S&éstrS&éstr
diff --git a/tests/sav/test_nativestring_fill_from_alt2.res b/tests/sav/test_nativestring_fill_from_alt2.res
new file mode 100644 (file)
index 0000000..2c26c70
--- /dev/null
@@ -0,0 +1 @@
+S&éstr
diff --git a/tests/sav/test_nativestring_fill_from_alt3.res b/tests/sav/test_nativestring_fill_from_alt3.res
new file mode 100644 (file)
index 0000000..f28809f
--- /dev/null
@@ -0,0 +1 @@
+&éstr
diff --git a/tests/test_copy_to_native.nit b/tests/test_copy_to_native.nit
new file mode 100644 (file)
index 0000000..380c02a
--- /dev/null
@@ -0,0 +1,28 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import core
+#alt1 intrude import core::text::ropes
+#alt2 intrude import core::text::ropes
+
+var ons = new NativeString(9)
+var base_str = "%Dégâštr"
+
+var str: String = base_str
+#alt1 str = new Concat(base_str, base_str)
+#alt2 str = new Concat(base_str, base_str.substring_from(2))
+
+var copy_len = (str.bytelen - 4).min(9)
+str.copy_to_native(ons, copy_len, 4, 0)
+print ons.to_s_with_length(copy_len)
diff --git a/tests/test_nativestring_fill_from.nit b/tests/test_nativestring_fill_from.nit
new file mode 100644 (file)
index 0000000..1b9e491
--- /dev/null
@@ -0,0 +1,27 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+#alt1 intrude import core::text::ropes
+import core
+
+var src_s = "S&éstr"
+var cpstr: Text = src_s
+#alt1 cpstr = new Concat(src_s, src_s)
+#alt2 cpstr = new FlatBuffer.from(src_s)
+#alt3 cpstr = cpstr.substring(1, 5)
+
+var ns = new NativeString(cpstr.bytelen)
+ns.fill_from(cpstr)
+
+print ns.to_s_with_length(cpstr.bytelen)