Skip to content

Commit

Permalink
Add [lreplace], based on partcl's PIR version but in NQP.
Browse files Browse the repository at this point in the history
All tests pass, enable the test file.
  • Loading branch information
coke committed Mar 31, 2010
1 parent 2393e1a commit 8f342db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build/Makefile.in
Expand Up @@ -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 \
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/Partcl/commands/main.pm
Expand Up @@ -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) {
Expand Down

0 comments on commit 8f342db

Please sign in to comment.