# New Ticket Created by  dakkar 
# Please include the string:  [perl #68292]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=68292 >


here is the first patch that I wrote in Lisbon: splice works.

I already committed the splice.t spectest into the pugs repository.
>From 888786de06a14b7616999a6e84a4f3e79f0941d6 Mon Sep 17 00:00:00 2001
From: dakkar <dak...@sardina.(none)>
Date: Thu, 6 Aug 2009 19:50:44 +0200
Subject: [PATCH] splice now works

---
 src/setting/Array.pm |   19 ++++++++++++++-----
 t/spectest.data      |    1 +
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/setting/Array.pm b/src/setting/Array.pm
index 2a8fb28..0c27786 100644
--- a/src/setting/Array.pm
+++ b/src/setting/Array.pm
@@ -1,12 +1,17 @@
+# "is export" on Array does not work (it's Perl6Array internally)
+
 class Array is also {
-    multi method splice(@array is rw: $offset = 0, $size = @array.elems - $offset, *...@values) is export {
+    multi method splice(@array is rw: $offset is copy = 0, $size? is copy, *...@values) {
         my @spliced;
         my @deleted;
 
-        my $off = ($offset > @array.end) ?? @array.end !! $offset;
-        my $len = $size;
-        @spliced.push(@array.shift) while ($off-- > 0 && @array);
-        @deleted.push(@array.shift) while ($len-- > 0 && @array);
+        $offset += @array.elems if $offset < 0;
+        $offset = @array.elems min floor($offset);
+        $size += @array.end if $size < 0;
+        $size = floor( $size // (@array.elems - $offset) );
+
+        @spliced.push(@array.shift) while (--$offset >= 0 && @array);
+        @deleted.push(@array.shift) while (--$size >= 0 && @array);
         @spliced.push(@values) if @values;
         @spliced.push(@array) if @array;
 
@@ -15,4 +20,8 @@ class Array is also {
     }
 }
 
+multi splice(@array is rw, $offset?, $size?, *...@values) {
+    @array.splice($offset,$size,@values);
+}
+
 # vim: ft=perl6
diff --git a/t/spectest.data b/t/spectest.data
index b202c01..dad03c1 100644
--- a/t/spectest.data
+++ b/t/spectest.data
@@ -379,6 +379,7 @@ S32-array/pop.t
 S32-array/push.t
 S32-array/rotate.t
 S32-array/shift.t
+S32-array/splice.t
 S32-array/unshift.t
 S32-container/zip.t
 S32-hash/exists.t
-- 
1.5.6.3

Attachment: signature.asc
Description: PGP signature

Reply via email to