Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add EXPAND-LITERAL-RANGE to Rakudo::Internals
Expands "a..e" to "abcde", "a..e..i" to "abcdefghi" and "a..ei..k"
to "abcdeijk".  Based on internal sub in Str, but now without recursion
and now also allows returning a list (with each char separately),
rather than a string
  • Loading branch information
lizmat committed Feb 18, 2016
1 parent 0270966 commit 64ab188
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/core/Rakudo/Internals.pm
Expand Up @@ -900,6 +900,46 @@ my class Rakudo::Internals {
!! X::Dynamic::NotFound.new(:name(name));
}

method EXPAND-LITERAL-RANGE(Str:D \x,$list) {
my str $s = nqp::unbox_s(x);
my int $chars = nqp::chars($s);
my Mu $result := nqp::list();
my int $start = 1;
my int $found = nqp::index($s,'..',$start);

# found and not at the end without trail
while nqp::isne_i($found,-1) && nqp::isne_i($found,$chars-2) {

if $found - $start -> $unsplit {
nqp::splice(
$result,
nqp::split("",nqp::substr($s,$start - 1,$unsplit)),
nqp::elems($result),
0
)
}

# add the range excluding last (may be begin point next range)
my int $from = nqp::ordat($s,$found - 1) - 1;
my int $to = nqp::ordat($s,$found + 2);
nqp::push($result,nqp::chr($from))
while nqp::islt_i($from = $from + 1,$to);

# look for next range
$found = nqp::index($s,'..',$start = $found + 3);
}

# add final bits
nqp::splice(
$result,
nqp::split("",nqp::substr($s,$start - 1)),
nqp::elems($result),
0
) if nqp::isle_i($start,$chars);

$list ?? $result !! nqp::join("",$result)
}

method MAKE-ABSOLUTE-PATH(Str:D $path, Str:D $abspath) {
if $path.ord == 47 { # 4x faster substr($path,0,1) eq "/"
$path
Expand Down

0 comments on commit 64ab188

Please sign in to comment.