Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing performance bug in Perl implementation #68

Open
wants to merge 6 commits into
base: perl
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions json_minify/lib/JSON_minify.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## JSON_minify.pm
## Copyright ©2018 Rémi Cohen-Scali
##
## Patched by Kyle Simpson, 2021
##
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the “Software”), to
Expand All @@ -23,7 +25,7 @@
## IN THE SOFTWARE.
##

our $VERSION = '1.1';
our $VERSION = '3.0';

package JSON_minify;

Expand Down Expand Up @@ -53,6 +55,8 @@ sub minify_string {
my $new_str = "";
# Current position of processing in input content
my $index = 0;
# Previous match index
my $prevIndex = 0;

# Flag indicating if processing is currently inside a multi line comment
my $in_multi = 0;
Expand Down Expand Up @@ -95,7 +99,7 @@ sub minify_string {
my $tmp = substr $input_string, $index, $len;

## Eventually strip spaces
if (! $in_string && $strip_space) {$tmp =~ s/[[:space:]]*//gm;}
if (! $in_string && $strip_space) {$tmp =~ s/[[:space:]]+//gm;}
# And add it in final result
$new_str .= $tmp;
}
Expand All @@ -106,6 +110,8 @@ sub minify_string {
$new_str .= ' ' x ($input_pos - $index - $token_len);
}

# Save previous index
$prevIndex = $index;
# As we copied the input chars, let's set index to actual position
$index = $input_pos;
# And get the match in a temporary
Expand All @@ -114,10 +120,10 @@ sub minify_string {
if ($token eq '"' && ! $in_comment)
{
# Get the left context of the match
my $leftcontext = substr($input_string, 0, $input_pos-1);
my $leftcontext = substr($input_string, $prevIndex, $input_pos-1-$prevIndex);
# Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc)
# at the end of the string
(my $escaped = $leftcontext) =~ m/(\\)*$/;
(my $escaped = $leftcontext) =~ m/(\\)+$/;
# Get length of match
my $escaped_full_len = length $& || '';

Expand Down