Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
[setting] implement split(), mostly stolen from partcl-nqp, Coke++
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Aug 20, 2010
1 parent 253aeeb commit b139200
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/setting/Regex.pm
Expand Up @@ -56,4 +56,34 @@ our sub subst ($text, $regex, $repl, :$global?) {
~$result;
}

=begin item split
Splits C<$text> on occurences of C<$regex>
=end item

our sub split ($regex, $text) {
my $pos := 0;
my @result;
my $looking := 1;
while $looking {
my $match :=
Regex::Cursor.parse($text, :rule($regex), :c($pos)) ;

if ?$match {
my $from := $match.from();
my $to := $match.to();
my $prefix := pir::substr__sPii($text, $pos, $from-$pos);
pir::push__vPP(@result, $prefix);
$pos := $match.to();
} else {
my $len := pir::length($text);
if $pos < $len {
pir::push__vPP(@result, pir::substr__ssi($text, $pos) );
}
$looking := 0;
}
}
return @result;
}


# vim: ft=perl6
11 changes: 11 additions & 0 deletions t/setting/04-regex.t
@@ -0,0 +1,11 @@
#! nqp

pir::load_bytecode('nqp-setting.pbc');

plan(2);

my @a := split(/\d/, 'a23b5d');
ok(+@a == 4, 'split produced 4 chunks');
ok(pir::join__SSP('|',@a) eq 'a||b5d', 'got right chunks');

# vim: ft=perl6

0 comments on commit b139200

Please sign in to comment.