~duangle/scopes-patch

[PATCH] Fixed emplace-append-many for String and Array, used to overwrite initial elements.

Details
Message ID
<CALet5G3dRzct12QMpUvkvNNQ1qXFx+h16KyXUvNamkHt-n9D9Q@mail.gmail.com>
DKIM signature
missing
Download raw message
# HG changeset patch
# User Westerbly Snaydley <westerbly@gmail.com>
# Date 1611216958 18000
#      Thu Jan 21 03:15:58 2021 -0500
# Node ID b29318f530d6a5a5f7aa9e3315e50373eaac4e62
# Parent  dd7c4222a645e58addcd21aa1048a54e3a79c336
Fixed emplace-append-many for String and Array, used to overwrite
initial elements.

diff -r dd7c4222a645 -r b29318f530d6 lib/scopes/Array.sc
--- a/lib/scopes/Array.sc    Sat Jan 16 23:31:36 2021 +0100
+++ b/lib/scopes/Array.sc    Thu Jan 21 03:15:58 2021 -0500
@@ -186,7 +186,8 @@
         let dest = (append-slots self size)
         for idx in (range size)
             let value = (((typeof self) . ElementType) args...)
-            assign value (self._items @ idx)
+            # we already know we have the capacity to do this
+            assign value ((reftoptr dest) @ idx)
         dest

     """"Insert `value` at `index` into the array `self` and return a reference
diff -r dd7c4222a645 -r b29318f530d6 lib/scopes/String.sc
--- a/lib/scopes/String.sc    Sat Jan 16 23:31:36 2021 +0100
+++ b/lib/scopes/String.sc    Thu Jan 21 03:15:58 2021 -0500
@@ -287,7 +287,8 @@
         let dest = (append-slots self size)
         for idx in (range size)
             let value = (((typeof self) . ElementType) args...)
-            assign value (self._items @ idx)
+            # we already know we have the capacity to do this
+            assign value ((reftoptr dest) @ idx)
         dest

     """"Insert `value` at `index` into the string `self` and return a reference
diff -r dd7c4222a645 -r b29318f530d6 testing/test_mutarray.sc
--- a/testing/test_mutarray.sc    Sat Jan 16 23:31:36 2021 +0100
+++ b/testing/test_mutarray.sc    Thu Jan 21 03:15:58 2021 -0500
@@ -231,3 +231,13 @@

 test-remove;
 One.test-refcount-balanced;
+
+do
+    local a : (Array i32)
+    for i in (range 3)
+        'append a 10
+    'emplace-append-many a 2 1
+    for i in (range 3)
+        test ((a @ i) == 10)
+    for i in (range 3 5)
+        test ((a @ i) == 1)
diff -r dd7c4222a645 -r b29318f530d6 testing/test_string.sc
--- a/testing/test_string.sc    Sat Jan 16 23:31:36 2021 +0100
+++ b/testing/test_string.sc    Thu Jan 21 03:15:58 2021 -0500
@@ -93,6 +93,10 @@
     test (q == "inittini")
     'append q (String "init")
     test (q == "inittiniinit")
+
+    local s : String "abcd"
+    'emplace-append-many s 4 101:i8
+    test (s == "abcdeeee")
     ;

 # testing proper globalization
Reply to thread Export thread (mbox)