Skip to content

Commit

Permalink
improved regex performance slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 28, 2011
1 parent cf468e5 commit 8721e7e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -87,7 +87,7 @@ sub _compile {

# Tokenize
my $pattern = [[]];
while ($css =~ /$TOKEN_RE/g) {
while ($css =~ /$TOKEN_RE/og) {
my ($separator, $element, $pc, $attrs, $combinator) =
($+{separator}, $+{element} // '', $+{pc}, $+{attrs}, $+{combinator});

Expand All @@ -104,7 +104,7 @@ sub _compile {

# Element
my $tag = '';
$element =~ s/$ELEMENT_RE// and $tag = $self->_unescape($+{element});
$element =~ s/$ELEMENT_RE//o and $tag = $self->_unescape($+{element});

# Subject
$selector->[0] = 'subject' if $tag =~ s/^\$//;
Expand All @@ -114,7 +114,7 @@ sub _compile {
push @$selector, ['tag', $tag];

# Class or ID
while ($element =~ /$CLASS_ID_RE/g) {
while ($element =~ /$CLASS_ID_RE/og) {

# Class
push @$selector, ['attribute', 'class', $self->_regex('~', $+{class})]
Expand All @@ -126,7 +126,7 @@ sub _compile {
}

# Pseudo classes
while ($pc =~ /$PSEUDO_CLASS_RE/g) {
while ($pc =~ /$PSEUDO_CLASS_RE/og) {

# "not"
if ($+{class} eq 'not') {
Expand All @@ -139,7 +139,7 @@ sub _compile {
}

# Attributes
while ($attrs =~ /$ATTR_RE/g) {
while ($attrs =~ /$ATTR_RE/og) {
my $key = $self->_unescape($+{key});
my $op = $+{op} // '';
my $value = $+{escaped} // $+{unescaped};
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -100,7 +100,7 @@ sub parse {
# Tokenize
my $tree = ['root'];
my $current = $tree;
while ($html =~ m/\G$TOKEN_RE/gcs) {
while ($html =~ m/\G$TOKEN_RE/ogcs) {
my ($text, $pi, $comment, $cdata, $doctype, $tag) =
(@+{qw/text pi comment cdata doctype tag/});

Expand All @@ -127,17 +127,17 @@ sub parse {
# End
next unless $tag;
my $cs = $self->xml;
if ($tag =~ /$END_RE/) {
if ($tag =~ $END_RE) {
$self->_end($cs ? $+{tag} : lc($+{tag}), \$current);
}

# Start
elsif ($tag =~ /$START_RE/) {
elsif ($tag =~ $START_RE) {
my ($start, $attr) = ($cs ? $+{tag} : lc($+{tag}), $+{attrs});

# Attributes
my $attrs = {};
while ($attr =~ /$ATTR_RE/g) {
while ($attr =~ /$ATTR_RE/og) {
my $key = $cs ? $+{key} : lc($+{key});
my $value = $+{quoted} // $+{quoted_too} // $+{unquoted};

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Template.pm
Expand Up @@ -276,7 +276,7 @@ sub parse {

# Tokenize
my @token;
for my $token (split /$token_re/, $line) {
for my $token (split $token_re, $line) {

# Capture end
@capture_token = ('cpen', undef)
Expand Down

0 comments on commit 8721e7e

Please sign in to comment.