Skip to content

Commit

Permalink
parse headers a little faster
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 10, 2015
1 parent fa631a9 commit 85abcdb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/DOM/HTML.pm
Expand Up @@ -105,7 +105,7 @@ sub parse {

my $xml = $self->xml;
my $current = my $tree = ['root'];
while ($html =~ m/\G$TOKEN_RE/gcso) {
while ($html =~ /\G$TOKEN_RE/gcso) {
my ($text, $doctype, $comment, $cdata, $pi, $tag, $runaway)
= ($1, $2, $3, $4, $5, $6, $11);

Expand Down
38 changes: 19 additions & 19 deletions lib/Mojo/JSON.pm
Expand Up @@ -67,7 +67,7 @@ sub _decode {
$$valueref = _decode_value();

# Leftover data
m/\G[\x20\x09\x0a\x0d]*\z/gc or _throw('Unexpected data');
/\G[\x20\x09\x0a\x0d]*\z/gc or _throw('Unexpected data');
} ? return undef : chomp $@;

return $@;
Expand All @@ -81,10 +81,10 @@ sub _decode_array {
push @array, _decode_value();

# Separator
redo if m/\G[\x20\x09\x0a\x0d]*,/gc;
redo if /\G[\x20\x09\x0a\x0d]*,/gc;

# End
last if m/\G[\x20\x09\x0a\x0d]*\]/gc;
last if /\G[\x20\x09\x0a\x0d]*\]/gc;

# Invalid character
_throw('Expected comma or right square bracket while parsing array');
Expand All @@ -98,24 +98,24 @@ sub _decode_object {
until (m/\G[\x20\x09\x0a\x0d]*\}/gc) {

# Quote
m/\G[\x20\x09\x0a\x0d]*"/gc
/\G[\x20\x09\x0a\x0d]*"/gc
or _throw('Expected string while parsing object');

# Key
my $key = _decode_string();

# Colon
m/\G[\x20\x09\x0a\x0d]*:/gc
/\G[\x20\x09\x0a\x0d]*:/gc
or _throw('Expected colon while parsing object');

# Value
$hash{$key} = _decode_value();

# Separator
redo if m/\G[\x20\x09\x0a\x0d]*,/gc;
redo if /\G[\x20\x09\x0a\x0d]*,/gc;

# End
last if m/\G[\x20\x09\x0a\x0d]*\}/gc;
last if /\G[\x20\x09\x0a\x0d]*\}/gc;

# Invalid character
_throw('Expected comma or right curly bracket while parsing object');
Expand All @@ -134,7 +134,7 @@ sub _decode_string {
# Invalid character
unless (m/\G"/gc) {
_throw('Unexpected character or invalid escape while parsing string')
if m/\G[\x00-\x1f\\]/;
if /\G[\x00-\x1f\\]/;
_throw('Unterminated string');
}

Expand All @@ -146,7 +146,7 @@ sub _decode_string {

# Unescape everything else
my $buffer = '';
while ($str =~ m/\G([^\\]*)\\(?:([^u])|u(.{4}))/gc) {
while ($str =~ /\G([^\\]*)\\(?:([^u])|u(.{4}))/gc) {
$buffer .= $1;

# Popular character
Expand All @@ -164,7 +164,7 @@ sub _decode_string {
or pos($_) = $pos + pos($str), _throw('Missing high-surrogate');

# Low surrogate
$str =~ m/\G\\u([Dd][C-Fc-f]..)/gc
$str =~ /\G\\u([Dd][C-Fc-f]..)/gc
or pos($_) = $pos + pos($str), _throw('Missing low-surrogate');

$ord = 0x10000 + ($ord - 0xd800) * 0x400 + (hex($1) - 0xdc00);
Expand All @@ -182,29 +182,29 @@ sub _decode_string {
sub _decode_value {

# Leading whitespace
m/\G[\x20\x09\x0a\x0d]*/gc;
/\G[\x20\x09\x0a\x0d]*/gc;

# String
return _decode_string() if m/\G"/gc;
return _decode_string() if /\G"/gc;

# Object
return _decode_object() if m/\G\{/gc;
return _decode_object() if /\G\{/gc;

# Array
return _decode_array() if m/\G\[/gc;
return _decode_array() if /\G\[/gc;

# Number
return 0 + $1
if m/\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc;
if /\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc;

# True
return $TRUE if m/\Gtrue/gc;
return $TRUE if /\Gtrue/gc;

# False
return $FALSE if m/\Gfalse/gc;
return $FALSE if /\Gfalse/gc;

# Null
return undef if m/\Gnull/gc;
return undef if /\Gnull/gc;

# Invalid character
_throw('Expected string, array, object, number, boolean or null');
Expand Down Expand Up @@ -265,7 +265,7 @@ sub _encode_value {
sub _throw {

# Leading whitespace
m/\G[\x20\x09\x0a\x0d]*/gc;
/\G[\x20\x09\x0a\x0d]*/gc;

# Context
my $context = 'Malformed JSON: ' . shift;
Expand Down
16 changes: 9 additions & 7 deletions lib/Mojo/Util.pm
Expand Up @@ -48,6 +48,9 @@ my %XML = (
'\'' => '''
);

# "Sun, 06 Nov 1994 08:49:37 GMT" or "Sunday, 06-Nov-94 08:49:37 GMT"
my $EXPIRES_RE = qr/(\w+\,\s+\d+\W+\w+\D+\d+\s+\d+:\d+:\d+\s+GMT)/;

# Encoding cache
my %CACHE;

Expand Down Expand Up @@ -383,23 +386,22 @@ sub _header {
my ($str, $cookie) = @_;

my (@tree, @token);
while ($str =~ s/^[,;\s]*([^=;, ]+)\s*//) {
while ($str =~ /\G[,;\s]*([^=;, ]+)\s*/gc) {
push @token, $1, undef;

# Special "expires" value (Sun, 06 Nov 1994 08:49:37 GMT)
my $e = $cookie && lc $1 eq 'expires';
if ($e && $str =~ s/^=\s*(\w+\,\s+\d+\W+\w+\D+\d+\s+\d+:\d+:\d+\s+GMT)//) {
# Special "expires" value
if ($cookie && lc $1 eq 'expires' && $str =~ /\G=\s*$EXPIRES_RE/gco) {
$token[-1] = $1;
}

# Normal value
elsif ($str =~ s/^=\s*("(?:\\\\|\\"|[^"])*"|[^;, ]*)\s*//) {
elsif ($str =~ /\G=\s*("(?:\\\\|\\"|[^"])*"|[^;, ]*)\s*/gc) {
$token[-1] = unquote $1;
}

# Separator
$str =~ s/^;\s*//;
next unless $str =~ s/^,\s*//;
$str =~ /\G;\s*/gc;
next unless $str =~ /\G,\s*/gc;
push @tree, [@token];
@token = ();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Static.pm
Expand Up @@ -105,7 +105,7 @@ sub serve_asset {

# Not satisfiable
return $res->code(416) unless my $size = $asset->size;
return $res->code(416) unless $range =~ m/^bytes=(\d+)?-(\d+)?/;
return $res->code(416) unless $range =~ /^bytes=(\d+)?-(\d+)?/;
my ($start, $end) = ($1 // 0, defined $2 && $2 < $size ? $2 : $size - 1);
return $res->code(416) if $start > $end;

Expand Down

0 comments on commit 85abcdb

Please sign in to comment.