Skip to content

Commit

Permalink
Move TRANSPOSE(-ONE) to more suitable place
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Feb 17, 2015
1 parent f17f88b commit 1c3d562
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 56 deletions.
56 changes: 0 additions & 56 deletions src/core/IO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,62 +47,6 @@ sub MAKE-ABSOLUTE-PATH(Str $path, Str $abspath) {
sub FORWARD-SLASH(Str \path) { TRANSPOSE-ONE(path,'\\','/') }
sub BACKWARD-SLASH(Str \path) { TRANSPOSE-ONE(path,'/','\\') }

sub TRANSPOSE-ONE(Str \path,\original,\final) { # 200x faster than .trans
my str $str = nqp::unbox_s(path);
my int $chars = nqp::chars($str);
my int $ordinal = ord(original);
my int $from;
my int $to;
my $parts := nqp::list_s();

while $to < $chars {
if nqp::ordat($str,$to) == $ordinal {
nqp::push_s($parts, $to > $from
?? nqp::substr($str,$from,$to - $from)
!! ''
);
$from = $to + 1;
}
$to = $to + 1;
}
nqp::push_s( $parts, $from < $chars
?? nqp::substr($str,$from,$chars - $from)
!! ''
);

nqp::elems($parts)
?? nqp::box_s(nqp::join(nqp::unbox_s(final),$parts),Str)
!! path;
}

sub TRANSPOSE(Str \path,\original,\final) { # 100x faster than .trans
my str $str = nqp::unbox_s(path);
my int $chars = nqp::chars($str);
my str $needle = nqp::unbox_s(original);
my int $skip = nqp::chars($needle);
my int $from;
my int $to;
my Mu $parts := nqp::list_s();

while $to < $chars {
$to = nqp::index($str,$needle,$from);
last if $to == -1;
nqp::push_s($parts, $to > $from
?? nqp::substr($str,$from,$to - $from)
!! ''
);
$to = $from = $to + $skip;
}
nqp::push_s( $parts, $from < $chars
?? nqp::substr($str,$from,$chars - $from)
!! ''
);

nqp::elems($parts)
?? nqp::box_s(nqp::join(nqp::unbox_s(final),$parts),Str)
!! path;
}

sub MAKE-BASENAME(Str $abspath) {
my str $abspath_s = nqp::unbox_s($abspath);
my int $offset = nqp::rindex($abspath_s,'/');
Expand Down
56 changes: 56 additions & 0 deletions src/core/Str.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,62 @@ sub substr-rw($s is rw, $from, $length?) {
);
}

sub TRANSPOSE(Str \string, Str \original, Str \final) {
my str $str = nqp::unbox_s(string);
my int $chars = nqp::chars($str);
my str $needle = nqp::unbox_s(original);
my int $skip = nqp::chars($needle);
my int $from;
my int $to;
my Mu $parts := nqp::list_s();

while $to < $chars {
$to = nqp::index($str,$needle,$from);
last if $to == -1;
nqp::push_s($parts, $to > $from
?? nqp::substr($str,$from,$to - $from)
!! ''
);
$to = $from = $to + $skip;
}
nqp::push_s( $parts, $from < $chars
?? nqp::substr($str,$from,$chars - $from)
!! ''
);

nqp::elems($parts)
?? nqp::box_s(nqp::join(nqp::unbox_s(final),$parts),Str)
!! string;
}

sub TRANSPOSE-ONE(Str \string, Str \original, Str \final) {
my str $str = nqp::unbox_s(string);
my int $chars = nqp::chars($str);
my int $ordinal = ord(original);
my int $from;
my int $to;
my $parts := nqp::list_s();

while $to < $chars {
if nqp::ordat($str,$to) == $ordinal {
nqp::push_s($parts, $to > $from
?? nqp::substr($str,$from,$to - $from)
!! ''
);
$from = $to + 1;
}
$to = $to + 1;
}
nqp::push_s( $parts, $from < $chars
?? nqp::substr($str,$from,$chars - $from)
!! ''
);

nqp::elems($parts)
?? nqp::box_s(nqp::join(nqp::unbox_s(final),$parts),Str)
!! string;
}

# These probably belong in a separate unicodey file

#?if parrot
Expand Down

0 comments on commit 1c3d562

Please sign in to comment.