Skip to content

Commit c642588

Browse files
committed
fixing performance bug (see #64) in PHP implementation
1 parent b30227c commit c642588

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

minify.json.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*! JSON.minify()
4-
v0.1 (c) Kyle Simpson
4+
v2.0 (c) Kyle Simpson
55
MIT License
66
*/
77

@@ -10,7 +10,7 @@ function json_minify($json) {
1010
$in_string = false;
1111
$in_multiline_comment = false;
1212
$in_singleline_comment = false;
13-
$tmp; $tmp2; $new_str = array(); $ns = 0; $from = 0; $lc; $rc; $lastIndex = 0;
13+
$tmp; $tmp2; $new_str = array(); $ns = 0; $from = 0; $prevFrom = 0; $lc; $rc; $lastIndex = 0;
1414

1515
while (preg_match($tokenizer,$json,$tmp,PREG_OFFSET_CAPTURE,$lastIndex)) {
1616
$tmp = $tmp[0];
@@ -24,10 +24,11 @@ function json_minify($json) {
2424
}
2525
$new_str[] = $tmp2;
2626
}
27+
$prevFrom = $from;
2728
$from = $lastIndex;
2829

2930
if ($tmp[0] == "\"" && !$in_multiline_comment && !$in_singleline_comment) {
30-
preg_match("/(\\\\)*$/",$lc,$tmp2);
31+
preg_match("/(\\\\)*$/",$lc,$tmp2,PREG_PATTERN_ORDER,$prevFrom);
3132
if (!$in_string || !$tmp2 || (strlen($tmp2[0]) % 2) == 0) { // start of string with ", or unescaped " character found to end string
3233
$in_string = !$in_string;
3334
}
@@ -54,4 +55,4 @@ function json_minify($json) {
5455
return implode("",$new_str);
5556
}
5657

57-
?>
58+
?>

0 commit comments

Comments
 (0)