Skip to content

Commit

Permalink
updated bug reporting FAQ answer
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 1, 2012
1 parent f89d0c3 commit e714df6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 55 deletions.
67 changes: 19 additions & 48 deletions lib/Mojo/JSON.pm
Expand Up @@ -10,21 +10,6 @@ has 'error';
my $FALSE = Mojo::JSON::_Bool->new(0);
my $TRUE = Mojo::JSON::_Bool->new(1);

my $BOM_RE = qr/
(?:
\357\273\277 # UTF-8
|
\377\376\0\0 # UTF-32LE
|
\0\0\376\377 # UTF-32BE
|
\376\377 # UTF-16BE
|
\377\376 # UTF-16LE
)
/x;
my $WHITESPACE_RE = qr/[\x20\x09\x0a\x0d]*/;

# Escaped special character map (with u2028 and u2029)
my %ESCAPE = (
'"' => '"',
Expand All @@ -38,18 +23,19 @@ my %ESCAPE = (
'u2028' => "\x{2028}",
'u2029' => "\x{2029}"
);
my %REVERSE;
for (0x00 .. 0x1F, 0x7F) { $REVERSE{pack 'C', $_} = sprintf '\u%.4X', $_ }
for my $key (keys %ESCAPE) { $REVERSE{$ESCAPE{$key}} = "\\$key" }
my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE;
for (0x00 .. 0x1F, 0x7F) { $REVERSE{pack 'C', $_} //= sprintf '\u%.4X', $_ }

# Unicode encoding detection
my $UTF_PATTERNS = {
"\0\0\0[^\0]" => 'UTF-32BE',
"\0[^\0]\0[^\0]" => 'UTF-16BE',
"[^\0]\0\0\0" => 'UTF-32LE',
"[^\0]\0[^\0]\0" => 'UTF-16LE'
'UTF-32BE' => qr/^\0\0\0[^\0]/,
'UTF-16BE' => qr/^\0[^\0]\0[^\0]/,
'UTF-32LE' => qr/^[^\0]\0\0\0/,
'UTF-16LE' => qr/^[^\0]\0[^\0]\0/
};

my $WHITESPACE_RE = qr/[\x20\x09\x0a\x0d]*/;

# "Hey...That's not the wallet inspector..."
sub decode {
my ($self, $string) = @_;
Expand All @@ -61,19 +47,18 @@ sub decode {
$self->error('Missing or empty input.') and return unless $string;

# Remove BOM
$string =~ s/^$BOM_RE//g;
$string
=~ s/^(?:\357\273\277|\377\376\0\0|\0\0\376\377|\376\377|\377\376)//g;

# Wide characters
$self->error('Wide character in input.') and return
unless utf8::downgrade($string, 1);

# Detect and decode unicode
my $encoding = 'UTF-8';
for my $pattern (keys %$UTF_PATTERNS) {
if ($string =~ /^$pattern/) {
$encoding = $UTF_PATTERNS->{$pattern};
last;
}
for my $name (keys %$UTF_PATTERNS) {
next unless $string =~ $UTF_PATTERNS->{$name};
$encoding = $name;
}
$string = Mojo::Util::decode $encoding, $string;

Expand Down Expand Up @@ -265,33 +250,19 @@ sub _decode_value {
}

sub _encode_array {
my $array = shift;

# Values
my @array;
for my $value (@$array) {
push @array, _encode_values($value);
}

# Stringify
my $string = join ',', @array;
return "[$string]";
return '[' . join(',', map { _encode_values($_) } @{shift()}) . ']';
}

sub _encode_object {
my $object = shift;

# Values
my @values;
for my $key (keys %$object) {
my $name = _encode_string($key);
my $value = _encode_values($object->{$key});
push @values, "$name:$value";
}
# Pairs
my @values =
map { _encode_string($_) . ':' . _encode_values($object->{$_}) }
keys %$object;

# Stringify
my $string = join ',', @values;
return "{$string}";
return '{' . join(',', @values) . '}';
}

sub _encode_string {
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Loader.pm
Expand Up @@ -52,8 +52,7 @@ sub search {
next if -d catfile splitdir($path), $file;

# Module found
my $name = fileparse $file, qr/\.pm/;
my $class = "$namespace\::$name";
my $class = "$namespace\::" . fileparse $file, qr/\.pm/;
push @$modules, $class unless $found{$class};
$found{$class} ||= 1;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojolicious/Guides/FAQ.pod
Expand Up @@ -121,12 +121,10 @@ good, the C<t> directory of this distribution has many good examples for how
to do it right. Writing a test is usually the hardest part of fixing a bug,
so the better your test case the faster it can be fixed. ;)

Once that's done you can contact the developers via via
L<GitHub|https://github.com/kraih/mojo/issues>,
L<mailing list|http://groups.google.com/group/mojolicious> or IRC (C<#mojo>
on C<irc.perl.org>).
Once that's done you can contact the developers via
L<GitHub issue|https://github.com/kraih/mojo/issues>.

If you decide to fix the bug yourself make sure to also take a look at
If you decide to fix the bug yourself, make sure to also take a look at
L<Mojolicious::Guides::CodingGuidelines>.

=head1 MORE
Expand Down

0 comments on commit e714df6

Please sign in to comment.