Skip to content

Commit

Permalink
- The support for Regex variables ($1, $2, ...) is now gone!
Browse files Browse the repository at this point in the history
Use a match variable instead:
        var match = /(\w+)\s+(\w+)/.match("hello world");
        say match[0];     # prints: "hello"
	say match[1];	  # prints: "world"

- Optimized the match[i] caption access: only an array with strings will be created per match, therefore is not equivalent with match.cap[0], which will create a new array each time it is called.
  • Loading branch information
trizen committed Oct 25, 2015
1 parent 5ed23c9 commit 1eaf761
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ scripts/Tests/recursion.sf
scripts/Tests/regex.sf
scripts/Tests/regex_bool.sf
scripts/Tests/regex_global_matching.sf
scripts/Tests/regex_match_as_array.sf
scripts/Tests/return.sf
scripts/Tests/return_types.sf
scripts/Tests/reverse_recursive_general_solution.sf
Expand Down
10 changes: 8 additions & 2 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ package Sidef::Parser {
# Regular expression
if (m{\G(?=/)} || /\G%r\b/gc) {
my $string = $self->get_quoted_string(code => $opt{code});
return Sidef::Types::Regex::Regex->new($string, /\G($self->{match_flags_re})/goc ? $1 : undef, $self);
return Sidef::Types::Regex::Regex->new($string, /\G($self->{match_flags_re})/goc ? $1 : undef);
}

# Static object (like String or nil)
Expand Down Expand Up @@ -1927,7 +1927,13 @@ package Sidef::Parser {

# Regex variables ($1, $2, ...)
if (/\G\$([0-9]+)\b/gc) {
return $self->{regexp_vars}{$1} //= Sidef::Variable::Variable->new(name => $1, type => 'var');
$self->fatal_error(
code => $_,
pos => (pos($_) - length($1)),
error => "attempt to use the deprecated regex variables",
);

#return $self->{regexp_vars}{$1} //= Sidef::Variable::Variable->new(name => $1, type => 'var');
}

/\G\$/gc && redo;
Expand Down
16 changes: 9 additions & 7 deletions lib/Sidef/Types/Regex/Match.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package Sidef::Types::Regex::Match {
use 5.014;
use overload
q{bool} => \&to_bool,
q{@{}} => \&cap,
q{""} => \&to_s;
q{""} => \&to_s,
q{@{}} => sub {
$_[0]->{_cached_arr} //= [map { Sidef::Types::String::String->new($_) } @{$_[0]->{captures}}];
};

use parent qw(
Sidef::Object::Object
Expand Down Expand Up @@ -52,11 +54,11 @@ package Sidef::Types::Regex::Match {

$hash{captures} = \@captures;

if (defined $hash{parser}) {
while (my ($key, $value) = each %{$hash{parser}{regexp_vars}}) {
$value->set_value(Sidef::Types::String::String->new($captures[$key - 1]));
}
}
#if (defined $hash{parser}) {
# while (my ($key, $value) = each %{$hash{parser}{regexp_vars}}) {
# $value->set_value(Sidef::Types::String::String->new($captures[$key - 1]));
# }
#}

bless \%hash, __PACKAGE__;
}
Expand Down
10 changes: 4 additions & 6 deletions lib/Sidef/Types/Regex/Regex.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package Sidef::Types::Regex::Regex {
use overload q{""} => \&get_value;

sub new {
my (undef, $regex, $mode, $parser) = @_;
my (undef, $regex, $mode) = @_;

if (ref($mode) eq 'Sidef::Types::String::String') {
$mode = $mode->get_value;
Expand All @@ -30,7 +30,6 @@ package Sidef::Types::Regex::Regex {
regex => $compiled_re,
global => $global_mode,
pos => 0,
parser => $parser,
},
__PACKAGE__;
}
Expand All @@ -55,10 +54,9 @@ package Sidef::Types::Regex::Regex {
}

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

Expand Down
29 changes: 12 additions & 17 deletions scripts/Tests/arithmetic_evaluation.sf
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ func evalArithmeticExp(s) {
}

var b = s.split('-');
b.len == 3
? (-1 * b[1].to_num - b[2].to_num)
: operate(s, '-');
b.len == 3 ? (-1*b[1].to_num - b[2].to_num)
: operate(s, '-');
}

s.gsub!(/[()]/,'').gsub!(/-\+/, '-');
Expand All @@ -35,19 +34,16 @@ func evalArithmeticExp(s) {
var reA = /\d\+/;
var reAS = /(-?\d+\.?\d*\s*[+-]\s*[+-]?\d+\.?\d*)/;

var match;
while (match = s.match(reMD)) {
var cap = match.cap[0];
cap ~~ reM
? s.sub!(reMD, operate(cap, '*').to_s)
: s.sub!(reMD, operate(cap, '/').to_s);
while (var match = reMD.match(s)) {
match[0] ~~ reM
? s.sub!(reMD, operate(match[0], '*').to_s)
: s.sub!(reMD, operate(match[0], '/').to_s);
}

while (match = s.match(reAS)) {
var cap = match.cap[0];
cap ~~ reA
? s.sub!(reAS, add(cap).to_s)
: s.sub!(reAS, subtract(cap).to_s);
while (var match = reAS.match(s)) {
match[0] ~~ reA
? s.sub!(reAS, add(match[0]).to_s)
: s.sub!(reAS, subtract(match[0]).to_s);
}

return s;
Expand All @@ -56,9 +52,8 @@ func evalArithmeticExp(s) {
var rePara = /(\([^\(\)]*\))/;
s.split!.join!('').sub!(/^\+/,'');

var match;
while (match = s.match(rePara)) {
s.sub!(rePara, evalExp(match.captures[0]));
while (var match = s.match(rePara)) {
s.sub!(rePara, evalExp(match[0]));
}

return evalExp(s).to_num;
Expand Down
8 changes: 8 additions & 0 deletions scripts/Tests/regex_match_as_array.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/ruby

var m = /(\w+)\s+(\w+)/.match("foo bar");

assert_eq(m[0], 'foo');
assert_eq(m[1], 'bar');

say "** Test passed!";

0 comments on commit 1eaf761

Please sign in to comment.