Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move .substr guts from Cool to Str, make .substr properly fail on neg…
…ative start or length arguments.
  • Loading branch information
pmichaud committed Jul 5, 2011
1 parent 87e9756 commit 86ff74d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
22 changes: 2 additions & 20 deletions src/core/Cool.pm
Expand Up @@ -13,26 +13,8 @@ my class Cool {
)
}

method substr($start as Int, $length?) {
# TODO: Update this to match the spec.
my Str $str := self.Str;
my Int $len := ($length // $str.chars).Int;
if ($len < 0) {
if ($start >= 0) {
$len := $len + $str.chars;
}
$len := $len - $start;
}

# XXX no prefix:<-> yet...
#if ($start > self.chars || $start < -self.chars) {
# return Mu;
#}

nqp::p6box_s(nqp::substr(
nqp::unbox_s($str),
nqp::unbox_i($start),
nqp::unbox_i($len)));
method substr($start as Int, $length?) {
self.Stringy.substr($start, $length);
}

method uc() {
Expand Down
12 changes: 12 additions & 0 deletions src/core/Str.pm
Expand Up @@ -32,6 +32,18 @@ my class Str {
);
}

method substr($start as Int, $length? is copy) {
fail "Negative start argument ($start) to .substr" if $start < 0;
fail "Start of substr ($start) beyond end of string" if $start > self.chars;
$length = self.chars - $start if !$length.defined;
fail "Negative length argument ($length) to .substr" if $length < 0;


nqp::p6box_s(nqp::substr(
nqp::unbox_s(self),
nqp::unbox_i($start),
nqp::unbox_i($length)));
}

# chars used to handle ranges for pred/succ
my $RANGECHAR =
Expand Down

0 comments on commit 86ff74d

Please sign in to comment.