| 1 | package JSON::Lines; | ||||||||||||||||||||||||||||
| 2 | 16 | ▶ | use 5.006; use strict; use warnings; our $VERSION = '1.12'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use re 'eval'; use Cpanel::JSON::XS; use base 'Import::Export'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | our ($JSON, $LINES, $STRING, %EX); | |||||||||||||||||||||||||||
| 5 | 16 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | $JSON = Cpanel::JSON::XS->new; | |||||||||||||||||||||||||||
| 7 | 16 | $STRING = qr{ " (?> (?: [^"\\]++ | \\. )*+ ) " }x; | |||||||||||||||||||||||||||
| 8 | 16 | $LINES = qr{ ( [\[\{] (?> (?: (?> [^\[\]\{\}"]++ ) | $STRING | (??{ $LINES }) )*+ ) [\]\}] ) }x; | |||||||||||||||||||||||||||
| 9 | 16 | %EX = ( | |||||||||||||||||||||||||||
| 10 | jsonl => [qw/all/] | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | 4 | ▶ | sub jsonl { | ||||||||||||||||||||||||||
|
Subroutine
jsonl: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my %args = (scalar @_ == 1 ? %{$_[0]} : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 4 | my $self = __PACKAGE__->new(%args); | |||||||||||||||||||||||||||
| 16 | return $args{file} | ||||||||||||||||||||||||||||
| 17 | ? $self->encode_file($args{file}, $args{data}) | ||||||||||||||||||||||||||||
| 18 | : $self->encode($args{data}) | ||||||||||||||||||||||||||||
| 19 | 4 | ▶ | if ($args{encode}); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 20 | return $args{file} | ||||||||||||||||||||||||||||
| 21 | ? $self->decode_file($args{file}) | ||||||||||||||||||||||||||||
| 22 | : $self->decode($args{data}) | ||||||||||||||||||||||||||||
| 23 | 2 | ▶ | if ($args{decode}); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 40 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ($pkg, %args) = (shift, scalar @_ == 1 ? %{$_[0]} : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 40 | my $self = bless { headers => [], _buffer => '' }, $pkg; | |||||||||||||||||||||||||||
| 28 | 40 | ▶ | exists $args{$_} && $JSON->$_($args{$_}) for qw/pretty canonical utf8/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 40 | $self->{$_} = $args{$_} for qw/parse_headers error_cb success_cb/; | |||||||||||||||||||||||||||
| 30 | 40 | $self; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 9 | ▶ | sub pretty { | ||||||||||||||||||||||||||
|
Subroutine
pretty: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 9 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 34 | 9 | ▶ | return $JSON->get_indent if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 5 | $JSON->pretty($val); | |||||||||||||||||||||||||||
| 36 | 5 | return $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 7 | ▶ | sub canonical { | ||||||||||||||||||||||||||
|
Subroutine
canonical: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 7 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 40 | 7 | ▶ | return $JSON->get_canonical if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 4 | $JSON->canonical($val); | |||||||||||||||||||||||||||
| 42 | 4 | return $self; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 6 | ▶ | sub utf8 { | ||||||||||||||||||||||||||
|
Subroutine
utf8: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 6 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 46 | 6 | ▶ | return $JSON->get_utf8 if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 3 | $JSON->utf8($val); | |||||||||||||||||||||||||||
| 48 | 3 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 5 | ▶ | sub parse_headers { | ||||||||||||||||||||||||||
|
Subroutine
parse_headers: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 5 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 52 | 5 | ▶ | return $self->{parse_headers} if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 2 | $self->{parse_headers} = $val; | |||||||||||||||||||||||||||
| 54 | 2 | return $self; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 5 | ▶ | sub error_cb { | ||||||||||||||||||||||||||
|
Subroutine
error_cb: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 5 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 58 | 5 | ▶ | return $self->{error_cb} if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 2 | $self->{error_cb} = $val; | |||||||||||||||||||||||||||
| 60 | 2 | return $self; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 5 | ▶ | sub success_cb { | ||||||||||||||||||||||||||
|
Subroutine
success_cb: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 5 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 64 | 5 | ▶ | return $self->{success_cb} if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 2 | $self->{success_cb} = $val; | |||||||||||||||||||||||||||
| 66 | 2 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 18 | ▶ | sub encode { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 18 | ▶ | my ($self, @data) = (shift, scalar @_ == 1 ? @{$_[0]} : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | @data = $self->_parse_headers(@data) | ||||||||||||||||||||||||||||
| 71 | 18 | ▶ | if ($self->{parse_headers}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 18 | my $stream; | |||||||||||||||||||||||||||
| 73 | 18 | for (@data) { | |||||||||||||||||||||||||||
| 74 | 66 | my $json = eval { $JSON->encode($_) }; | |||||||||||||||||||||||||||
| 75 | 66 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($self->{error_cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | $self->{error_cb}->('encode', $@, $_); | |||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 0 | die $@; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 66 | ▶ | $self->{success_cb}->('encode', $json, $_) if $self->{success_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 66 | ▶ | $stream .= $json . ($json =~ m/\n$/ ? "" : "\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 18 | return $self->{stream} = $stream; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 5 | ▶ | sub encode_file { | ||||||||||||||||||||||||||
|
Subroutine
encode_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 5 | my ($self, $file) = (shift, shift); | |||||||||||||||||||||||||||
| 90 | 5 | open my $fh, '>', $file; | |||||||||||||||||||||||||||
| 91 | 5 | print $fh $self->encode(@_); | |||||||||||||||||||||||||||
| 92 | 5 | close $fh; | |||||||||||||||||||||||||||
| 93 | 5 | return $file; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 160 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=13 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 160 | my ($self, $string) = @_; | |||||||||||||||||||||||||||
| 97 | 160 | ▶ | if (defined $self->{_buffer} && length $self->{_buffer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"_buffer"} and length $self->{"_buffer"}
MC/DC50%1/2
defined $self->{"_buffer"} and length $self->{"_buffer"}
defined $self->{"_buffer"}
length $self->{"_buffer"}
Truth table66%2/3
A and B
A= defined $self->{"_buffer"}B= length $self->{"_buffer"}
| |||||||||||||||||||||||||||||
| 98 | 2 | $string = $self->{_buffer} . $string; | |||||||||||||||||||||||||||
| 99 | 2 | $self->{_buffer} = ''; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 160 | my @lines; | |||||||||||||||||||||||||||
| 102 | 160 | my $pos = 0; | |||||||||||||||||||||||||||
| 103 | 160 | my $len = length($string); | |||||||||||||||||||||||||||
| 104 | 160 | ▶ | while ($pos < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 362 | ▶ | if ($string =~ m/\G[\s\n]+/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 138 | $pos = pos($string); | |||||||||||||||||||||||||||
| 107 | 138 | next; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 224 | my $remaining = substr($string, $pos); | |||||||||||||||||||||||||||
| 110 | 224 | my ($obj, $chars_consumed, $regex_matched) = $self->_decode_one($remaining); | |||||||||||||||||||||||||||
| 111 | 224 | ▶ | if (defined $obj) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 112 | 219 | push @lines, $obj; | |||||||||||||||||||||||||||
| 113 | 219 | $pos += $chars_consumed; | |||||||||||||||||||||||||||
| 114 | } elsif ($regex_matched) { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $pos += $chars_consumed if $chars_consumed > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $pos++ if $chars_consumed == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 5 | ▶ | if ($remaining =~ /^[\[\{]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 4 | $self->{_buffer} = $remaining; | |||||||||||||||||||||||||||
| 120 | 4 | last; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 1 | ▶ | if ($string =~ m/\G[^\[\{]+/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 1 | $pos = pos($string); | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | 0 | $pos++; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 220 | pos($string) = $pos; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | @lines = $self->_deparse_headers(@lines) | ||||||||||||||||||||||||||||
| 131 | 160 | ▶ | if ($self->{parse_headers}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 160 | ▶ | return wantarray ? @lines : \@lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 4 | ▶ | sub remaining { | ||||||||||||||||||||||||||
|
Subroutine
remaining: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 4 | ▶ | return $self->{_buffer} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_buffer"} // ""
MC/DC0%0/1
$self->{"_buffer"} // ""
$self->{"_buffer"}
Truth table50%1/2
A
A= $self->{"_buffer"}
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 17 | ▶ | sub clear_buffer { | ||||||||||||||||||||||||||
|
Subroutine
clear_buffer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||
| 140 | 17 | $self->{_buffer} = ''; | |||||||||||||||||||||||||||
| 141 | 17 | return $self; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 2 | ▶ | sub decode_file { | ||||||||||||||||||||||||||
|
Subroutine
decode_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 2 | my ($self, $file) = (shift, shift); | |||||||||||||||||||||||||||
| 145 | 2 | open my $fh, '<', $file; | |||||||||||||||||||||||||||
| 146 | 2 | my $content = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 147 | 2 | close $fh; | |||||||||||||||||||||||||||
| 148 | 2 | return $self->decode($content); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 2 | ▶ | sub add_line { | ||||||||||||||||||||||||||
|
Subroutine
add_line: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 2 | my ($self, $line, $fh) = @_; | |||||||||||||||||||||||||||
| 152 | 2 | ▶ | if (defined $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | print $fh $self->encode([$line]); | |||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||
| 155 | 2 | my $stream = $self->{stream}; | |||||||||||||||||||||||||||
| 156 | 2 | my $add = $self->encode([$line]); | |||||||||||||||||||||||||||
| 157 | 2 | $self->{stream} = $stream . $add; | |||||||||||||||||||||||||||
| 158 | 2 | $self->{stream}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub clear_stream { | ||||||||||||||||||||||||||
|
Subroutine
clear_stream: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 0 | $_[0]->{stream} = ''; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 1 | ▶ | sub get_lines { | ||||||||||||||||||||||||||
|
Subroutine
get_lines: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | 1 | my ($self, $fh, $lines) = @_; | |||||||||||||||||||||||||||
| 166 | 1 | my @lines; | |||||||||||||||||||||||||||
| 167 | 1 | for (1 .. $lines) { | |||||||||||||||||||||||||||
| 168 | 3 | my $line = $self->get_line($fh); | |||||||||||||||||||||||||||
| 169 | 3 | push @lines, $line; | |||||||||||||||||||||||||||
| 170 | 3 | ▶ | last if eof($fh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 1 | ▶ | return wantarray ? @lines : \@lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 17 | ▶ | sub get_line { | ||||||||||||||||||||||||||
|
Subroutine
get_line: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 17 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||
| 176 | 17 | ▶ | $self->{_line_buffer} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_line_buffer"} //= []
MC/DC100%1/1
$self->{"_line_buffer"} //= []
$self->{"_line_buffer"}
Truth table100%2/2
A
A= $self->{"_line_buffer"}
| |||||||||||||||||||||||||||||
| 177 | 17 | ▶ | if (@{$self->{_line_buffer}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 3 | return shift @{$self->{_line_buffer}}; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 14 | my $line = ''; | |||||||||||||||||||||||||||
| 181 | 14 | ▶ | $line .= <$fh> while ($line !~ m/^$LINES/ && !eof($fh)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$line =~ /^$LINES/ or eof $fh
MC/DC100%2/2
$line =~ /^$LINES/ or eof $fh
$line =~ /^$LINES/
eof $fh
Truth table100%3/3
A or B
A= $line =~ /^$LINES/B= eof $fh
| |||||||||||||||||||||||||||||
| 182 | 14 | ▶ | return undef if $line eq '' && eof($fh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line eq "" and eof $fh
MC/DC50%1/2
$line eq "" and eof $fh
$line eq ""
eof $fh
Truth table66%2/3
A and B
A= $line eq ""B= eof $fh
| |||||||||||||||||||||||||||||
| 183 | 12 | my @objects = $self->decode($line); | |||||||||||||||||||||||||||
| 184 | 12 | ▶ | return undef unless @objects; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 12 | my $first = shift @objects; | |||||||||||||||||||||||||||
| 186 | 12 | push @{$self->{_line_buffer}}, @objects; | |||||||||||||||||||||||||||
| 187 | 12 | return $first; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 16 | ▶ | sub get_line_at { | ||||||||||||||||||||||||||
|
Subroutine
get_line_at: called CC=18 SCAR=30.2
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 16 | my ($self, $fh, $index, $seek) = @_; | |||||||||||||||||||||||||||
| 191 | 16 | my $fh_id = fileno($fh); | |||||||||||||||||||||||||||
| 192 | 16 | ▶ | my ($target_line, $target_offset) = $index =~ /:/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | ? split(/:/, $index) | ||||||||||||||||||||||||||||
| 194 | : ($index, 0); | ||||||||||||||||||||||||||||
| 195 | 16 | ▶ | $self->{_line_pos} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_line_pos"} //= {}
MC/DC100%1/1
$self->{"_line_pos"} //= {}
$self->{"_line_pos"}
Truth table100%2/2
A
A= $self->{"_line_pos"}
| |||||||||||||||||||||||||||||
| 196 | 16 | ▶ | $self->{_parsed_lines} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_parsed_lines"} //= {}
MC/DC100%1/1
$self->{"_parsed_lines"} //= {}
$self->{"_parsed_lines"}
Truth table100%2/2
A
A= $self->{"_parsed_lines"}
| |||||||||||||||||||||||||||||
| 197 | 16 | ▶ | if ($seek) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 9 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 199 | 9 | $self->{_line_pos}{$fh_id} = 0; | |||||||||||||||||||||||||||
| 200 | 9 | $self->{_parsed_lines}{$fh_id} = {}; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 16 | ▶ | $self->{_line_pos}{$fh_id} //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_line_pos"}{$fh_id} //= 0
MC/DC0%0/1
$self->{"_line_pos"}{$fh_id} //= 0
$self->{"_line_pos"}{$fh_id}
Truth table50%1/2
A
A= $self->{"_line_pos"}{$fh_id}
| |||||||||||||||||||||||||||||
| 203 | 16 | ▶ | $self->{_parsed_lines}{$fh_id} //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_parsed_lines"}{$fh_id} //= {}
MC/DC0%0/1
$self->{"_parsed_lines"}{$fh_id} //= {}
$self->{"_parsed_lines"}{$fh_id}
Truth table50%1/2
A
A= $self->{"_parsed_lines"}{$fh_id}
| |||||||||||||||||||||||||||||
| 204 | 16 | ▶ | if ($target_line < $self->{_line_pos}{$fh_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 2 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 206 | 2 | $self->{_line_pos}{$fh_id} = 0; | |||||||||||||||||||||||||||
| 207 | 2 | $self->{_parsed_lines}{$fh_id} = {}; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 16 | ▶ | while ($self->{_line_pos}{$fh_id} < $target_line && !eof($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"_line_pos"}{$fh_id} < $target_line and not eof $fh
MC/DC100%2/2
$self->{"_line_pos"}{$fh_id} < $target_line and not eof $fh
$self->{"_line_pos"}{$fh_id} < $target_line
not eof $fh
Truth table100%3/3
A and not(B)
A= $self->{"_line_pos"}{$fh_id} < $target_lineB= not eof $fh
| |||||||||||||||||||||||||||||
| 210 | 19 | <$fh>; | |||||||||||||||||||||||||||
| 211 | 19 | $self->{_line_pos}{$fh_id}++; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 16 | ▶ | return undef if eof($fh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 15 | ▶ | if (exists $self->{_parsed_lines}{$fh_id}{$target_line}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 0 | my $line_objects = $self->{_parsed_lines}{$fh_id}{$target_line}; | |||||||||||||||||||||||||||
| 216 | 0 | ▶ | return undef unless $line_objects && @$line_objects > $target_offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$line_objects and @$line_objects > $target_offset
MC/DC0%0/2
$line_objects and @$line_objects > $target_offset
$line_objects
@$line_objects > $target_offset
Truth table0%0/3
A and B
A= $line_objectsB= @$line_objects > $target_offset
| |||||||||||||||||||||||||||||
| 217 | 0 | return $line_objects->[$target_offset]; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 15 | my $line = ''; | |||||||||||||||||||||||||||
| 220 | 15 | my $start_line = $self->{_line_pos}{$fh_id}; | |||||||||||||||||||||||||||
| 221 | 15 | ▶ | while ($line !~ m/^$LINES/ && !eof($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line =~ /^$LINES/ or eof $fh
MC/DC50%1/2
$line =~ /^$LINES/ or eof $fh
$line =~ /^$LINES/
eof $fh
Truth table66%2/3
A or B
A= $line =~ /^$LINES/B= eof $fh
| |||||||||||||||||||||||||||||
| 222 | 29 | $line .= <$fh>; | |||||||||||||||||||||||||||
| 223 | 29 | $self->{_line_pos}{$fh_id}++; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 15 | ▶ | return undef unless $line =~ m/^$LINES/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 15 | my @objects = $self->decode($line); | |||||||||||||||||||||||||||
| 227 | 15 | $self->{_parsed_lines}{$fh_id}{$start_line} = \@objects; | |||||||||||||||||||||||||||
| 228 | 15 | ▶ | return undef unless @objects > $target_offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 15 | return $objects[$target_offset]; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | 3 | ▶ | sub get_subset { | ||||||||||||||||||||||||||
|
Subroutine
get_subset: called CC=11 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 3 | my ($self, $fh, $offset, $length, $out_file) = @_; | |||||||||||||||||||||||||||
| 233 | 3 | my $in_fh = 1; | |||||||||||||||||||||||||||
| 234 | 3 | ▶ | if (ref $fh ne 'GLOB') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 1 | my $file = $fh; | |||||||||||||||||||||||||||
| 236 | 1 | ▶ | open my $ffh, '<', $file or die "cannot open file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 1 | $fh = $ffh; | |||||||||||||||||||||||||||
| 238 | 1 | $in_fh = 0; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 3 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 241 | 3 | my @all_objects; | |||||||||||||||||||||||||||
| 242 | 3 | my $line = ''; | |||||||||||||||||||||||||||
| 243 | 3 | ▶ | while (!eof($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 29 | ▶ | do { $line .= <$fh> } while ($line !~ m/^$LINES/ && !eof($fh)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$line =~ /^$LINES/ or eof $fh
MC/DC0%0/2
$line =~ /^$LINES/ or eof $fh
$line =~ /^$LINES/
eof $fh
Truth table33%1/3
A or B
A= $line =~ /^$LINES/B= eof $fh
| |||||||||||||||||||||||||||||
| 245 | 29 | ▶ | if ($line =~ m/^$LINES/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 29 | push @all_objects, $self->decode($line); | |||||||||||||||||||||||||||
| 247 | 29 | $line = ""; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | 3 | ▶ | if (!$in_fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 1 | close $fh; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | 3 | my $end = $length; | |||||||||||||||||||||||||||
| 254 | 3 | ▶ | $end = $#all_objects if $end > $#all_objects; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 3 | my @subset = @all_objects[$offset .. $end]; | |||||||||||||||||||||||||||
| 256 | 3 | ▶ | if ($out_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | open my $cfh, '>', $out_file or die "cannot open file for writing: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | print $cfh $self->encode(\@subset); | |||||||||||||||||||||||||||
| 259 | 0 | close $cfh; | |||||||||||||||||||||||||||
| 260 | 0 | return 1; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 3 | return \@subset; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | 7 | ▶ | sub group_lines { | ||||||||||||||||||||||||||
|
Subroutine
group_lines: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 265 | 7 | my ($self, $fh, $key) = @_; | |||||||||||||||||||||||||||
| 266 | 7 | ▶ | die "Key must be provided for grouping" unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 7 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 268 | 7 | my (%groups, $line_num, $line); | |||||||||||||||||||||||||||
| 269 | 7 | ($line_num, $line) = (0, ''); | |||||||||||||||||||||||||||
| 270 | 7 | ▶ | while (!eof($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 40 | my $start_line = $line_num; | |||||||||||||||||||||||||||
| 272 | 40 | ▶ | while ($line !~ m/^$LINES/ && !eof($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line =~ /^$LINES/ or eof $fh
MC/DC50%1/2
$line =~ /^$LINES/ or eof $fh
$line =~ /^$LINES/
eof $fh
Truth table66%2/3
A or B
A= $line =~ /^$LINES/B= eof $fh
| |||||||||||||||||||||||||||||
| 273 | 152 | $line .= <$fh>; | |||||||||||||||||||||||||||
| 274 | 152 | $line_num++; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | 40 | ▶ | last unless $line =~ m/^$LINES/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 40 | my @objects = $self->decode($line); | |||||||||||||||||||||||||||
| 278 | 40 | $line = ''; | |||||||||||||||||||||||||||
| 279 | 40 | my $obj_offset = 0; | |||||||||||||||||||||||||||
| 280 | 40 | for my $data (@objects) { | |||||||||||||||||||||||||||
| 281 | my $value = ref $key eq 'CODE' | ||||||||||||||||||||||||||||
| 282 | 19 | ? do { local $_ = $data; $key->($data) } | |||||||||||||||||||||||||||
| 283 | 45 | ▶ | : $data->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 45 | ▶ | my $index = @objects > 1 ? "$start_line:$obj_offset" : $start_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 45 | push @{ $groups{$value} }, $index; | |||||||||||||||||||||||||||
| 286 | 45 | $obj_offset++; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 7 | return \%groups; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | 224 | ▶ | sub _decode_one { | ||||||||||||||||||||||||||
|
Subroutine
_decode_one: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 292 | 224 | my ($self, $string) = @_; | |||||||||||||||||||||||||||
| 293 | 224 | ▶ | return (undef, 0, 0) unless $string =~ /^[\[\{]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 223 | ▶ | if ($string =~ m/^($LINES)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 219 | my $json_str = $1; | |||||||||||||||||||||||||||
| 296 | 219 | my $chars_consumed = length($json_str); | |||||||||||||||||||||||||||
| 297 | 219 | my $struct = eval { $JSON->decode($json_str) }; | |||||||||||||||||||||||||||
| 298 | 219 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 0 | return (undef, $chars_consumed, 1); | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | 219 | ▶ | $self->{success_cb}->('decode', $struct, $json_str) if $self->{success_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 219 | return ($struct, $chars_consumed, 1); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | 4 | return (undef, 0, 0); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub _decode_line { | ||||||||||||||||||||||||||
|
Subroutine
_decode_line: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 307 | 0 | my ($self, $line) = @_; | |||||||||||||||||||||||||||
| 308 | 0 | my $struct = eval { $JSON->decode($line) }; | |||||||||||||||||||||||||||
| 309 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if ($self->{error_cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 0 | return $self->{error_cb}->('decode', $@, $line); | |||||||||||||||||||||||||||
| 312 | } else { | ||||||||||||||||||||||||||||
| 313 | 0 | die $@; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | return $self->{success_cb}->('decode', $struct, $line) if $self->{success_cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | return $struct; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 2 | ▶ | sub _parse_headers { | ||||||||||||||||||||||||||
|
Subroutine
_parse_headers: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 320 | 2 | my ($self, @data) = @_; | |||||||||||||||||||||||||||
| 321 | 2 | my @headers = @{ $self->{headers} }; | |||||||||||||||||||||||||||
| 322 | 2 | ▶ | unless (@headers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 2 | ▶ | if (ref $data[0] eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | @headers = @{ shift @data }; | |||||||||||||||||||||||||||
| 325 | } else { | ||||||||||||||||||||||||||||
| 326 | 2 | my %key_map; | |||||||||||||||||||||||||||
| 327 | 2 | for (@data) { | |||||||||||||||||||||||||||
| 328 | 5 | %key_map = (%key_map, %{$_}); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | 2 | @headers = sort keys %key_map; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 2 | $self->{headers} = \@headers; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | 2 | my @body; | |||||||||||||||||||||||||||
| 335 | 2 | for my $d (@data) { | |||||||||||||||||||||||||||
| 336 | push @body, (ref $d eq 'ARRAY') | ||||||||||||||||||||||||||||
| 337 | ? $d | ||||||||||||||||||||||||||||
| 338 | : [ | ||||||||||||||||||||||||||||
| 339 | map { | ||||||||||||||||||||||||||||
| 340 | 5 | ▶ | $d->{$_} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | } @headers | ||||||||||||||||||||||||||||
| 342 | ]; | ||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | return ( | ||||||||||||||||||||||||||||
| 345 | 2 | \@headers, | |||||||||||||||||||||||||||
| 346 | @body | ||||||||||||||||||||||||||||
| 347 | ); | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | 1 | ▶ | sub _deparse_headers { | ||||||||||||||||||||||||||
|
Subroutine
_deparse_headers: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 350 | 1 | my ($self, @data) = @_; | |||||||||||||||||||||||||||
| 351 | 1 | ▶ | return @data unless ref $data[0] eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | 1 | my @headers = @{ shift @data }; | |||||||||||||||||||||||||||
| 353 | 1 | my @body; | |||||||||||||||||||||||||||
| 354 | 1 | for my $d (@data) { | |||||||||||||||||||||||||||
| 355 | 2 | my $i = 0; | |||||||||||||||||||||||||||
| 356 | push @body, (ref $d eq 'HASH') | ||||||||||||||||||||||||||||
| 357 | ? $d | ||||||||||||||||||||||||||||
| 358 | : { | ||||||||||||||||||||||||||||
| 359 | map { | ||||||||||||||||||||||||||||
| 360 | 2 | ▶ | $_ => $d->[$i++] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | } @headers | ||||||||||||||||||||||||||||
| 362 | }; | ||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | 1 | return @body; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | 16 | 1; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | __END__ | ||||||||||||||||||||||||||||