Skip to content

Commit

Permalink
changed heuristics for number detection in Mojo::JSON to better line …
Browse files Browse the repository at this point in the history
…up with user expectations
  • Loading branch information
kraih committed May 13, 2013
1 parent 9c53f86 commit 9f75c27
Show file tree
Hide file tree
Showing 29 changed files with 162 additions and 147 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -10,6 +10,8 @@
- Changed Mojolicious default secret to the application moniker to make it
slightly more secure.
- Changed types in Mojolicious::Types to consistently use arrays.
- Changed heuristics for number detection in Mojo::JSON to better line up
with user expectations.
- Removed callback support from body method in Mojo::Message.
- Removed Mojolicious::Plugin::PoweredBy and
Mojolicious::Plugin::RequestTimer.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -57,14 +57,14 @@ sub add_chunk {
}

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

my $handle = $self->handle;
$handle->sysseek($self->start_range, SEEK_SET);

# Calculate window size
my $end = $self->end_range // $self->size;
my $len = length $string;
my $len = length $str;
my $size = $len > 131072 ? $len : 131072;
$size = $end - $self->start_range if $size > $end - $self->start_range;

Expand All @@ -79,7 +79,7 @@ sub contains {
$window .= $buffer;

# Search window
my $pos = index $window, $string;
my $pos = index $window, $str;
return $offset + $pos if $pos >= 0;
$offset += $read;
return -1 if $read == 0 || $offset == $end;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -21,14 +21,14 @@ sub add_chunk {
}

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

my $start = $self->start_range;
my $pos = index $self->{content}, $string, $start;
my $pos = index $self->{content}, $str, $start;
$pos -= $start if $start && $pos >= 0;
my $end = $self->end_range;

return $end && ($pos + length $string) >= $end ? -1 : $pos;
return $end && ($pos + length $str) >= $end ? -1 : $pos;
}

sub get_chunk {
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -216,7 +216,7 @@ Print bytestream to handle and append a newline, defaults to C<STDOUT>.
=head2 secure_compare
my $success = $stream->secure_compare($string);
my $success = $stream->secure_compare($str);
Compare bytestream with L<Mojo::Util/"secure_compare">.
Expand Down Expand Up @@ -274,8 +274,8 @@ L<Mojo::Util/"squish">.
=head2 to_string
my $string = $stream->to_string;
my $string = "$stream";
my $str = $stream->to_string;
my $str = "$stream";
Stringify bytestream.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Content.pm
Expand Up @@ -447,13 +447,13 @@ Extract multipart boundary from C<Content-Type> header.
=head2 build_body
my $string = $content->build_body;
my $str = $content->build_body;
Render whole body.
=head2 build_headers
my $string = $content->build_headers;
my $str = $content->build_headers;
Render all headers.
Expand Down
18 changes: 9 additions & 9 deletions lib/Mojo/Cookie.pm
Expand Up @@ -14,24 +14,24 @@ sub parse { croak 'Method "parse" not implemented by subclass' }
sub to_string { croak 'Method "to_string" not implemented by subclass' }

sub _tokenize {
my ($self, $string) = @_;
my ($self, $str) = @_;

# Nibbling parser
my (@tree, @token);
while ($string =~ s/^\s*([^=;,]+)\s*=?\s*//) {
while ($str =~ s/^\s*([^=;,]+)\s*=?\s*//) {
my $name = $1;

# "expires" is a special case, thank you Netscape...
$string =~ s/^([^;,]+,?[^;,]+)/"$1"/ if $name =~ /^expires$/i;
$str =~ s/^([^;,]+,?[^;,]+)/"$1"/ if $name =~ /^expires$/i;

# Value
my $value;
$value = unquote $1 if $string =~ s/^("(?:\\\\|\\"|[^"])+"|[^;,]+)\s*//;
$value = unquote $1 if $str =~ s/^("(?:\\\\|\\"|[^"])+"|[^;,]+)\s*//;
push @token, [$name, $value];

# Separator
$string =~ s/^\s*;\s*//;
next unless $string =~ s/^\s*,\s*//;
$str =~ s/^\s*;\s*//;
next unless $str =~ s/^\s*,\s*//;
push @tree, [@token];
@token = ();
}
Expand Down Expand Up @@ -84,14 +84,14 @@ following new ones.
=head2 parse
my $cookies = $cookie->parse($string);
my $cookies = $cookie->parse($str);
Parse cookies. Meant to be overloaded in a subclass.
=head2 to_string
my $string = $cookie->to_string;
my $string = "$cookie";
my $str = $cookie->to_string;
my $str = "$cookie";
Render cookie. Meant to be overloaded in a subclass.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Cookie/Request.pm
Expand Up @@ -4,10 +4,10 @@ use Mojo::Base 'Mojo::Cookie';
use Mojo::Util 'quote';

sub parse {
my ($self, $string) = @_;
my ($self, $str) = @_;

my @cookies;
for my $token (map {@$_} $self->_tokenize($string // '')) {
for my $token (map {@$_} $self->_tokenize($str // '')) {
my ($name, $value) = @$token;
next if $name =~ /^\$/;
push @cookies, $self->new(name => $name, value => $value // '');
Expand Down Expand Up @@ -61,7 +61,7 @@ Parse cookies.
=head2 to_string
my $string = $cookie->to_string;
my $str = $cookie->to_string;
Render cookie.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -19,10 +19,10 @@ sub expires {
}

sub parse {
my ($self, $string) = @_;
my ($self, $str) = @_;

my @cookies;
for my $token ($self->_tokenize($string // '')) {
for my $token ($self->_tokenize($str // '')) {
for my $i (0 .. $#$token) {
my ($name, $value) = @{$token->[$i]};

Expand Down Expand Up @@ -156,7 +156,7 @@ Parse cookies.
=head2 to_string
my $string = $cookie->to_string;
my $str = $cookie->to_string;
Render cookie.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Date.pm
Expand Up @@ -131,8 +131,8 @@ Parse date.
=head2 to_string
my $string = $date->to_string;
my $string = "$date";
my $str = $date->to_string;
my $str = "$date";
Render date suitable for HTTP messages.
Expand Down
14 changes: 7 additions & 7 deletions lib/Mojo/Exception.pm
Expand Up @@ -22,19 +22,19 @@ sub to_string {
my $self = shift;

return $self->message unless $self->verbose;
my $string = $self->message ? $self->message : '';
my $str = $self->message ? $self->message : '';

# Before
$string .= $_->[0] . ': ' . $_->[1] . "\n" for @{$self->lines_before};
$str .= $_->[0] . ': ' . $_->[1] . "\n" for @{$self->lines_before};

# Line
$string .= ($self->line->[0] . ': ' . $self->line->[1] . "\n")
$str .= ($self->line->[0] . ': ' . $self->line->[1] . "\n")
if $self->line->[0];

# After
$string .= $_->[0] . ': ' . $_->[1] . "\n" for @{$self->lines_after};
$str .= $_->[0] . ': ' . $_->[1] . "\n" for @{$self->lines_after};

return $string;
return $str;
}

sub trace {
Expand Down Expand Up @@ -198,8 +198,8 @@ Throw exception with stacktrace.
=head2 to_string
my $string = $e->to_string;
my $string = "$e";
my $str = $e->to_string;
my $str = "$e";
Render exception.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Headers.pm
Expand Up @@ -539,7 +539,7 @@ multiline values are disabled by default.
=head2 to_string
my $string = $headers->to_string;
my $str = $headers->to_string;
Turn headers into a string, suitable for HTTP messages.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Home.pm
Expand Up @@ -166,8 +166,8 @@ Portably generate an absolute path for a file relative to the home directory.
=head2 to_string
my $string = $home->to_string;
my $string = "$home";
my $str = $home->to_string;
my $str = "$home";
Home directory.
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojo/JSON.pm
Expand Up @@ -262,9 +262,9 @@ sub _encode_object {
}

sub _encode_string {
my $string = shift;
$string =~ s!([\x00-\x1F\x7F\x{2028}\x{2029}\\"/\b\f\n\r\t])!$REVERSE{$1}!gs;
return "\"$string\"";
my $str = shift;
$str =~ s!([\x00-\x1F\x7F\x{2028}\x{2029}\\"/\b\f\n\r\t])!$REVERSE{$1}!gs;
return "\"$str\"";
}

sub _encode_value {
Expand Down Expand Up @@ -293,9 +293,8 @@ sub _encode_value {
return 'null' unless defined $value;

# Number
my $flags = B::svref_2object(\$value)->FLAGS;
return $value
if $flags & (B::SVp_IOK | B::SVp_NOK) && !($flags & B::SVp_POK);
return 0 + $value
if B::svref_2object(\$value)->FLAGS & (B::SVp_IOK | B::SVp_NOK);

# String
return _encode_string($value);
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -505,7 +505,7 @@ Error and code.
=head2 extract_start_line
my $success = $msg->extract_start_line(\$string);
my $success = $msg->extract_start_line(\$str);
Extract start line from string. Meant to be overloaded in a subclass.
Expand Down Expand Up @@ -602,7 +602,7 @@ Size of the start line in bytes.
=head2 to_string
my $string = $msg->to_string;
my $str = $msg->to_string;
Render whole message.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Request.pm
Expand Up @@ -351,7 +351,7 @@ Access request cookies, usually L<Mojo::Cookie::Request> objects.
=head2 extract_start_line
my $success = $req->extract_start_line(\$string);
my $success = $req->extract_start_line(\$str);
Extract request line from string.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -214,7 +214,7 @@ Generate default response message for code.
=head2 extract_start_line
my $success = $res->extract_start_line(\$string);
my $success = $res->extract_start_line(\$str);
Extract status line from string.
Expand Down
18 changes: 9 additions & 9 deletions lib/Mojo/Parameters.pm
Expand Up @@ -79,16 +79,16 @@ sub params {
}

# Parse string
if (defined(my $string = delete $self->{string})) {
if (defined(my $str = delete $self->{string})) {
my $params = $self->{params} = [];

# Detect pair separator for reconstruction
return $params unless length($string // '');
$self->pair_separator(';') if $string =~ /;/ && $string !~ /\&/;
return $params unless length($str // '');
$self->pair_separator(';') if $str =~ /;/ && $str !~ /\&/;

# W3C suggests to also accept ";" as a separator
my $charset = $self->charset;
for my $pair (split /[\&\;]+/, $string) {
for my $pair (split /[\&\;]+/, $str) {
$pair =~ /^([^=]*)(?:=(.*))?$/;
my $name = $1 // '';
my $value = $2 // '';
Expand Down Expand Up @@ -158,9 +158,9 @@ sub to_string {

# String
my $charset = $self->charset;
if (defined(my $string = $self->{string})) {
$string = encode $charset, $string if $charset;
return url_escape $string, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?';
if (defined(my $str = $self->{string})) {
$str = encode $charset, $str if $charset;
return url_escape $str, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?';
}

# Build pairs
Expand Down Expand Up @@ -326,8 +326,8 @@ the parameters.
=head2 to_string
my $string = $params->to_string;
my $string = "$params";
my $str = $params->to_string;
my $str = "$params";
Turn parameters into a string.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Path.pm
Expand Up @@ -245,7 +245,7 @@ Parse path.
=head2 to_abs_string
my $string = $path->to_abs_string;
my $str = $path->to_abs_string;
Turn path into an absolute string.
Expand Down Expand Up @@ -288,8 +288,8 @@ Turn path into a route.
=head2 to_string
my $string = $path->to_string;
my $string = "$path";
my $str = $path->to_string;
my $str = "$path";
Turn path into a string.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Template.pm
Expand Up @@ -340,14 +340,14 @@ Mojo::Template - Perl-ish templates!
# More advanced
my $output = $mt->render(<<'EOF', 23, 'foo bar');
% my ($number, $text) = @_;
% my ($num, $text) = @_;
%= 5 * 5
<!DOCTYPE html>
<html>
<head><title>More advanced</title></head>
<body>
test 123
foo <% my $i = $number + 2; %>
foo <% my $i = $num + 2; %>
% for (1 .. 23) {
* some text <%= $i++ %>
% }
Expand Down

0 comments on commit 9f75c27

Please sign in to comment.