| 1 | package Linux::Event::Stream::Codec::Line; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | ▶ | sub new ($class, %opt) { | |||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | my $chomp = delete $opt{chomp} // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $opt{"chomp"} // 1
MC/DC0%0/1
delete $opt{"chomp"} // 1
delete $opt{"chomp"}
Truth table50%1/2
A
A= delete $opt{"chomp"}
| |||||||||||||||||||||||||||||
| 12 | 3 | my $max_line = delete $opt{max_line}; | |||||||||||||||||||||||||||
| 13 | 3 | ▶ | my $append_nl = delete $opt{append_newline} // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $opt{"append_newline"} // 1
MC/DC0%0/1
delete $opt{"append_newline"} // 1
delete $opt{"append_newline"}
Truth table50%1/2
A
A= delete $opt{"append_newline"}
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | croak 'unknown options: ' . join(', ', sort keys %opt) if %opt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | return bless { | |||||||||||||||||||||||||||
| 18 | chomp => $chomp, | ||||||||||||||||||||||||||||
| 19 | max_line => $max_line, | ||||||||||||||||||||||||||||
| 20 | append_newline => $append_nl, | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | decode => \&_decode, | ||||||||||||||||||||||||||||
| 23 | encode => \&_encode, | ||||||||||||||||||||||||||||
| 24 | }, $class; | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ▶ | sub _decode ($self, $inref, $outref) { | |||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=7 SCAR=21.5
| |||||||||||||||||||||||||||||
| 28 | # Consume complete lines ending with "\n". | ||||||||||||||||||||||||||||
| 29 | 2 | while (1) { | |||||||||||||||||||||||||||
| 30 | 5 | my $idx = index($$inref, "\n"); | |||||||||||||||||||||||||||
| 31 | 5 | ▶ | last if $idx < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | my $max = $self->{max_line}; | |||||||||||||||||||||||||||
| 34 | 3 | ▶ | if (defined($max) && $idx + 1 > $max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $max and $idx + 1 > $max
MC/DC0%0/2
defined $max and $idx + 1 > $max
defined $max
$idx + 1 > $max
Truth table33%1/3
A and B
A= defined $maxB= $idx + 1 > $max
| |||||||||||||||||||||||||||||
| 35 | 0 | return (0, "line too long (>$max bytes)"); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | my $line = substr($$inref, 0, $idx + 1, ''); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | if ($self->{chomp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | substr($line, -1, 1, '') if length($line) && substr($line, -1, 1) eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $line and substr($line, -1, 1) eq "\n"
MC/DC0%0/2
length $line and substr($line, -1, 1) eq "\n"
length $line
substr($line, -1, 1) eq "\n"
Truth table33%1/3
A and B
A= length $lineB= substr($line, -1, 1) eq "\n"
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | push @$outref, $line; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | return 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ▶ | sub _encode ($self, $msg) { | |||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=5 SCAR=18.9
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | $msg = '' if !defined $msg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | $msg = "$msg"; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | if ($self->{append_newline}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | return $msg if length($msg) && substr($msg, -1, 1) eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $msg and substr($msg, -1, 1) eq "\n"
MC/DC0%0/2
length $msg and substr($msg, -1, 1) eq "\n"
length $msg
substr($msg, -1, 1) eq "\n"
Truth table33%1/3
A and B
A= length $msgB= substr($msg, -1, 1) eq "\n"
| |||||||||||||||||||||||||||||
| 56 | 1 | return $msg . "\n"; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | return $msg; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 7 | 1; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | __END__ | ||||||||||||||||||||||||||||