From 8f342db2cdc98444290fa7d022135878125fad2a Mon Sep 17 00:00:00 2001 From: Coke Date: Wed, 31 Mar 2010 01:18:42 -0400 Subject: [PATCH] Add [lreplace], based on partcl's PIR version but in NQP. All tests pass, enable the test file. --- build/Makefile.in | 3 ++- src/Partcl/commands/main.pm | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/build/Makefile.in b/build/Makefile.in index a7e6d53..4789c42 100644 --- a/build/Makefile.in +++ b/build/Makefile.in @@ -75,6 +75,7 @@ TEST_FILES = \ t/cmd_llength.t \ t/cmd_lrange.t \ t/cmd_lrepeat.t \ + t/cmd_lreplace.t \ t/cmd_lreverse.t \ t/cmd_lset.t \ t/cmd_split.t \ @@ -90,7 +91,7 @@ TEST_FILES = \ # t/cmd_catch.t t/cmd_continue.t t/cmd_eval.t t/cmd_expr.t t/cmd_file.t # t/cmd_fileevent.t t/cmd_foreach.t t/cmd_format.t t/cmd_gets.t # t/cmd_global.t t/cmd_if.t t/cmd_info.t t/cmd_lappend.t t/cmd_linsert.t -# t/cmd_lreplace.t t/cmd_lsort.t t/cmd_namespace.t +# t/cmd_lsort.t t/cmd_namespace.t # t/cmd_proc.t t/cmd_regexp.t t/cmd_return.t t/cmd_set.t t/cmd_socket.t # t/cmd_subst.t t/cmd_switch.t t/cmd_trace.t t/cmd_unset.t t/cmd_upvar.t # t/cmd_variable.t t/cmd_vwait.t t/tcl_backslash.t t/tcl_conversion.t diff --git a/src/Partcl/commands/main.pm b/src/Partcl/commands/main.pm index b40a2ee..1a3f6fc 100644 --- a/src/Partcl/commands/main.pm +++ b/src/Partcl/commands/main.pm @@ -477,6 +477,35 @@ our sub lrepeat(*@args) { } our sub lreplace(*@args) { + if +@args < 3 { + error('wrong # args: should be "lreplace list first last ?element element ...?"'); + } + + my @list := pir::clone__pp(@args.shift().getList()); + + my $first := @list.getIndex(@args.shift()); + my $last := @list.getIndex(@args.shift()); + + if +@list == 0 { + pir::splice__vppii(@list, @args, 0, 0); + return @list; + } + + $last := +@list -1 if $last >= +@list; + $first := 0 if $first < 0; + + if $first >= +@list { + error("list doesn't contain element $first"); + } + + my $count := $last - $first + 1; + if $count >= 0 { + pir::splice__vppii(@list, @args, $first, $count); + return @list; + } + + pir::splice__vppii(@list, @args, $first, 0); + return @list; } our sub lreverse(*@args) {