Skip to content

Commit

Permalink
- Extended the String.contains() method to support regular expression…
Browse files Browse the repository at this point in the history
…s as arguments
  • Loading branch information
trizen committed Nov 8, 2015
1 parent de7537b commit 87b8a4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
15 changes: 13 additions & 2 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -898,18 +898,29 @@ package Sidef::Types::String::String {
}

sub contains {
my ($self, $string, $start_pos) = @_;
my ($self, $arg, $start_pos) = @_;

$start_pos =
defined($start_pos)
? $start_pos->get_value
: 0;

if (ref($arg) eq 'Sidef::Types::Regex::Regex') {
my $regex = $arg->{regex};
my $s = $$self;

if ($start_pos != 0) {
pos($s) = $start_pos;
}

return Sidef::Types::Bool::Bool->new(scalar $s =~ /$regex/g);
}

if ($start_pos < 0) {
$start_pos = CORE::length($$self) + $start_pos;
}

Sidef::Types::Bool::Bool->new(CORE::index($$self, $string->get_value, $start_pos) != -1);
Sidef::Types::Bool::Bool->new(CORE::index($$self, $arg->get_value, $start_pos) != -1);
}

*include = \&contains;
Expand Down
9 changes: 3 additions & 6 deletions scripts/Tests/quicksort_in_parallel.sf
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ func quicksort(arr) {

say arr;

var le = arr.grep{ _ <= p};
var gt = arr.grep{ _ > p};

var forks = [
{quicksort(le)}.fork,
{quicksort(gt)}.fork,
{ quicksort(arr.grep{ _ <= p}) }.fork,
{ quicksort(arr.grep{ _ > p }) }.fork,
];

forks[0].get + [p] + forks[1].get;
}

quicksort(1..50 -> shuffle).dump.say;
say quicksort(1..50 -> shuffle);

0 comments on commit 87b8a4a

Please sign in to comment.