line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JE::String; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.066'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
101
|
|
|
101
|
|
38134
|
use strict; |
|
101
|
|
|
|
|
154
|
|
|
101
|
|
|
|
|
3491
|
|
7
|
101
|
|
|
101
|
|
446
|
use warnings; no warnings 'utf8'; |
|
101
|
|
|
101
|
|
134
|
|
|
101
|
|
|
|
|
2348
|
|
|
101
|
|
|
|
|
392
|
|
|
101
|
|
|
|
|
142
|
|
|
101
|
|
|
|
|
4134
|
|
8
|
|
|
|
|
|
|
|
9
|
101
|
|
|
|
|
560
|
use overload fallback => 1, |
10
|
|
|
|
|
|
|
'""' => 'value', |
11
|
|
|
|
|
|
|
# cmp => sub { "$_[0]" cmp $_[1] } |
12
|
101
|
|
|
101
|
|
1471
|
; |
|
101
|
|
|
|
|
875
|
|
13
|
|
|
|
|
|
|
|
14
|
101
|
|
|
101
|
|
6479
|
use Carp; |
|
101
|
|
|
|
|
191
|
|
|
101
|
|
|
|
|
6800
|
|
15
|
101
|
|
|
101
|
|
585
|
use Scalar::Util qw 'blessed tainted'; |
|
101
|
|
|
|
|
149
|
|
|
101
|
|
|
|
|
5698
|
|
16
|
|
|
|
|
|
|
|
17
|
101
|
|
|
101
|
|
506
|
use Exporter 5.57 'import'; |
|
101
|
|
|
|
|
2729
|
|
|
101
|
|
|
|
|
62822
|
|
18
|
|
|
|
|
|
|
our @EXPORT_OK = qw'surrogify desurrogify'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
require JE::Object::String; |
21
|
|
|
|
|
|
|
require JE::Boolean; |
22
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub new { |
32
|
19012
|
|
|
19012
|
0
|
34285
|
my($class, $global, $val) = @_; |
33
|
19012
|
50
|
|
|
|
66044
|
defined blessed $global |
34
|
|
|
|
|
|
|
or croak "First argument to JE::String->new is not an object"; |
35
|
|
|
|
|
|
|
|
36
|
19012
|
|
|
|
|
24277
|
my $self; |
37
|
19012
|
50
|
33
|
|
|
61566
|
if(defined blessed $val and $val->can('to_string')) { |
38
|
0
|
|
|
|
|
0
|
$self = bless [$val->to_string->[0],undef,$global], $class; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else { |
41
|
19012
|
|
|
|
|
65790
|
$self = bless [undef,$val, $global], $class; |
42
|
|
|
|
|
|
|
} |
43
|
19012
|
|
|
|
|
70453
|
$self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _new { # ~~~ Should we document this and make it public? The problem |
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
|
47693
|
100
|
|
47693
|
|
275719
|
bless [defined $_[2] ? $_[2] : '',undef,$_[1]], $_[0]; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub prop { |
55
|
|
|
|
|
|
|
# ~~~ Make prop simply return the value if the prototype has that |
56
|
|
|
|
|
|
|
# property. |
57
|
390
|
|
|
390
|
0
|
598
|
my $self = shift; |
58
|
|
|
|
|
|
|
|
59
|
390
|
100
|
|
|
|
1170
|
if ($_[0] eq 'length') { |
60
|
13
|
100
|
|
|
|
122
|
return JE::Number->new($$self[2], length ( |
61
|
|
|
|
|
|
|
defined $$self[0] ? $$self[0] : |
62
|
|
|
|
|
|
|
($$self[0]=surrogify($$self[1])) |
63
|
|
|
|
|
|
|
)); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
377
|
|
|
|
|
1473
|
$$self[2]->prototype_for('String')->prop(@_); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub keys { |
70
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
71
|
0
|
|
|
|
|
0
|
$$self[2]->prototype_for('String')->keys;} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub delete { |
74
|
0
|
|
|
0
|
0
|
0
|
return $_[1] ne 'length' |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub method { |
78
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
79
|
1
|
|
|
|
|
123
|
$$self[2]->prototype_for('String')->prop(shift)->apply( |
80
|
|
|
|
|
|
|
$self,$$self[2]->upgrade(@_) |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub value { |
86
|
29772
|
100
|
|
29772
|
0
|
2057549
|
defined $_[0][1] ? $_[0][1] : ($_[0][1] = desurrogify($_[0][0])); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
*TO_JSON=*value; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub value16 { |
91
|
41101
|
100
|
|
41101
|
0
|
272674
|
defined $_[0][0] ? $_[0][0] : ($_[0][0] = surrogify($_[0][1])); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
19975
|
|
|
19975
|
0
|
67781
|
sub typeof { 'string' } |
96
|
1195
|
|
|
1195
|
0
|
2855
|
sub id { 'str:' . $_[0]->value16 } |
97
|
80
|
|
|
80
|
0
|
775
|
sub class { 'String' } |
98
|
675
|
|
|
675
|
0
|
2215
|
sub primitive { 1 } |
99
|
|
|
|
|
|
|
|
100
|
26019
|
|
|
26019
|
0
|
46509
|
sub to_primitive { $_[0] } |
101
|
29456
|
|
|
29456
|
0
|
118491
|
sub to_string { $_[0] } |
102
|
|
|
|
|
|
|
# $_[0][2] is the global obj |
103
|
130
|
100
|
|
130
|
0
|
1847
|
sub to_boolean { JE::Boolean->new( $_[0][2], |
104
|
|
|
|
|
|
|
length defined $_[0][0] |
105
|
|
|
|
|
|
|
? $_[0][0] : $_[0][1] |
106
|
|
|
|
|
|
|
) } |
107
|
7
|
|
|
7
|
0
|
207
|
sub to_object { JE::Object::String->new($_[0][2], shift) } |
108
|
|
|
|
|
|
|
|
109
|
99
|
|
|
99
|
|
11729
|
our $s = qr.[\p{Zs}\s\ck\x{2028}\x{2029}]*.; |
|
99
|
|
|
|
|
14433
|
|
|
99
|
|
|
|
|
2011
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub to_number { |
112
|
844
|
|
|
844
|
0
|
20326
|
my $value = (my $self = shift)->[0]; |
113
|
844
|
100
|
|
|
|
2281
|
defined $value or $value = $$self[1]; |
114
|
844
|
100
|
|
|
|
10417
|
JE::Number->new($self->[2], |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
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
|
|
|
2
|
0
|
8
|
sub global { $_[0][2] } |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub taint { |
136
|
1
|
|
|
1
|
0
|
5
|
my $self = shift; |
137
|
1
|
50
|
33
|
|
|
23
|
tainted $self->[0] || tainted $self->[1] and return $self; |
138
|
0
|
|
|
|
|
0
|
my $alter_ego = [@$self]; |
139
|
0
|
0
|
|
|
|
0
|
$alter_ego->[defined $alter_ego->[0] ? 0 : 1] .= shift(); |
140
|
0
|
|
|
|
|
0
|
return bless $alter_ego, ref $self; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub desurrogify($) { |
145
|
45894
|
|
|
45894
|
0
|
67462
|
my $ret = shift; |
146
|
45894
|
|
|
|
|
47930
|
my($ord1, $ord2); |
147
|
45894
|
|
|
|
|
140495
|
for(my $n = 0; $n < length $ret; ++$n) { # really slow |
148
|
1866499
|
100
|
100
|
|
|
5046240
|
($ord1 = ord substr $ret,$n,1) >= 0xd800 and |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
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
|
45894
|
|
|
|
|
268965
|
$ret; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub surrogify($) { |
174
|
511
|
|
|
511
|
0
|
986
|
my $ret = shift; |
175
|
|
|
|
|
|
|
|
176
|
101
|
|
|
101
|
|
1760738
|
no warnings 'utf8'; |
|
101
|
|
|
|
|
706
|
|
|
101
|
|
|
|
|
11858
|
|
177
|
|
|
|
|
|
|
|
178
|
511
|
|
|
|
|
64511
|
$ret =~ s<([^\0-\x{ffff}])>< |
179
|
8
|
|
|
|
|
232
|
chr((ord($1) - 0x10000) / 0x400 + 0xD800) |
180
|
|
|
|
|
|
|
. chr((ord($1) - 0x10000) % 0x400 + 0xDC00) |
181
|
|
|
|
|
|
|
>eg; |
182
|
511
|
|
|
|
|
2135
|
$ret; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |
187
|
|
|
|
|
|
|
__END__ |