| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package String::JS; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2016-03-11'; # DATE |
|
4
|
|
|
|
|
|
|
our $VERSION = '0.03'; # VERSION |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
15877
|
use 5.010001; |
|
|
1
|
|
|
|
|
2
|
|
|
7
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
15
|
|
|
8
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
380
|
use JSON::MaybeXS; |
|
|
1
|
|
|
|
|
5685
|
|
|
|
1
|
|
|
|
|
57
|
|
|
11
|
|
|
|
|
|
|
my $json = JSON::MaybeXS->new->allow_nonref; |
|
12
|
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
5
|
use Exporter; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
95
|
|
|
14
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
|
15
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
|
16
|
|
|
|
|
|
|
encode_js_string |
|
17
|
|
|
|
|
|
|
decode_js_string |
|
18
|
|
|
|
|
|
|
); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my %esc = ( |
|
21
|
|
|
|
|
|
|
"\n" => "\\n", |
|
22
|
|
|
|
|
|
|
"\r" => "\\r", |
|
23
|
|
|
|
|
|
|
"\x0b" => "\\v", |
|
24
|
|
|
|
|
|
|
"\t" => "\\t", |
|
25
|
|
|
|
|
|
|
"\b" => "\\b", |
|
26
|
|
|
|
|
|
|
"\f" => "\\f", |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub encode_js_string { |
|
30
|
7
|
|
|
7
|
1
|
143345
|
my ($str, $mode) = @_; |
|
31
|
1
|
|
|
1
|
|
3
|
no warnings 'uninitialized'; # shut up warning when $str is undef |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
340
|
|
|
32
|
7
|
100
|
|
|
|
15
|
if ($mode) { |
|
33
|
5
|
100
|
|
|
|
14
|
if ($mode == 1) { |
|
|
|
100
|
|
|
|
|
|
|
34
|
2
|
|
|
|
|
11
|
$str =~ s/([\\'])/\\$1/g; |
|
35
|
2
|
100
|
|
|
|
12
|
return qq('$str') unless $str =~ /[^\040-\176]/; # fast exit |
|
36
|
1
|
|
|
|
|
5
|
$str =~ s/([\n\r\x0b\t\b\f])/$esc{$1}/g; |
|
37
|
1
|
|
|
|
|
3
|
$str =~ s/([\0-\037\177-\377])/sprintf('\\x%02x',ord($1))/eg; |
|
|
0
|
|
|
|
|
0
|
|
|
38
|
1
|
|
|
|
|
1
|
$str =~ s/([^\040-\176])/sprintf('\\u{%04x}',ord($1))/eg; |
|
|
0
|
|
|
|
|
0
|
|
|
39
|
1
|
|
|
|
|
5
|
return qq('$str'); |
|
40
|
|
|
|
|
|
|
} elsif ($mode == 2) { |
|
41
|
2
|
|
|
|
|
10
|
$str =~ s/([\\'"])/\\\\$1/g; |
|
42
|
2
|
100
|
|
|
|
12
|
return qq('$str') unless $str =~ /[^\040-\176]/; # fast exit |
|
43
|
1
|
|
|
|
|
6
|
$str =~ s/([\n\r\x0b\t\b\f])/\\$esc{$1}/g; |
|
44
|
1
|
|
|
|
|
2
|
$str =~ s/([\0-\037\177-\377])/sprintf('\\\\x%02x',ord($1))/eg; |
|
|
0
|
|
|
|
|
0
|
|
|
45
|
1
|
|
|
|
|
2
|
$str =~ s/([^\040-\176])/sprintf('\\\\u{%04x}',ord($1))/eg; |
|
|
0
|
|
|
|
|
0
|
|
|
46
|
1
|
|
|
|
|
5
|
return qq('$str'); |
|
47
|
|
|
|
|
|
|
} else { |
|
48
|
1
|
|
|
|
|
10
|
die "Invalid mode, must be 0, 1, or 2"; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} else { |
|
51
|
2
|
|
|
|
|
34
|
return $json->encode("$str"); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub decode_js_string { |
|
56
|
2
|
|
|
2
|
1
|
1452
|
my $str = shift; |
|
57
|
2
|
100
|
|
|
|
11
|
if ($str =~ /\A"/o) { |
|
|
|
50
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
14
|
$json->decode($str); |
|
59
|
|
|
|
|
|
|
} elsif ($str =~ /\A'/o) { |
|
60
|
0
|
|
|
|
|
0
|
die "Decoding JavaScript string with single quotes not yet implemented"; |
|
61
|
|
|
|
|
|
|
} else { |
|
62
|
1
|
|
|
|
|
10
|
die "Invalid JavaScript string literal"; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
|
67
|
|
|
|
|
|
|
# ABSTRACT: Utilities for Javascript string literal representation |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |