Skip to content

Commit

Permalink
- String.match() and String.gmatch() are a little bit faster now.
Browse files Browse the repository at this point in the history
- Optimized Array.splice() to no longer create an anonymous array.
  • Loading branch information
trizen committed Dec 15, 2015
1 parent 801ff02 commit 31f81c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
8 changes: 2 additions & 6 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ package Sidef::Types::Array::Array {

sub insert {
my ($self, $index, @objects) = @_;
splice(@{$self}, $index->get_value, 0, @{__PACKAGE__->new(@objects)});
splice(@{$self}, $index->get_value, 0, @objects);
$self;
}

Expand Down Expand Up @@ -1197,11 +1197,7 @@ package Sidef::Types::Array::Array {
$offset = defined($offset) ? $offset->get_value : 0;
$length = defined($length) ? $length->get_value : scalar(@{$self});
if (@objects) {
return $self->new(CORE::splice(@{$self}, $offset, $length, @{__PACKAGE__->new(@objects)}));
}
$self->new(CORE::splice(@{$self}, $offset, $length));
$self->new(CORE::splice(@{$self}, $offset, $length, @objects));
}
sub take_right {
Expand Down
13 changes: 3 additions & 10 deletions lib/Sidef/Types/Regex/Regex.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@ package Sidef::Types::Regex::Regex {
sub new {
my (undef, $regex, $mode) = @_;

if (ref($mode) eq 'Sidef::Types::String::String') {
$mode = $mode->get_value;
}

my $global_mode = defined($mode) && $mode =~ tr/g//d;

if (not defined $mode or $mode eq '') {
$mode = q{^};
}
$mode = defined($mode) ? ref($mode) ? $mode->get_value : $mode : '^';

my $global_mode = $mode =~ tr/g//d;
my $compiled_re = qr{(?$mode:$regex)};

bless {
Expand All @@ -44,7 +37,7 @@ package Sidef::Types::Regex::Regex {
sub match {
my ($self, $object, $pos) = @_;

$object //= Sidef::Types::String::String->new('');
$object //= do { state $x = Sidef::Types::String::String->new('') };

if ($object->SUPER::isa('ARRAY')) {
my $match;
Expand Down
25 changes: 21 additions & 4 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,30 @@ package Sidef::Types::String::String {
}

sub match {
my ($self, $regex, @rest) = @_;
$regex->match($self, @rest);
my ($self, $regex, $pos) = @_;

$regex = $regex->to_re
if ref($regex) ne 'Sidef::Types::Regex::Regex';

Sidef::Types::Regex::Match->new(
obj => $$self,
self => $regex,
pos => defined($pos) ? $pos->get_value : undef,
);
}

sub gmatch {
my ($self, $regex, @rest) = @_;
$regex->gmatch($self, @rest);
my ($self, $regex, $pos) = @_;

$regex = $regex->to_re
if ref($regex) ne 'Sidef::Types::Regex::Regex';

local $regex->{global} = 1;
Sidef::Types::Regex::Match->new(
obj => $$self,
self => $regex,
pos => defined($pos) ? $pos->get_value : undef,
);
}

sub array_to {
Expand Down

0 comments on commit 31f81c4

Please sign in to comment.