Skip to content

Commit

Permalink
add [string totitle]
Browse files Browse the repository at this point in the history
  • Loading branch information
coke committed Mar 23, 2010
1 parent 5e1227c commit 9fd1e7c
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/Partcl/commands/string.pm
Expand Up @@ -219,7 +219,47 @@ our sub string(*@args) {
%r = $P0
}
} elsif $cmd eq 'totitle' {
return '';
if +@args <1 || +@args >3 {
error('wrong # args: should be "string totitle string ?first? ?last?"')
}
my $orig_str := @args[0];
my $orig_len := pir::length__is($orig_str);
# If no range is specified, do to all the string
my $first; my $last;
if pir::defined(@args[1]) {
$first := $orig_str.getIndex(@args[1]);
if pir::defined(@args[2]) {
$last := $orig_str.getIndex(@args[2]);
} else {
$last := $first;
}
} else {
$first := 0;
$last := $orig_len;
}
return $orig_str if $first > $orig_len;
$last := $orig_len if $last > $orig_len;
my $chunk_len := $last - $first + 1;
return Q:PIR {
$P0 = find_lex '$orig_str'
$S0 = $P0
$P0 = find_lex '$first'
$I0 = $P0
$P0 = find_lex '$chunk_len'
$I1 = $P0
$S1 = substr $S0, $I0, $I1
titlecase $S1
substr $S0, $I0, $I1, $S1
$P0 = box $S0
%r = $P0
}
} elsif $cmd eq 'toupper' {
if +@args <1 || +@args >3 {
error('wrong # args: should be "string toupper string ?first? ?last?"')
Expand Down

0 comments on commit 9fd1e7c

Please sign in to comment.