Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
RT #122469 coerce indent()'s arg to Int
  • Loading branch information
perlpilot committed Aug 4, 2014
1 parent f25e551 commit caee539
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/core/Str.pm
Expand Up @@ -987,12 +987,12 @@ my class Str does Stringy { # declared in BOOTSTRAP
}
proto method indent($) {*}
# Zero indent does nothing
multi method indent(Int $steps where { $_ == 0 }) {
multi method indent($steps as Int where { $_ == 0 }) {
self;
}

# Positive indent does indent
multi method indent(Int $steps where { $_ > 0 }) {
multi method indent($steps as Int where { $_ > 0 }) {
# We want to keep trailing \n so we have to .comb explicitly instead of .lines
return self.comb(/:r ^^ \N* \n?/).map({
given $_.Str {
Expand All @@ -1018,10 +1018,19 @@ my class Str does Stringy { # declared in BOOTSTRAP
}).join;
}

# Negative values and Whatever-* do outdent
multi method indent($steps where { nqp::istype($_, Whatever) || nqp::istype($_, Int) && $_ < 0 }) {
# Negative indent (outdent)
multi method indent($steps as Int where { $_ < 0 }) {
return outdent(self, $steps);
}

# Whatever indent (outdent)
multi method indent(Whatever $steps) {
return outdent(self, $steps);
}

sub outdent($obj, $steps) {
# Loop through all lines to get as much info out of them as possible
my @lines = self.comb(/:r ^^ \N* \n?/).map({
my @lines = $obj.comb(/:r ^^ \N* \n?/).map({
# Split the line into indent and content
my ($indent, $rest) = @($_ ~~ /^(\h*) (.*)$/);

Expand All @@ -1041,7 +1050,7 @@ my class Str does Stringy { # declared in BOOTSTRAP

# Figure out the amount * should outdent by, we also use this for warnings
my $common-prefix = min @lines.grep({ .<indent-size> || .<rest> ~~ /\S/}).map({ $_<indent-size> });
return self if $common-prefix === Inf;
return $obj if $common-prefix === Inf;

# Set the actual outdent amount here
my Int $outdent = $steps ~~ Whatever ?? $common-prefix
Expand Down

0 comments on commit caee539

Please sign in to comment.