JE/String.pm

filter: Statementstmt 90.9% 70 / 77 Branchbran 84.3% 27 / 32 Conditioncond 66.6% 10 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 93.1% 27 / 29 Podpod 0.0% 0 / 20 totaltotal 75.6% 137 / 181 CC 22 SCAR 11.8
CC
22
Cov
86%
taint
8.7
desurrogify
6.0
to_number
5.0
CRAP 3.2
« JE/escape.pl ↑ Summary JE/Number.pm »
1 package JE::String;
2  
3 101 our $VERSION = '0.066';
4  
5  
6 101 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 101 ▶use warnings; no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 101 ▶use overload fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
10 '""' => 'value',
11 # cmp => sub { "$_[0]" cmp $_[1] }
12 101 ;
13  
14 101 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 101 ▶use Scalar::Util qw 'blessed tainted';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 101 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 101 our @EXPORT_OK = qw'surrogify desurrogify';
19  
20 101 require JE::Object::String;
21 101 require JE::Boolean;
22 101 require JE::Number;
23  
24  
25 # Internals:
26 # bless [ $utf16_string, $unicode_string, $global_object], 'JE::String';
27 # Either of the first two slots may be empty. It will be filled in
28 # on demand.
29  
30  
31 18992 ▶sub new {
Subroutine new: called CC=4 SCAR=15.5
Pod undocumented
32 18992 my($class, $global, $val) = @_;
33 18992 ▶ defined blessed $global
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($global) or croak("First argument to JE::String->new is not an object") 18992 0 18992
34 or croak "First argument to JE::String->new is not an object";
35  
36 18992 my $self;
37 18992 ▶ if(defined blessed $val and $val->can('to_string')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($val) and $val->can('to_string')) { } 0 18992 18992
Condition33%1/3
defined builtin::blessed($val) and $val->can("to_string")
!ll&&!rl&&r
1899200
MC/DC0%0/2
defined builtin::blessed($val) and $val->can("to_string")
defined builtin::blessed($val) $val->can("to_string")
Truth table33%1/3
A and B
A= defined builtin::blessed($val)B= $val->can("to_string")
ABresult
0X0
100
111
38 0 $self = bless [$val->to_string->[0],undef,$global], $class;
39 }
40 else {
41 18992 $self = bless [undef,$val, $global], $class;
42 }
43 18992 $self;
44 }
45  
46 47601 ▶sub _new { # ~~~ Should we document this and make it public? The problem
Subroutine _new: called CC=2 SCAR=6.9
47 # with it is that it has no error-checking whatsoever, and
48 # can consequently make JS do weird things. (Maybe it’s OK,
49 # since I doubt any code would choke on a charCodeAt result
50 # > 0xffff.)
51 47601 ▶ bless [defined $_[2] ? $_[2] : '',undef,$_[1]], $_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 47600 1 47601
52 }
53  
54 390 ▶sub prop {
Subroutine prop: called CC=3 SCAR=11.0
Pod undocumented
55 # ~~~ Make prop simply return the value if the prototype has that
56 # property.
57 390 my $self = shift;
58  
59 390 ▶ if ($_[0] eq 'length') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "length") 13 377 390
60 13 ▶ return JE::Number->new($$self[2], length (
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[0] ? : 12 1 13
61 defined $$self[0] ? $$self[0] :
62 ($$self[0]=surrogify($$self[1]))
63 ));
64 }
65  
66 377 $$self[2]->prototype_for('String')->prop(@_);
67 }
68  
69 0 ▶sub keys {
Subroutine keys: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my $self = shift;
71 0 $$self[2]->prototype_for('String')->keys;}
72  
73 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
74 0 return $_[1] ne 'length'
75 }
76  
77 1 ▶sub method {
Subroutine method: called CC=1 SCAR=0.0
Pod undocumented
78 1 my $self = shift;
79 1 $$self[2]->prototype_for('String')->prop(shift)->apply(
80 $self,$$self[2]->upgrade(@_)
81 );
82 }
83  
84  
85 29735 ▶sub value {
Subroutine value: called CC=2 SCAR=6.9
Pod undocumented
86 29735 ▶ defined $_[0][1] ? $_[0][1] : ($_[0][1] = desurrogify($_[0][0]));
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0][1] ? : 7595 22140 29735
87 }
88 101 *TO_JSON=*value;
89  
90 41076 ▶sub value16 {
Subroutine value16: called CC=2 SCAR=6.9
Pod undocumented
91 41076 ▶ defined $_[0][0] ? $_[0][0] : ($_[0][0] = surrogify($_[0][1]));
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0][0] ? : 40838 238 41076
92 }
93  
94  
95 19955 ▶sub typeof { 'string' }
Subroutine typeof: called CC=1 SCAR=0.0
Pod undocumented
96 1195 ▶sub id { 'str:' . $_[0]->value16 }
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
97 80 ▶sub class { 'String' }
Subroutine class: called CC=1 SCAR=0.0
Pod undocumented
98 680 ▶sub primitive { 1 }
Subroutine primitive: called CC=1 SCAR=0.0
Pod undocumented
99  
100 26013 ▶sub to_primitive { $_[0] }
Subroutine to_primitive: called CC=1 SCAR=0.0
Pod undocumented
101 29454 ▶sub to_string { $_[0] }
Subroutine to_string: called CC=1 SCAR=0.0
Pod undocumented
102 # $_[0][2] is the global obj
103 115 ▶sub to_boolean { JE::Boolean->new( $_[0][2],
Subroutine to_boolean: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
length defined $_[0][0] ? : 44 71 115
104 length defined $_[0][0]
105 ? $_[0][0] : $_[0][1]
106 ) }
107 7 ▶sub to_object { JE::Object::String->new($_[0][2], shift) }
Subroutine to_object: called CC=1 SCAR=0.0
Pod undocumented
108  
109 101 our $s = qr.[\p{Zs}\s\ck\x{2028}\x{2029}]*.;
110  
111 857 ▶sub to_number {
Subroutine to_number: called CC=5 SCAR=16.1
Pod undocumented
112 857 my $value = (my $self = shift)->[0];
113 857 ▶ defined $value or $value = $$self[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $value or $value = $self->[1] 606 251 857
114 857 ▶ JE::Number->new($self->[2],
Branch: 3 branches
BranchTrueFalseTotal
$value =~ /^$JE::String::s ( [+-]? (?: (?=[0-9]|\.[0-9]) [0-9]* (?:\.[0-9]*)? (?:[Ee][+-]?[0-9]+)? | Infinity ) $JE::String::s )? \z /ox ? : 496 361 857
defined $1 ? : 493 3 496
$value =~ /^$JE::String::s 0[Xx] ([A-Fa-f0-9]+) $JE::String::s\z/ox ? : 6 355 361
115 $value =~ /^$s
116 (
117 [+-]?
118 (?:
119 (?=[0-9]|\.[0-9]) [0-9]* (?:\.[0-9]*)?
120 (?:[Ee][+-]?[0-9]+)?
121 |
122 Infinity
123 )
124 $s
125 )?
126 \z
127 /ox ? defined $1 ? $value : 0 :
128 $value =~ /^$s 0[Xx] ([A-Fa-f0-9]+) $s\z/ox ? hex $1 :
129 'NaN'
130 );
131 }
132  
133 2 ▶sub global { $_[0][2] }
Subroutine global: called CC=1 SCAR=0.0
Pod undocumented
134  
135 1 ▶sub taint {
Subroutine taint: called CC=4 SCAR=21.7
Pod undocumented
136 1 my $self = shift;
137 1 ▶ tainted $self->[0] || tainted $self->[1] and return $self;
Branch: 1 branch
BranchTrueFalseTotal
&tainted($self->[0]) || &tainted($self->[1]) and return $self 1 0 1
Condition33%1/3
&tainted($self->[0]) or &tainted($self->[1])
l!l&&r!l&&!r
100
MC/DC0%0/2
&tainted($self->[0]) or &tainted($self->[1])
&tainted($self->[0]) &tainted($self->[1])
Truth table33%1/3
A or B
A= &tainted($self->[0])B= &tainted($self->[1])
ABresult
1X1
011
000
138 0 my $alter_ego = [@$self];
139 0 ▶ $alter_ego->[defined $alter_ego->[0] ? 0 : 1] .= shift();
Branch: 1 branch
BranchTrueFalseTotal
defined $alter_ego->[0] ? : 0 0 0
140 0 return bless $alter_ego, ref $self;
141 }
142  
143  
144 45802 ▶sub desurrogify($) {
Subroutine desurrogify: called CC=6 SCAR=17.9
Pod undocumented
145 45802 my $ret = shift;
146 45802 my($ord1, $ord2);
147 45802 ▶ for(my $n = 0; $n < length $ret; ++$n) { # really slow
Branch: 1 branch
BranchTrueFalseTotal
if ($n < length $ret) 1864882 45802 1910684
148 1864882 ▶ ($ord1 = ord substr $ret,$n,1) >= 0xd800 and
Branch: 1 branch
BranchTrueFalseTotal
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 and $ord2 <= 57343 and substr($ret, $n, 2) = chr 65536 + ($ord1 - 55296) * 1024 + ($ord2 - 56320) 10 1864872 1864882
Condition88%8/9
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 and $ord2 <= 57343
!ll&&!rl&&r
1864872010
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320
!ll&&!rl&&r
18648512110
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319
!ll&&!rl&&r
18648193231
MC/DC75%3/4
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 and $ord2 <= 57343
($ord1 = ord substr($ret, $n, 1)) >= 55296 $ord1 <= 56319 ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 $ord2 <= 57343
Truth table80%4/5
A and B and C and D
A= ($ord1 = ord substr($ret, $n, 1)) >= 55296B= $ord1 <= 56319C= ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320D= $ord2 <= 57343
ABCDresult
0XXX0
10XX0
110X0
11100
11111
149 $ord1 <= 0xdbff and
150 ($ord2 = ord substr $ret,$n+1,1) >= 0xdc00 and
151 $ord2 <= 0xdfff and
152 substr($ret,$n,2) =
153 chr 0x10000 + ($ord1 - 0xD800) * 0x400 + ($ord2 - 0xDC00);
154 }
155  
156 # In perl 5.8.8, if there is a sub on the call stack that was
157 # triggered by the overloading mechanism when the object with the
158 # overloaded operator was passed as the only argument to 'die',
159 # then the following substitution magically calls that subroutine
160 # again with the same arguments, thereby causing infinite
161 # recursion:
162 #
163 # $ret =~ s/([\x{d800}-\x{dbff}])([\x{dc00}-\x{dfff}])/
164 # chr 0x10000 + (ord($1) - 0xD800) * 0x400 +
165 # (ord($2) - 0xDC00)
166 # /ge;
167 #
168 # 5.9.4 still has this bug.
169  
170 45802 $ret;
171 }
172  
173 513 ▶sub surrogify($) {
Subroutine surrogify: called CC=1 SCAR=0.0
Pod undocumented
174 513 my $ret = shift;
175  
176 101 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 513 $ret =~ s<([^\0-\x{ffff}])><
179 8 chr((ord($1) - 0x10000) / 0x400 + 0xD800)
180 . chr((ord($1) - 0x10000) % 0x400 + 0xDC00)
181 >eg;
182 513 $ret;
183 }
184  
185  
186 101 1;
187 __END__
« JE/escape.pl ↑ Summary JE/Number.pm »