lib/JSON_minify.pm

filter: Statementstmt 89.0% 49 / 55 Branchbran 75.0% 30 / 40 Conditioncond 79.6% 47 / 59 MC/DCmcdc 60.0% 18 / 30 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 2 totaltotal 77.8% 148 / 190 CC 41 SCAR 39.1
CC
41
Cov
82%
minify_string
53.4
BEGIN
1.0
BEGIN
1.0
CRAP 49.8
↑ Summary script/json_minify.pl »
1 #!/usr/bin/perl
2 ##
3 ## JSON_minify.pm
4 ## Copyright ©2018 Rémi Cohen-Scali
5 ##
6 ##
7 ## Permission is hereby granted, free of charge, to any person obtaining a copy
8 ## of this software and associated documentation files (the “Software”), to
9 ## deal in the Software without restriction, including without limitation the
10 ## rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 ## sell copies of the Software, and to permit persons to whom the Software is
12 ## furnished to do so, subject to the following conditions:
13 ##
14 ## The above copyright notice and this permission notice shall be included in
15 ## all copies or substantial portions of the Software.
16 ##
17 ## THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 ## IN THE SOFTWARE.
24 ##
25  
26 2 our $VERSION = '1.0';
27  
28 package JSON_minify;
29  
30 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub new {
34 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
35 1 return bless {}, $class;
36 }
37  
38 ##
39 ## minify_string
40 ##
41 ## Minify a json content available in the 2nd input parameter 'input_string'
42 ##
43 sub minify_string {
44 # self param: the object instance
45 5 ▶ my $self = shift;
Subroutine minify_string: called CC=41 SCAR=39.8
Pod undocumented
46 # input_string param: a string containing json
47 5 my $input_string = shift;
48 # strip_space param: a boolean that specify if caller
49 # wants to strip spaces
50 5 ▶ my $strip_space = (@_ ? shift : 1);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 5 5
51  
52 # Returned value: a string containing the minified json
53 5 my $new_str = "";
54 # Current position of processing in input content
55 5 my $index = 0;
56  
57 # Flag indicating if processing is currently inside a multi line comment
58 5 my $in_multi = 0;
59 # Flag indicating if processing is currently inside a single line comment
60 5 my $in_single = 0;
61 # Flag indicating if processing is currently inside a string
62 5 my $in_string = 0;
63 # Flag indicating if processing is currently inside a comment (single or multi)
64 5 my $in_comment = 0;
65  
66 # Let's iterate on every match for each token
67 # This is actually a tokenization
68 # Token of interrest are ", /*, */, //, \n, \r, \t, and space
69 # Regex options are GLOBAL & MULTILINE
70 5 ▶ while ($input_string =~ m/("|(\/\*)|(\*\/)|(\/\/)|[[:space:]])/gm)
Branch: 1 branch
BranchTrueFalseTotal
if ($input_string =~ m[("|(/\*)|(\*/)|(//)|[[:space:]])]gm) 470 5 475
71 {
72 # Initialize context for this match
73 # First the match itself (which is group 1)
74 470 my $token = $1;
75 # Its position in the content
76 470 my $input_pos = pos $input_string;
77 # And its length.
78 # FIXME: Should always match till end of string, so defined not useful anymore
79 # Should replace with:
80 # my $token_len = length $token;
81 470 ▶ my $token_len = defined $token ? length $token : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $token ? : 470 0 470
82  
83 # Integrate in_comment value
84 470 ▶ $in_comment = $in_multi || $in_single;
Condition100%3/3
$in_multi || $in_single
l!l&&r!l&&!r
12319328
MC/DC100%2/2
$in_multi || $in_single
$in_multi $in_single
Truth table100%3/3
A || B
A= $in_multiB= $in_single
ABresult
1X1
011
000
85 # if not in a comment
86 470 ▶ if (!$in_comment)
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_comment) { } 328 142 470
elsif (not $strip_space) { } 0 142 142
87 {
88 # Get the substring between previous pos and now pos
89 328 my $len = ($input_pos - $index - $token_len);
90 # If len < 0, set to 0
91 # FIXME: this was necessary because of a bug later that has been fixed.
92 # I should now be able remove it
93 328 ▶ if ($len == -1) {$len = 0;}
Branch: 1 branch
BranchTrueFalseTotal
if ($len == -1) 0 328 328
94 # Get the sub string
95 328 my $tmp = substr $input_string, $index, $len;
96
97 ## Eventually strip spaces
98 328 ▶ if (! $in_string && $strip_space) {$tmp =~ s/[[:space:]]*//gm;}
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_string and $strip_space) 271 57 328
Condition66%2/3
not $in_string and $strip_space
!ll&&!rl&&r
570271
MC/DC50%1/2
not $in_string and $strip_space
not $in_string $strip_space
Truth table66%2/3
not(A) and B
A= not $in_stringB= $strip_space
ABresult
0X0
100
111
99 # And add it in final result
100 328 $new_str .= $tmp;
101 }
102 # Else if not in comment and not stripping spaces
103 elsif (! $strip_space)
104 {
105 # we replace the substring with spaces
106 0 $new_str .= ' ' x ($input_pos - $index - $token_len);
107 }
108  
109 # As we copied the input chars, let's set index to actual position
110 470 $index = $input_pos;
111 # And get the match in a temporary
112 ##my $val = $token;
113 # If we are closing a string (in string, tok is a dbl quote and not in comment)
114 470 ▶ if ($token eq '"' && ! $in_comment)
Branch: 5 branches
BranchTrueFalseTotal
if ($token eq '"' and not $in_comment) { } 78 392 470
elsif (not $in_string || $in_comment) { } 234 158 392
elsif ($token eq '*/' and $in_multi and not $in_string || $in_single) { } 10 148 158
elsif ($token eq "\r" || $token eq "\n" and not $in_multi and $in_single) { } 6 142 148
elsif (not $in_comment or $token eq ' ' || $token eq "\r" || $token eq "\n" || $token eq "\t" and not $strip_space) { } 16 130 146
Condition100%3/3
$token eq "\"" and not $in_comment
!ll&&!rl&&r
390278
MC/DC100%2/2
$token eq "\"" and not $in_comment
$token eq "\"" not $in_comment
Truth table100%3/3
A and not(B)
A= $token eq "\""B= not $in_comment
ABresult
0X0
100
111
115 {
116 # Get the left context of the match
117 78 my $leftcontext = substr($input_string, 0, $input_pos-1);
118 # Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc)
119 # at the end of the string
120 78 (my $escaped = $leftcontext) =~ m/(\\)*$/;
121 # Get length of match
122 78 ▶ my $escaped_full_len = length $& || '';
Condition100%2/2
length $& || ''
l!l
672
MC/DC100%1/1
length $& || ''
length $&
Truth table100%2/2
A
A= length $&
Aresult
11
00
123  
124 # We got a dbl quote, then
125 # we are either at start of string or unescaped dbl quote (end of string)
126 # if not in string, then a string is starting OR
127 # in string and no backslash at all, we close the string OR
128 # in string and even number of backslash (dbl quote is not backslashed)
129 # then we close the string also
130 78 ▶ if (! $in_string || ! defined $1 || ($escaped_full_len % 2 == 0))
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_string && defined $1 or $escaped_full_len % 2 == 0) 74 4 78
Condition100%6/6
not $in_string && defined $1 or $escaped_full_len % 2 == 0
l!l&&r!l&&!r
7224
$in_string && defined $1
!ll&&!rl&&r
37356
MC/DC100%3/3
not $in_string && defined $1 or $escaped_full_len % 2 == 0
$in_string defined $1 $escaped_full_len % 2 == 0
Truth table100%4/4
not(A && B) or C
A= $in_stringB= defined $1C= $escaped_full_len % 2 == 0
ABCresult
0XX1
10X1
1111
1100
131 {
132 # Then change the in string flag to its negated value
133 74 ▶ $in_string = $in_string ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$in_string ? : 37 37 74
134 }
135 # Let's go back one char to put the dbl quote in the new_string at next iteration
136 78 $index--;
137 }
138 # Else if we are neither in string nor in comment
139 ▶ elsif (! ($in_string || $in_comment))
Condition100%3/3
$in_string || $in_comment
l!l&&r!l&&!r
16142234
MC/DC100%2/2
$in_string || $in_comment
$in_string $in_comment
Truth table100%3/3
A || B
A= $in_stringB= $in_comment
ABresult
1X1
011
000
140 {
141 # Check that token is '/*', then start a multi comment
142 234 ▶ if ($token eq '/*') {$in_multi = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if ($token eq '/*') { } 10 224 234
elsif ($token eq '//') { } 6 218 224
143 # Or check that token is '//', then start a single comment
144 6 elsif ($token eq '//') {$in_single = 1; }
145 }
146 # Else if token is closing multi and we are in multi and we are not
147 # in string neither in single
148 ▶ elsif ($token eq '*/' && $in_multi && !($in_string || $in_single))
Condition66%6/9
$token eq "*/" and $in_multi and not $in_string || $in_single
!ll&&!rl&&r
148010
$token eq "*/" and $in_multi
!ll&&!rl&&r
145310
$in_string || $in_single
l!l&&r!l&&!r
0010
MC/DC50%2/4
$token eq "*/" and $in_multi and not $in_string || $in_single
$token eq "*/" $in_multi $in_string $in_single
Truth table60%3/5
A and B and not(C || D)
A= $token eq "*/"B= $in_multiC= $in_stringD= $in_single
ABCDresult
0XXX0
10XX0
111X0
11010
11001
149 {
150 # Multi line comment reached its end: unset flag in_multi
151 10 $in_multi = 0;
152 # If we d'ont strip spaces, let's add spaces for
153 # token (same length to preserve indentation)
154 10 ▶ if (! $strip_space) {$new_str .= ' ' x length($token);}
Branch: 1 branch
BranchTrueFalseTotal
unless ($strip_space) 0 10 10
155 }
156 # Else if token is some kind of cariage return and we are not in multiline
157 # comment and we are in single line comment
158 ▶ elsif (($token eq "\r" || $token eq "\n") && ! $in_multi && $in_single)
Condition77%7/9
$token eq "\r" || $token eq "\n" and not $in_multi and $in_single
!ll&&!rl&&r
14206
$token eq "\r" || $token eq "\n" and not $in_multi
!ll&&!rl&&r
131116
$token eq "\r" or $token eq "\n"
l!l&&r!l&&!r
017131
MC/DC50%2/4
$token eq "\r" || $token eq "\n" and not $in_multi and $in_single
$token eq "\r" $token eq "\n" not $in_multi $in_single
Truth table42%3/7
A or B and not(C) and D
A= $token eq "\r"B= $token eq "\n"C= not $in_multiD= $in_single
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
159 {
160 # Single line comment reached its end: unset flag in_single
161 6 $in_single = 0;
162 }
163 # Else if we are not in any comment at all, token is any kind of space
164 # and we do not strip space
165 ▶ elsif (! $in_comment ||
Condition80%12/15
not $in_comment or $token eq " " || $token eq "\r" || $token eq "\n" || $token eq "\t" and not $strip_space
l!l&&r!l&&!r
160126
$token eq " " || $token eq "\r" || $token eq "\n" || $token eq "\t" and not $strip_space
!ll&&!rl&&r
41220
$token eq " " or $token eq "\r" or $token eq "\n" or $token eq "\t"
l!l&&r!l&&!r
11394
$token eq " " or $token eq "\r" or $token eq "\n"
l!l&&r!l&&!r
1021113
$token eq " " or $token eq "\r"
l!l&&r!l&&!r
102024
MC/DC16%1/6
not $in_comment or $token eq " " || $token eq "\r" || $token eq "\n" || $token eq "\t" and not $strip_space
not $in_comment $token eq " " $token eq "\r" $token eq "\n" $token eq "\t" not $strip_space
Truth table50%5/10
not(A) or B or C or D or E and not(F)
A= not $in_commentB= $token eq " "C= $token eq "\r"D= $token eq "\n"E= $token eq "\t"F= not $strip_space
ABCDEFresult
1XXXXX1
01XXX11
001XX11
0001X11
0000111
00000X0
01XXX00
001XX00
0001X00
0000100
166 (($token eq ' ' || $token eq "\r" || $token eq "\n" || $token eq "\t") &&
167 ! $strip_space))
168 {
169 # Then add these spaces to the new string
170 16 $new_str .= $token;
171 }
172 # Set in_comment flag value according to its compositing ones
173 470 ▶ $in_comment = $in_single || $in_multi;
Condition100%3/3
$in_single || $in_multi
l!l&&r!l&&!r
19123328
MC/DC100%2/2
$in_single || $in_multi
$in_single $in_multi
Truth table100%3/3
A || B
A= $in_singleB= $in_multi
ABresult
1X1
011
000
174  
175 # If we do not strip spaces
176 470 ▶ if (!$strip_space)
Branch: 1 branch
BranchTrueFalseTotal
unless ($strip_space) 0 470 470
177 {
178 # We need to replace separating tokens with spaces
179 0 ▶ if ($token eq "\r" || $token eq "\n") {$new_str .= $token; }
Branch: 2 branches
BranchTrueFalseTotal
if ($token eq "\r" or $token eq "\n") { } 0 0 0
elsif ($in_comment) { } 0 0 0
Condition0%0/3
$token eq "\r" or $token eq "\n"
l!l&&r!l&&!r
000
MC/DC0%0/2
$token eq "\r" or $token eq "\n"
$token eq "\r" $token eq "\n"
Truth table0%0/3
A or B
A= $token eq "\r"B= $token eq "\n"
ABresult
1X1
011
000
180 # or to replace comments tokens characters with spaces
181 0 elsif ($in_comment) {$new_str .= (' ' x length($token)); }
182 }
183 }
184 5 $new_str .= substr $input_string, $index;
185 }
186  
187 2 1;
188  
189 __END__
↑ Summary script/json_minify.pl »