| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | package MOP4Import::Util::CommentedJson; | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Exporter qw/import/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our @EXPORT_OK = qw/strip_json_comments/; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $re_atom = qr{true|false|null}xs; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $re_string = qr{ | |||||||||||||||||||||||||||
| 14 | "(?: [^\\"] | ||||||||||||||||||||||||||||
| 15 | | \\ (?: \\ | ||||||||||||||||||||||||||||
| 16 | | \" | ||||||||||||||||||||||||||||
| 17 | | / | b | f | n | r | t | ||||||||||||||||||||||||||||
| 18 | | u [0-9A-F]{4} | ||||||||||||||||||||||||||||
| 19 | ) | ||||||||||||||||||||||||||||
| 20 | )*+" | ||||||||||||||||||||||||||||
| 21 | }xs; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | our $re_number = qr{ | |||||||||||||||||||||||||||
| 24 | -?+ | ||||||||||||||||||||||||||||
| 25 | (?: 0 | ||||||||||||||||||||||||||||
| 26 | | [1-9] [0-9]*+ | ||||||||||||||||||||||||||||
| 27 | )?+ | ||||||||||||||||||||||||||||
| 28 | (?: | ||||||||||||||||||||||||||||
| 29 | \. [0-9]++ | ||||||||||||||||||||||||||||
| 30 | )?+ | ||||||||||||||||||||||||||||
| 31 | (?: | ||||||||||||||||||||||||||||
| 32 | [eE] [-+]?+ [0-9]*+ | ||||||||||||||||||||||||||||
| 33 | )?+ | ||||||||||||||||||||||||||||
| 34 | }xs; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | our $re_comment1 = qr{//[^\n]*+}xs; | |||||||||||||||||||||||||||
| 37 | 1 | our $re_comment2 = qr{/\*.*?\*/}xs; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | our $re_ALL = qr{ [\[\]\{\},:] | |||||||||||||||||||||||||||
| 40 | | (?<comment> $re_comment1 | $re_comment2) | ||||||||||||||||||||||||||||
| 41 | | $re_atom | ||||||||||||||||||||||||||||
| 42 | | $re_number | ||||||||||||||||||||||||||||
| 43 | | $re_string | ||||||||||||||||||||||||||||
| 44 | }xs; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub strip_json_comments { | ||||||||||||||||||||||||||||
| 47 | 2 | ▶ | my ($pack, $bytes) = @_; | ||||||||||||||||||||||||||
|
Subroutine
strip_json_comments: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 2 | $bytes =~ s/\A\xef\xbb\xbf//; # Strip BOM of utf-8. | |||||||||||||||||||||||||||
| 49 | 2 | my @region = reverse list_json_comments($pack, $bytes); | |||||||||||||||||||||||||||
| 50 | 2 | foreach my $region (@region) { | |||||||||||||||||||||||||||
| 51 | 6 | my ($startPos, $len) = @$region; | |||||||||||||||||||||||||||
| 52 | 6 | substr($bytes, $startPos, $len, ''); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 2 | $bytes; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub list_json_comments { | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | my @pos; | ||||||||||||||||||||||||||
|
Subroutine
list_json_comments: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | while ($_[1] =~ m{\G(?<ws>\s*+)$re_ALL}g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 325 | ▶ | if (defined $+{comment} and my $len = length($+{comment})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $+{"comment"} and my $len = length $+{"comment"}
MC/DC50%1/2
defined $+{"comment"} and my $len = length $+{"comment"}
defined $+{"comment"}
my $len = length $+{"comment"}
Truth table66%2/3
A and B
A= defined $+{"comment"}B= my $len = length $+{"comment"}
| |||||||||||||||||||||||||||||
| 61 | 6 | my $startPos = pos($_[1]) - $len; | |||||||||||||||||||||||||||
| 62 | 6 | push @pos, [$startPos, $len] | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 2 | @pos; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | unless (caller) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | my $pack = __PACKAGE__; | |||||||||||||||||||||||||||
| 70 | 0 | my $method = shift; | |||||||||||||||||||||||||||
| 71 | 0 | my $codec = JSON::MaybeXS->new(allow_nonref => 1); | |||||||||||||||||||||||||||
| 72 | 0 | my @res = $pack->$method(@ARGV); | |||||||||||||||||||||||||||
| 73 | 0 | print $codec->encode(\@res), "\n"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | 1; | |||||||||||||||||||||||||||