line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Typist::Util::String; |
2
|
1
|
|
|
1
|
|
838
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use base qw( Exporter ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
5
|
1
|
|
|
1
|
|
4
|
use vars qw( @EXPORT_OK ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1174
|
|
6
|
|
|
|
|
|
|
@EXPORT_OK = qw( decode_html decode_xml remove_html encode_html encode_xml |
7
|
|
|
|
|
|
|
encode_js encode_php encode_phphere encode_url ); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub encode_js { |
10
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
11
|
0
|
0
|
|
|
|
|
return '' unless defined $str; |
12
|
0
|
|
|
|
|
|
$str =~ s!(['"\\])!\\$1!g; |
13
|
0
|
|
|
|
|
|
$str =~ s!\n!\\n!g; |
14
|
0
|
|
|
|
|
|
$str =~ s!\f!\\f!g; |
15
|
0
|
|
|
|
|
|
$str =~ s!\r!\\r!g; |
16
|
0
|
|
|
|
|
|
$str =~ s!\t!\\t!g; |
17
|
0
|
|
|
|
|
|
$str; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub encode_php { |
21
|
0
|
|
|
0
|
1
|
|
my ($str, $meth) = @_; |
22
|
0
|
0
|
|
|
|
|
return '' unless defined $str; |
23
|
0
|
0
|
|
|
|
|
if ($meth eq 'qq') { |
|
|
0
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
$str = encode_phphere($str); |
25
|
0
|
|
|
|
|
|
$str =~ s!"!\\"!g; ## Replace " with \" |
26
|
|
|
|
|
|
|
} elsif (substr($meth, 0, 4) eq 'here') { |
27
|
0
|
|
|
|
|
|
$str = encode_phphere($str); |
28
|
|
|
|
|
|
|
} else { |
29
|
0
|
|
|
|
|
|
$str =~ s!\\!\\\\!g; ## Replace \ with \\ |
30
|
0
|
|
|
|
|
|
$str =~ s!'!\\'!g; ## Replace ' with \' |
31
|
|
|
|
|
|
|
} |
32
|
0
|
|
|
|
|
|
$str; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub encode_phphere { |
36
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
37
|
0
|
|
|
|
|
|
$str =~ s!\\!\\\\!g; ## Replace \ with \\ |
38
|
0
|
|
|
|
|
|
$str =~ s!\$!\\\$!g; ## Replace $ with \$ |
39
|
0
|
|
|
|
|
|
$str =~ s!\n!\\n!g; ## Replace character \n with string \n |
40
|
0
|
|
|
|
|
|
$str =~ s!\r!\\r!g; ## Replace character \r with string \r |
41
|
0
|
|
|
|
|
|
$str =~ s!\t!\\t!g; ## Replace character \t with string \t |
42
|
0
|
|
|
|
|
|
$str; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub encode_url { |
46
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
47
|
0
|
|
|
|
|
|
$str =~ s!([^a-zA-Z0-9_.~-])!uc sprintf "%%%02x", ord($1)!eg; |
|
0
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
$str; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub decode_url { |
52
|
0
|
|
|
0
|
0
|
|
my ($str) = @_; |
53
|
0
|
|
|
|
|
|
$str =~ s!%([0-9a-fA-F][0-9a-fA-F])!pack("H*",$1)!eg; |
|
0
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
$str; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
{ |
58
|
1
|
|
|
1
|
|
802
|
my $Have_Entities = eval 'use HTML::Entities; 1' ? 1 : 0; |
|
1
|
|
|
|
|
13338
|
|
|
1
|
|
|
|
|
103
|
|
59
|
|
|
|
|
|
|
my $NoHTMLEntities = 1; # hard coded. make switch? purpose? |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub encode_html { |
62
|
0
|
|
|
0
|
1
|
|
my ($html, $can_double_encode) = @_; |
63
|
0
|
0
|
|
|
|
|
return '' unless defined $html; |
64
|
0
|
|
|
|
|
|
$html =~ tr!\cM!!d; |
65
|
0
|
0
|
0
|
|
|
|
if ($Have_Entities && !$NoHTMLEntities) { |
66
|
0
|
|
|
|
|
|
$html = HTML::Entities::encode_entities($html); |
67
|
|
|
|
|
|
|
} else { |
68
|
0
|
0
|
|
|
|
|
if ($can_double_encode) { |
69
|
0
|
|
|
|
|
|
$html =~ s!&!&!g; |
70
|
|
|
|
|
|
|
} else { |
71
|
|
|
|
|
|
|
## Encode any & not followed by something that looks like |
72
|
|
|
|
|
|
|
## an entity, numeric or otherwise. |
73
|
0
|
|
|
|
|
|
$html =~ s/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w{1,8});)/&/g; |
74
|
|
|
|
|
|
|
} |
75
|
0
|
|
|
|
|
|
$html =~ s!"!"!g; #" |
76
|
0
|
|
|
|
|
|
$html =~ s!
|
77
|
0
|
|
|
|
|
|
$html =~ s!>!>!g; |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
$html; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub decode_html { |
83
|
0
|
|
|
0
|
1
|
|
my ($html) = @_; |
84
|
0
|
0
|
|
|
|
|
return '' unless defined $html; |
85
|
0
|
|
|
|
|
|
$html =~ tr!\cM!!d; |
86
|
0
|
0
|
0
|
|
|
|
if ($Have_Entities && !$NoHTMLEntities) { |
87
|
0
|
|
|
|
|
|
$html = HTML::Entities::decode_entities($html); |
88
|
|
|
|
|
|
|
} else { |
89
|
0
|
|
|
|
|
|
$html =~ s!"!"!g; #" |
90
|
0
|
|
|
|
|
|
$html =~ s!<!
|
91
|
0
|
|
|
|
|
|
$html =~ s!>!>!g; |
92
|
0
|
|
|
|
|
|
$html =~ s!&!&!g; |
93
|
|
|
|
|
|
|
} |
94
|
0
|
|
|
|
|
|
$html; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
my %Map = ( |
100
|
|
|
|
|
|
|
'&' => '&', |
101
|
|
|
|
|
|
|
'"' => '"', |
102
|
|
|
|
|
|
|
'<' => '<', |
103
|
|
|
|
|
|
|
'>' => '>', |
104
|
|
|
|
|
|
|
'\'' => ''' |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
my %Map_Decode = reverse %Map; |
107
|
|
|
|
|
|
|
my $RE = join '|', keys %Map; |
108
|
|
|
|
|
|
|
my $RE_D = join '|', keys %Map_Decode; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub encode_xml { |
111
|
0
|
|
|
0
|
1
|
|
my ($str, $nocdata) = @_; |
112
|
0
|
0
|
|
|
|
|
return '' unless defined $str; |
113
|
0
|
0
|
0
|
|
|
|
if ( |
114
|
|
|
|
|
|
|
!$nocdata |
115
|
|
|
|
|
|
|
&& $str =~ m/ |
116
|
|
|
|
|
|
|
<[^>]+> ## HTML markup |
117
|
|
|
|
|
|
|
| ## or |
118
|
|
|
|
|
|
|
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?); |
119
|
|
|
|
|
|
|
## something that looks like an HTML entity. |
120
|
|
|
|
|
|
|
/x |
121
|
|
|
|
|
|
|
) { |
122
|
|
|
|
|
|
|
## If ]]> exists in the string, encode the > to >. |
123
|
0
|
|
|
|
|
|
$str =~ s/]]>/]]>/g; |
124
|
0
|
|
|
|
|
|
$str = ''; |
125
|
|
|
|
|
|
|
} else { |
126
|
0
|
|
|
|
|
|
$str =~ s!($RE)!$Map{$1}!g; |
127
|
|
|
|
|
|
|
} |
128
|
0
|
|
|
|
|
|
$str; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub decode_xml { |
132
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
133
|
0
|
0
|
|
|
|
|
return '' unless defined $str; |
134
|
0
|
0
|
|
|
|
|
if ($str =~ s//$1/g) { |
135
|
|
|
|
|
|
|
## Decode encoded ]]> |
136
|
0
|
|
|
|
|
|
$str =~ s/]]&(gt|#62);/]]>/g; |
137
|
|
|
|
|
|
|
} else { |
138
|
0
|
|
|
|
|
|
$str =~ s!($RE_D)!$Map_Decode{$1}!g; |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
|
$str; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub remove_html { |
145
|
0
|
|
|
0
|
1
|
|
my ($text) = @_; |
146
|
0
|
0
|
|
|
|
|
return $text if !defined $text; # suppress warnings |
147
|
0
|
|
|
|
|
|
$text =~ s!<[^>]+>!!gs; |
148
|
0
|
|
|
|
|
|
$text =~ s!
|
149
|
0
|
|
|
|
|
|
$text; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
1; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 NAME |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Typist::Util::String - Utility methods for string manipulation |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 METHODS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=over |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item decode_html |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item decode_xml |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item remove_html |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item encode_html |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item encode_xml |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item encode_js |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item encode_php |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item encode_phphere |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item encode_url |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=end |