blib/lib/SION.pm

filter: Statementstmt 99.6% 309 / 310 Branchbran 92.3% 181 / 196 Conditioncond 63.1% 24 / 38 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 100% 58 / 58 Podpod 84.6% 11 / 13 totaltotal 92.6% 590 / 637 CC 113 SCAR 23.2
CC
113
Cov
94%
_encode_value
30.1
_try_number
14.0
_decode_value
13.0
CRAP 10.2
↑ Summary
1 package SION;
2  
3 5 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our $VERSION = '0.01';
16  
17 5 our @EXPORT = qw(encode_sion decode_sion);
18 5 our @EXPORT_OK = qw(true false is_bool);
19  
20 5 ▶use constant INF => 9**9**9;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use constant NAN => 9**9**9 - 9**9**9;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 my $HAS_CORE_BOOL = $] >= 5.035009;
24  
25 3 ▶sub true { JSON::PP::true }
Subroutine true: called CC=1 SCAR=0.0
Pod documented
26 3 ▶sub false { JSON::PP::false }
Subroutine false: called CC=1 SCAR=0.0
Pod documented
27  
28 7 ▶sub is_bool {
Subroutine is_bool: called CC=2 SCAR=6.9
Pod documented
29 7 ▶ return 1 if JSON::PP::is_bool( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if JSON::PP::is_bool($_[0]) 5 2 7
30 2 return _is_core_bool( $_[0] );
31 }
32  
33 89 ▶sub _is_core_bool {
Subroutine _is_core_bool: called CC=2 SCAR=7.0
34 89 ▶ return !!0 unless $HAS_CORE_BOOL;
Branch: 1 branch
BranchTrueFalseTotal
unless $HAS_CORE_BOOL 0 89 89
35 5 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 89 return builtin::is_bool( $_[0] );
37 }
38  
39 #### functional interface
40  
41 5 my $SION_UTF8;
42  
43 2 ▶sub encode_sion { ( $SION_UTF8 ||= SION->new->utf8 )->encode( $_[0] ) }
Subroutine encode_sion: called CC=2 SCAR=9.2
Pod documented
Condition33%1/3
$SION_UTF8 ||= "SION"->new->utf8
l!l&&r!l&&!r
020
MC/DC0%0/2
$SION_UTF8 ||= "SION"->new->utf8
$SION_UTF8 "SION"->new->utf8
Truth table33%1/3
A ||= B
A= $SION_UTF8B= "SION"->new->utf8
ABresult
1X1
011
000
44 3 ▶sub decode_sion { ( $SION_UTF8 ||= SION->new->utf8 )->decode( $_[0] ) }
Subroutine decode_sion: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$SION_UTF8 ||= "SION"->new->utf8
l!l&&r!l&&!r
120
MC/DC0%0/2
$SION_UTF8 ||= "SION"->new->utf8
$SION_UTF8 "SION"->new->utf8
Truth table66%2/3
A ||= B
A= $SION_UTF8B= "SION"->new->utf8
ABresult
1X1
011
000
45  
46 #### constructor and accessors
47  
48 34 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 34 my $class = shift;
50 34 bless {
51 utf8 => 0,
52 ascii => 0,
53 indent => 0,
54 indent_length => 4,
55 space_before => 0,
56 space_after => 0,
57 canonical => 0,
58 allow_nonref => 1,
59 allow_blessed => 0,
60 convert_blessed => 0,
61 max_depth => 512,
62 }, $class;
63 }
64  
65 5 for my $name (
66 qw( utf8 ascii indent space_before space_after canonical
67 allow_nonref allow_blessed convert_blessed )
68 )
69 {
70 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 45 ▶ *{$name} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
72 29 my $self = shift;
73 29 ▶ $self->{$name} = @_ ? ( $_[0] ? 1 : 0 ) : 1;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 2 27 29
$_[0] ? : 0 2 2
74 29 $self;
75 45 };
76 9 ▶ *{"get_$name"} = sub { $_[0]->{$name} };
Subroutine __ANON__: called CC=1 SCAR=0.0
77 }
78  
79 5 ▶sub pretty {
Subroutine pretty: called CC=3 SCAR=11.0
Pod documented
80 5 my ( $self, $enable ) = @_;
81 5 ▶ $enable = 1 unless defined $enable;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enable 4 1 5
82 5 ▶ $enable = $enable ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 4 1 5
83 5 @{$self}{qw(indent space_before space_after)} = ($enable) x 3;
84 5 $self;
85 }
86  
87 3 ▶sub max_depth {
Subroutine max_depth: called CC=2 SCAR=6.9
Pod documented
88 3 my $self = shift;
89 3 ▶ $self->{max_depth} = @_ ? $_[0] : 0x7FFFFFFF;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 1 3
90 3 $self;
91 }
92 2 ▶sub get_max_depth { $_[0]->{max_depth} }
Subroutine get_max_depth: called CC=1 SCAR=0.0
Pod undocumented
93  
94 2 ▶sub indent_length {
Subroutine indent_length: called CC=2 SCAR=6.9
Pod documented
95 2 my $self = shift;
96 2 ▶ $self->{indent_length} = @_ ? $_[0] : 4;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
97 2 $self;
98 }
99 1 ▶sub get_indent_length { $_[0]->{indent_length} }
Subroutine get_indent_length: called CC=1 SCAR=0.0
Pod undocumented
100  
101 #### encoder
102  
103 63 ▶sub encode {
Subroutine encode: called CC=5 SCAR=16.1
Pod documented
104 63 my ( $self, $data ) = @_;
105 63 ▶ if ( !$self->{allow_nonref}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"allow_nonref"} or (ref $data eq "ARRAY" or ref $data eq "HASH")) 1 62 63
Condition83%5/6
$self->{"allow_nonref"} or (ref $data eq "ARRAY" or ref $data eq "HASH")
l!l&&r!l&&!r
6111
ref $data eq "ARRAY" or ref $data eq "HASH"
l!l&&r!l&&!r
101
MC/DC66%2/3
$self->{"allow_nonref"} or (ref $data eq "ARRAY" or ref $data eq "HASH")
$self->{"allow_nonref"} ref $data eq "ARRAY" ref $data eq "HASH"
Truth table75%3/4
A or B or C
A= $self->{"allow_nonref"}B= ref $data eq "ARRAY"C= ref $data eq "HASH"
ABCresult
1XX1
01X1
0011
0000
106 and not( ref $data eq 'ARRAY' or ref $data eq 'HASH' ) )
107 {
108 1 Carp::croak( 'hash- or arrayref expected'
109 . ' (not a simple scalar, use allow_nonref to allow this)' );
110 }
111 62 my $text = $self->_encode_value( $data, 0 );
112 57 ▶ return $self->{utf8} ? Encode::encode( 'UTF-8', $text ) : $text;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'utf8'} ? : 6 51 57
113 }
114  
115 685 ▶sub _encode_value {
Subroutine _encode_value: called CC=30 SCAR=34.0
116 5 ▶ no warnings 'recursion'; # max_depth is the limit, not perl's warning
Subroutine BEGIN: called CC=1 SCAR=0.0
117 685 my ( $self, $v, $depth ) = @_;
118 685 ▶ if ( $depth > $self->{max_depth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > $self->{"max_depth"}) 2 683 685
119 2 Carp::croak( 'SION text or perl structure exceeds maximum nesting'
120 . ' level (max_depth set too low?)' );
121 }
122 683 ▶ if ( my $class = Scalar::Util::blessed($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = builtin::blessed($v)) 25 658 683
123 25 ▶ return ( $v ? 'true' : 'false' ) if $v->isa('JSON::PP::Boolean');
Branch: 2 branches
BranchTrueFalseTotal
if $v->isa("JSON::PP::Boolean") 11 14 25
$v ? : 7 4 11
124 14 ▶ return _data_str( $v->data ) if $v->isa('SION::Data');
Branch: 1 branch
BranchTrueFalseTotal
if $v->isa("SION::Data") 1 13 14
125 13 ▶ return '.Ext("' . MIME::Base64::encode_base64( $v->data, '' ) . '")'
Branch: 1 branch
BranchTrueFalseTotal
if $v->isa("SION::Ext") 4 9 13
126 if $v->isa('SION::Ext');
127 9 ▶ return '.Date(' . _double_str( 0.0 + $v->epoch ) . ')'
Branch: 1 branch
BranchTrueFalseTotal
if $v->isa("SION::Date") 5 4 9
128 if $v->isa('SION::Date');
129 4 ▶ if ( $self->{convert_blessed} and $v->can('TO_SION') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"convert_blessed"} and $v->can("TO_SION")) 1 3 4
Condition66%2/3
$self->{"convert_blessed"} and $v->can("TO_SION")
!ll&&!rl&&r
301
MC/DC50%1/2
$self->{"convert_blessed"} and $v->can("TO_SION")
$self->{"convert_blessed"} $v->can("TO_SION")
Truth table66%2/3
A and B
A= $self->{"convert_blessed"}B= $v->can("TO_SION")
ABresult
0X0
100
111
130 1 return $self->_encode_value( $v->TO_SION, $depth );
131 }
132 3 ▶ return '.Date(' . _double_str( 0.0 + $v->epoch ) . ')'
Branch: 1 branch
BranchTrueFalseTotal
if $v->can("epoch") 1 2 3
133 if $v->can('epoch'); # Time::Piece, DateTime, Time::Moment ...
134 2 ▶ return 'nil' if $self->{allow_blessed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_blessed"} 1 1 2
135 1 Carp::croak( "encountered object '$v', but neither allow_blessed"
136 . ' nor convert_blessed settings are enabled' );
137 }
138 658 my $r = ref $v;
139 658 ▶ if ( $r eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "ARRAY") 538 120 658
140 538 my @elems = map { $self->_encode_value( $_, $depth + 1 ) } @$v;
141 23 return $self->_wrap( \@elems, $depth, '[]' );
142 }
143 120 ▶ if ( $r eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "HASH") 18 102 120
144 18 my @keys = keys %$v;
145 18 ▶ @keys = sort @keys if $self->{canonical};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"canonical"} 16 2 18
146 18 ▶ my $sb = $self->{space_before} ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'space_before'} ? : 7 11 18
147 18 ▶ my $sa = $self->{space_after} ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'space_after'} ? : 7 11 18
148 my @pairs = map {
149 18 $self->_string_or_data($_)
150 . $sb . ':' . $sa
151 63 . $self->_encode_value( $v->{$_}, $depth + 1 )
152 } @keys;
153 18 return $self->_wrap( \@pairs, $depth, '[:]' );
154 }
155 102 ▶ if ( $r eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "SCALAR") 3 99 102
156 3 ▶ return 'true' if defined $$v and $$v eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if defined $$v and $$v eq 1 1 2 3
Condition66%2/3
defined $$v and $$v eq 1
!ll&&!rl&&r
021
MC/DC50%1/2
defined $$v and $$v eq 1
defined $$v $$v eq 1
Truth table66%2/3
A and B
A= defined $$vB= $$v eq 1
ABresult
0X0
100
111
157 2 ▶ return 'false' if defined $$v and ( $$v eq '0' or $$v eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $$v and $$v eq 0 || $$v eq "" 1 1 2
Condition66%4/6
defined $$v and $$v eq 0 || $$v eq ""
!ll&&!rl&&r
011
$$v eq 0 or $$v eq ""
l!l&&r!l&&!r
101
MC/DC33%1/3
defined $$v and $$v eq 0 || $$v eq ""
defined $$v $$v eq 0 $$v eq ""
Truth table50%2/4
A and B or C
A= defined $$vB= $$v eq 0C= $$v eq ""
ABCresult
0XX0
1000
11X1
1011
158 1 Carp::croak('cannot encode reference to scalar unless it is \0 or \1');
159 }
160 99 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 1 98 99
161 1 Carp::croak("cannot encode reference to $r");
162 }
163 # plain scalar
164 98 ▶ return 'nil' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 11 87 98
165 87 ▶ return ( $v ? 'true' : 'false' ) if _is_core_bool($v);
Branch: 2 branches
BranchTrueFalseTotal
if _is_core_bool($v) 2 85 87
$v ? : 1 1 2
166 85 my $flags = B::svref_2object( \$v )->FLAGS;
167 85 ▶ if ( $flags & ( B::SVp_IOK() | B::SVp_NOK() )
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 12288 and not $flags & 16384) 54 31 85
Condition66%2/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
31054
MC/DC50%1/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
168 and !( $flags & B::SVp_POK() ) )
169 {
170 54 ▶ return $flags & B::SVp_NOK() ? _double_str($v) : "$v";
Branch: 1 branch
BranchTrueFalseTotal
$flags & 8192 ? : 21 33 54
171 }
172 31 return $self->_string_or_data($v);
173 }
174  
175 41 ▶sub _wrap {
Subroutine _wrap: called CC=4 SCAR=13.9
176 41 my ( $self, $elems, $depth, $empty ) = @_;
177 41 ▶ return $empty unless @$elems;
Branch: 1 branch
BranchTrueFalseTotal
unless @$elems 9 32 41
178 32 ▶ if ( $self->{indent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"indent"}) 8 24 32
179 8 my $in = ' ' x ( $self->{indent_length} * ( $depth + 1 ) );
180 8 my $out = ' ' x ( $self->{indent_length} * $depth );
181 8 return "[\n" . join( ",\n", map { $in . $_ } @$elems ) . "\n$out]";
182 }
183 24 ▶ my $sep = ',' . ( $self->{space_after} ? ' ' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'space_after'} ? : 2 22 24
184 24 return '[' . join( $sep, @$elems ) . ']';
185 }
186  
187 31 ▶sub _double_str {
Subroutine _double_str: called CC=4 SCAR=13.9
188 31 my $v = shift;
189 31 ▶ return 'nan' if $v != $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v != $v 1 30 31
190 30 ▶ return 'inf' if $v == INF;
Branch: 1 branch
BranchTrueFalseTotal
if $v == "Inf" 1 29 30
191 29 ▶ return '-inf' if $v == - INF();
Branch: 1 branch
BranchTrueFalseTotal
if $v == "-Inf" 1 28 29
192 28 return sprintf '%a', $v;
193 }
194  
195 5 ▶sub _data_str {
Subroutine _data_str: called CC=1 SCAR=0.0
196 5 my $v = shift;
197 5 return '.Data("' . MIME::Base64::encode_base64( $v, '' ) . '")';
198 }
199  
200 94 ▶sub _string_or_data {
Subroutine _string_or_data: called CC=3 SCAR=11.0
201 94 my ( $self, $v ) = @_;
202 94 ▶ return $self->_string($v) if utf8::is_utf8($v);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($v) 73 21 94
203 21 my $chars = eval {
204 21 Encode::decode( 'UTF-8', $v, Encode::FB_CROAK | Encode::LEAVE_SRC );
205 };
206 21 ▶ return defined $chars ? $self->_string($chars) : _data_str($v);
Branch: 1 branch
BranchTrueFalseTotal
defined $chars ? : 17 4 21
207 }
208  
209 5 my %ESC = (
210 "\x22" => '\"',
211 "\x5c" => '\\\\',
212 "\x08" => '\b',
213 "\x09" => '\t',
214 "\x0a" => '\n',
215 "\x0c" => '\f',
216 "\x0d" => '\r',
217 );
218  
219 90 ▶sub _string {
Subroutine _string: called CC=3 SCAR=11.0
220 90 my ( $self, $s ) = @_;
221 90 ▶ $s =~ s/([\x22\x5c\x00-\x1f])/$ESC{$1} || sprintf '\u%04x', ord $1/ge;
Condition66%2/3
$ESC{$1} || sprintf('\\u%04x', ord $1)
l!l&&r!l&&!r
410
MC/DC0%0/2
$ESC{$1} || sprintf('\\u%04x', ord $1)
$ESC{$1} sprintf('\\u%04x', ord $1)
Truth table66%2/3
A || B
A= $ESC{$1}B= sprintf('\\u%04x', ord $1)
ABresult
1X1
011
000
222 90 ▶ if ( $self->{ascii} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ascii"}) 3 87 90
223 3 $s =~ s/([^\x00-\x7f])/_ascii_escape(ord $1)/ge;
224 }
225 90 return qq("$s");
226 }
227  
228 6 ▶sub _ascii_escape {
Subroutine _ascii_escape: called CC=2 SCAR=6.9
229 6 my $cp = shift;
230 6 ▶ return sprintf '\u%04x', $cp if $cp <= 0xFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $cp <= 65535 4 2 6
231 2 $cp -= 0x10000;
232 2 return sprintf '\u%04x\u%04x', 0xD800 + ( $cp >> 10 ),
233 0xDC00 + ( $cp & 0x3FF );
234 }
235  
236 #### decoder
237  
238 5 our $_TEXT;
239  
240 71 ▶sub decode {
Subroutine decode: called CC=7 SCAR=19.7
Pod documented
241 71 my ( $self, $input ) = @_;
242 71 ▶ Carp::croak('decode: input is undefined') unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 71 71
243 71 ▶ if ( $self->{utf8} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"utf8"}) 6 65 71
244 6 $input = Encode::decode( 'UTF-8', $input,
245 Encode::FB_CROAK | Encode::LEAVE_SRC );
246 }
247 71 local $_TEXT = $input;
248 71 pos($_TEXT) = 0;
249 71 my $value = $self->_decode_value(0);
250 57 _skip_ws();
251 57 ▶ if ( pos($_TEXT) != length($_TEXT) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $SION::_TEXT != length $SION::_TEXT) 2 55 57
252 2 $self->_croak('garbage after SION value');
253 }
254 55 ▶ if ( !$self->{allow_nonref}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"allow_nonref"} or (ref $value eq "ARRAY" or ref $value eq "HASH")) 1 54 55
Condition50%3/6
$self->{"allow_nonref"} or (ref $value eq "ARRAY" or ref $value eq "HASH")
l!l&&r!l&&!r
5401
ref $value eq "ARRAY" or ref $value eq "HASH"
l!l&&r!l&&!r
001
MC/DC33%1/3
$self->{"allow_nonref"} or (ref $value eq "ARRAY" or ref $value eq "HASH")
$self->{"allow_nonref"} ref $value eq "ARRAY" ref $value eq "HASH"
Truth table50%2/4
A or B or C
A= $self->{"allow_nonref"}B= ref $value eq "ARRAY"C= ref $value eq "HASH"
ABCresult
1XX1
01X1
0011
0000
255 and not( ref $value eq 'ARRAY' or ref $value eq 'HASH' ) )
256 {
257 1 Carp::croak( 'SION text must be an array or dictionary'
258 . ' (but found number, string, or atom,'
259 . ' use allow_nonref to allow this)' );
260 }
261 54 return $value;
262 }
263  
264 14 ▶sub _croak {
Subroutine _croak: called CC=4 SCAR=14.1
265 14 my ( $self, $msg ) = @_;
266 14 ▶ my $pos = pos($_TEXT) // 0;
Condition50%1/2
pos $SION::_TEXT // 0
l!l
140
MC/DC0%0/1
pos $SION::_TEXT // 0
pos $SION::_TEXT
Truth table50%1/2
A
A= pos $SION::_TEXT
Aresult
11
00
267 14 my $len = length($_TEXT) - $pos;
268 14 ▶ my $near =
Branch: 2 branches
BranchTrueFalseTotal
$len <= 0 ? : 5 9 14
$len > 32 ? : 0 9 9
269 $len <= 0 ? '(end of string)'
270 : $len > 32 ? substr( $_TEXT, $pos, 32 ) . '...'
271 : substr( $_TEXT, $pos );
272 14 Carp::croak( sprintf '%s, at character offset %d (before "%s")',
273 $msg, $pos, $near );
274 }
275  
276 806 ▶sub _skip_ws {
Subroutine _skip_ws: called CC=1 SCAR=0.0
277 806 $_TEXT =~ /\G(?:\s+|\/\/[^\n\r]*)+/gc;
278 806 return;
279 }
280  
281 311 ▶sub _decode_value {
Subroutine _decode_value: called CC=13 SCAR=25.7
282 5 ▶ no warnings 'recursion'; # max_depth is the limit, not perl's warning
Subroutine BEGIN: called CC=1 SCAR=0.0
283 311 my ( $self, $depth ) = @_;
284 311 ▶ if ( $depth > $self->{max_depth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > $self->{"max_depth"}) 1 310 311
285 1 Carp::croak( 'SION text or perl structure exceeds maximum nesting'
286 . ' level (max_depth set too low?)' );
287 }
288 310 _skip_ws();
289 310 ▶ if ( $_TEXT =~ /\G\[/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G\[/cgu) 56 254 310
290 56 return $self->_decode_collection( $depth + 1 );
291 }
292 254 ▶ if ( $_TEXT =~ /\G"((?:\\.|[^\\"])*+)"/gcs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G"((?:\\.|[^\\"])*+)"/cgsu) 116 138 254
293 116 return _unescape("$1");
294 }
295 138 ▶ return undef if $_TEXT =~ /\Gnil\b/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $SION::_TEXT =~ /\Gnil\b/cgu 16 122 138
296 122 ▶ return JSON::PP::true if $_TEXT =~ /\Gtrue\b/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $SION::_TEXT =~ /\Gtrue\b/cgu 11 111 122
297 111 ▶ return JSON::PP::false if $_TEXT =~ /\Gfalse\b/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $SION::_TEXT =~ /\Gfalse\b/cgu 5 106 111
298 106 ▶ if ( $_TEXT =~ /\G\.Date\(/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G\.Date\(/cgu) 9 97 106
299 9 _skip_ws();
300 9 my $n = _try_number();
301 9 ▶ $self->_croak('number expected while parsing .Date') unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 2 7 9
302 7 _skip_ws();
303 7 ▶ $_TEXT =~ /\G\)/gc or $self->_croak('")" expected while parsing .Date');
Branch: 1 branch
BranchTrueFalseTotal
$SION::_TEXT =~ /\G\)/cgu or $self->_croak("\")\" expected while parsing .Date") 7 0 7
304 7 return SION::Date->new($n);
305 }
306 97 ▶ if ( $_TEXT =~ /\G\.Data\("([0-9A-Za-z+\/]*={0,3})"\)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ m[\G\.Data\("([0-9A-Za-z+/]*={0,3})"\)]cgu) 5 92 97
307 5 return MIME::Base64::decode_base64($1);
308 }
309 92 ▶ if ( $_TEXT =~ /\G\.Ext\("([0-9A-Za-z+\/]*={0,3})"\)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ m[\G\.Ext\("([0-9A-Za-z+/]*={0,3})"\)]cgu) 5 87 92
310 5 return SION::Ext->new( MIME::Base64::decode_base64($1) );
311 }
312 87 my $n = _try_number();
313 87 ▶ return $n if defined $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n 80 7 87
314 7 $self->_croak( 'malformed SION string, neither array, dictionary,'
315 . ' number, string or atom' );
316 }
317  
318 96 ▶sub _try_number {
Subroutine _try_number: called CC=14 SCAR=26.4
319 96 ▶ if ( $_TEXT =~
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G([+-]?)0[xX]([0-9A-Fa-f]+)(?:\.([0-9A-Fa-f]*))?[pP]([+-]?[0-9]+)/cgu) 22 74 96
320 /\G([+-]?)0[xX]([0-9A-Fa-f]+)(?:\.([0-9A-Fa-f]*))?[pP]([+-]?[0-9]+)/gc )
321 {
322 22 return _mk_hexfloat( $1, $2, $3, $4 );
323 }
324 74 ▶ if ( $_TEXT =~ /\G([+-]?)0[xX]([0-9A-Fa-f]+)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G([+-]?)0[xX]([0-9A-Fa-f]+)/cgu) 3 71 74
325 3 my $v = hex $2;
326 3 ▶ return $1 eq '-' ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 2 1 3
327 }
328 71 ▶ if ( $_TEXT =~ /\G([+-]?)0[oO]([0-7]+)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G([+-]?)0[oO]([0-7]+)/cgu) 1 70 71
329 1 my $v = oct "0$2";
330 1 ▶ return $1 eq '-' ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 1 1
331 }
332 70 ▶ if ( $_TEXT =~ /\G([+-]?)0[bB]([01]+)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G([+-]?)0[bB]([01]+)/cgu) 1 69 70
333 1 my $v = oct "0b$2";
334 1 ▶ return $1 eq '-' ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 1 1
335 }
336 69 ▶ if ( $_TEXT =~
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G([+-]?)((?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/cgu) 56 13 69
337 /\G([+-]?)((?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/gc )
338 {
339 56 my ( $sign, $body ) = ( $1, $2 );
340 56 ▶ my $v = $body =~ /[.eE]/ ? 0.0 + $body : 0 + $body;
Branch: 1 branch
BranchTrueFalseTotal
$body =~ /[.eE]/u ? : 7 49 56
341 56 ▶ return $sign eq '-' ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 5 51 56
342 }
343 13 ▶ return NAN if $_TEXT =~ /\G[+-]?[Nn]a[Nn]\b/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $SION::_TEXT =~ /\G[+-]?[Nn]a[Nn]\b/cgu 1 12 13
344 12 ▶ if ( $_TEXT =~ /\G([+-]?)[Ii]nf(?:inity)?\b/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G([+-]?)[Ii]nf(?:inity)?\b/cgu) 3 9 12
345 3 ▶ return $1 eq '-' ? - INF() : INF;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 1 2 3
346 }
347 9 return undef;
348 }
349  
350 22 ▶sub _mk_hexfloat {
Subroutine _mk_hexfloat: called CC=6 SCAR=18.3
351 22 my ( $sign, $int, $frac, $exp ) = @_;
352 22 ▶ $frac = '' unless defined $frac;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frac 16 6 22
353 22 my $mant = 0.0;
354 22 $mant = $mant * 16.0 + hex($_) for split //, $int . $frac;
355 22 my $e = $exp - 4 * length $frac;
356 22 ▶ $e = $e > 99_999 ? 99_999 : $e < -99_999 ? -99_999 : $e;
Branch: 2 branches
BranchTrueFalseTotal
$e > 99999 ? : 0 22 22
$e < -99999 ? : 0 22 22
357 # ldexp() keeps the result an NV so Double survives a round trip
358 22 my $v = POSIX::ldexp( $mant, $e );
359 22 ▶ return $sign eq '-' ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 0 22 22
360 }
361  
362 5 my %UNESC = (
363 '"' => '"',
364 '\\' => '\\',
365 '/' => '/',
366 "'" => "'",
367 '0' => "\0",
368 'b' => "\b",
369 'f' => "\f",
370 'n' => "\n",
371 'r' => "\r",
372 't' => "\t",
373 );
374  
375 116 ▶sub _unescape {
Subroutine _unescape: called CC=4 SCAR=13.9
376 116 my $s = shift;
377 5 ▶ no warnings 'surrogate', 'non_unicode';
Subroutine BEGIN: called CC=1 SCAR=0.0
378 # surrogate pairs first
379 116 $s =~ s{\\[uU]([Dd][89ABab][0-9A-Fa-f]{2})\\[uU]([Dd][C-Fc-f][0-9A-Fa-f]{2})}
380 1 {chr(0x10000 + ((hex($1) - 0xD800) << 10) + (hex($2) - 0xDC00))}ge;
381 116 $s =~ s{\\(?:[uU]\{([0-9A-Fa-f]{1,6})\}|[uU]([0-9A-Fa-f]{4})|(.))}
382 {
383 defined $1 ? chr hex $1
384 : defined $2 ? chr hex $2
385 9 ▶ : defined $UNESC{$3} ? $UNESC{$3}
Branch: 3 branches
BranchTrueFalseTotal
defined $1 ? : 1 8 9
defined $2 ? : 3 5 8
defined $UNESC{$3} ? : 4 1 5
386 : Carp::croak(qq(illegal backslash escape: \\$3 in SION string))
387 }sge;
388 115 return $s;
389 }
390  
391 56 ▶sub _decode_collection {
Subroutine _decode_collection: called CC=12 SCAR=24.9
392 56 my ( $self, $depth ) = @_;
393 56 _skip_ws();
394 56 ▶ if ( $_TEXT =~ /\G:/gc ) { # [:] -- empty dictionary
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G:/cgu) 6 50 56
395 6 _skip_ws();
396 6 ▶ $_TEXT =~ /\G\]/gc or $self->_croak('"]" expected after "[:"');
Branch: 1 branch
BranchTrueFalseTotal
$SION::_TEXT =~ /\G\]/cgu or $self->_croak("\"]\" expected after \"[:\"") 6 0 6
397 6 return {};
398 }
399 50 ▶ return [] if $_TEXT =~ /\G\]/gc; # [] -- empty array
Branch: 1 branch
BranchTrueFalseTotal
if $SION::_TEXT =~ /\G\]/cgu 5 45 50
400 45 my $first = $self->_decode_value($depth);
401 42 _skip_ws();
402 42 ▶ if ( $_TEXT =~ /\G:/gc ) { # dictionary
Branch: 1 branch
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G:/cgu) 22 20 42
403 22 my %hash;
404 22 $hash{ $self->_key_str($first) } = $self->_decode_value($depth);
405 21 while (1) {
406 89 _skip_ws();
407 89 ▶ if ( $_TEXT =~ /\G,/gc ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G,/cgu) { } 69 20 89
elsif ($SION::_TEXT =~ /\G\]/cgu) { } 19 1 20
408 69 _skip_ws();
409 69 ▶ last if $_TEXT =~ /\G\]/gc; # trailing comma
Branch: 1 branch
BranchTrueFalseTotal
if $SION::_TEXT =~ /\G\]/cgu 1 68 69
410 68 my $k = $self->_decode_value($depth);
411 68 _skip_ws();
412 68 ▶ $_TEXT =~ /\G:/gc
Branch: 1 branch
BranchTrueFalseTotal
$SION::_TEXT =~ /\G:/cgu or $self->_croak("\":\" expected while parsing dictionary") 68 0 68
413 or $self->_croak('":" expected while parsing dictionary');
414 68 $hash{ $self->_key_str($k) } = $self->_decode_value($depth);
415 }
416 19 elsif ( $_TEXT =~ /\G\]/gc ) { last }
417 else {
418 1 $self->_croak('"," or "]" expected while parsing dictionary');
419 }
420 }
421 20 return \%hash;
422 }
423 20 my @array = ($first); # array
424 20 while (1) {
425 55 _skip_ws();
426 55 ▶ if ( $_TEXT =~ /\G,/gc ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($SION::_TEXT =~ /\G,/cgu) { } 38 17 55
elsif ($SION::_TEXT =~ /\G\]/cgu) { } 15 2 17
427 38 _skip_ws();
428 38 ▶ last if $_TEXT =~ /\G\]/gc; # trailing comma
Branch: 1 branch
BranchTrueFalseTotal
if $SION::_TEXT =~ /\G\]/cgu 1 37 38
429 37 push @array, $self->_decode_value($depth);
430 }
431 15 elsif ( $_TEXT =~ /\G\]/gc ) { last }
432 else {
433 2 $self->_croak('"," or "]" expected while parsing array');
434 }
435 }
436 16 return \@array;
437 }
438  
439 # SION allows any value as a dictionary key; perl hash keys are strings,
440 # so non-string keys are stringified.
441 89 ▶sub _key_str {
Subroutine _key_str: called CC=6 SCAR=18.8
442 89 my ( $self, $k ) = @_;
443 89 ▶ return 'nil' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 1 88 89
444 88 ▶ if ( Scalar::Util::blessed($k) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($k)) 1 87 88
445 1 ▶ return $k ? 'true' : 'false' if $k->isa('JSON::PP::Boolean');
Branch: 2 branches
BranchTrueFalseTotal
if $k->isa("JSON::PP::Boolean") 1 0 1
$k ? : 1 0 1
446 }
447 87 ▶ return "$k" unless ref $k;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $k 87 0 87
448 0 return SION->new->canonical->encode($k);
449 }
450  
451 #### helper classes
452  
453 package SION::Date;
454  
455 use overload
456 1 ▶ '0+' => sub { ${ $_[0] } },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
457 4 ▶ '""' => sub { '.Date(' . SION::_double_str( 0.0 + ${ $_[0] } ) . ')' },
Subroutine __ANON__: called CC=1 SCAR=0.0
458 5 fallback => 1;
459  
460 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
461 9 my ( $class, $epoch ) = @_;
462 9 $epoch = 0.0 + $epoch;
463 9 return bless \$epoch, $class;
464 }
465  
466 8 ▶sub epoch { ${ $_[0] } }
Subroutine epoch: called CC=1 SCAR=0.0
467  
468 package SION::Data;
469  
470 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
471 1 my ( $class, $data ) = @_;
472 1 utf8::downgrade($data); # dies if $data contains wide characters
473 1 return bless \$data, $class;
474 }
475  
476 1 ▶sub data { ${ $_[0] } }
Subroutine data: called CC=1 SCAR=0.0
477  
478 package SION::Ext;
479  
480 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
481 6 my ( $class, $data ) = @_;
482 6 utf8::downgrade($data); # dies if $data contains wide characters
483 6 return bless \$data, $class;
484 }
485  
486 5 ▶sub data { ${ $_[0] } }
Subroutine data: called CC=1 SCAR=0.0
487  
488 package SION;
489  
490 5 1;
491  
492 __END__
↑ Summary