Skip to content

Commit

Permalink
implement [string range]
Browse files Browse the repository at this point in the history
  • Loading branch information
coke authored and Will committed Mar 23, 2010
1 parent 54b4f03 commit 973490f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Partcl/commands/string.pm
Expand Up @@ -112,7 +112,21 @@ our sub string(*@args) {
my $globber := StringGlob::Compiler.compile($pattern);
?Regex::Cursor.parse($string, :rule($globber), :c(0));
} elsif $cmd eq 'range' {
return '';
if +@args != 3 {
error('wrong # args: should be "string range string first last"')
}

my $string := @args[0];
my $first := $string.getIndex(@args[1]);
my $last := $string.getIndex(@args[2]);

return '' if $first > $last;
$first := 0 if $first < 0;

my $last_index := pir::length__is($string) - 1;
$last := $last_index if $last > $last_index;

return pir::substr__ssii($string, $first, $last-$first+1)
} elsif $cmd eq 'repeat' {
if +@args != 2 {
error('wrong # args: should be "string repeat string count"');
Expand Down

0 comments on commit 973490f

Please sign in to comment.