From 9fd1e7cd05ee9d8df5712543ac0d39dec5801255 Mon Sep 17 00:00:00 2001 From: Will Date: Mon, 22 Mar 2010 23:49:32 -0400 Subject: [PATCH] add [string totitle] --- src/Partcl/commands/string.pm | 42 ++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Partcl/commands/string.pm b/src/Partcl/commands/string.pm index 3d02412..b2d8397 100644 --- a/src/Partcl/commands/string.pm +++ b/src/Partcl/commands/string.pm @@ -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?"')