Filename | /usr/local/src/github.com/foswiki/core/lib/Foswiki/Macros/ENCODE.pm |
Statements | Executed 196 statements in 1.68ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
15 | 1 | 1 | 534µs | 1.16ms | ENCODE | Foswiki::
1 | 1 | 1 | 40µs | 52µs | BEGIN@4.61 | Foswiki::
1 | 1 | 1 | 28µs | 63µs | BEGIN@5.62 | Foswiki::
0 | 0 | 0 | 0s | 0s | _s2d | Foswiki::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # See bottom of file for license and copyright information | ||||
2 | package Foswiki; | ||||
3 | |||||
4 | 2 | 61µs | 2 | 64µs | # spent 52µs (40+12) within Foswiki::BEGIN@4.61 which was called:
# once (40µs+12µs) by Foswiki::_expandMacroOnTopicRendering at line 4 # spent 52µs making 1 call to Foswiki::BEGIN@4.61
# spent 12µs making 1 call to strict::import |
5 | 2 | 943µs | 2 | 97µs | # spent 63µs (28+35) within Foswiki::BEGIN@5.62 which was called:
# once (28µs+35µs) by Foswiki::_expandMacroOnTopicRendering at line 5 # spent 63µs making 1 call to Foswiki::BEGIN@5.62
# spent 34µs making 1 call to warnings::import |
6 | 11 | 36µs | my @DIG = map { chr($_) } ( 0 .. 9 ); | ||
7 | |||||
8 | # Returns a decimal number encoded as a string where each digit is | ||||
9 | # replaced by an unprintable character | ||||
10 | sub _s2d { | ||||
11 | return join( '', map { chr( int($_) ) } split( '', shift ) ); | ||||
12 | } | ||||
13 | |||||
14 | # spent 1.16ms (534µs+627µs) within Foswiki::ENCODE which was called 15 times, avg 77µs/call:
# 15 times (534µs+627µs) by Foswiki::_expandMacroOnTopicRendering at line 3066 of /usr/local/src/github.com/foswiki/core/lib/Foswiki.pm, avg 77µs/call | ||||
15 | 165 | 489µs | my ( $this, $params ) = @_; | ||
16 | |||||
17 | my $old = $params->{old}; | ||||
18 | my $new = $params->{new}; | ||||
19 | my $type = $params->{type}; | ||||
20 | |||||
21 | if ( defined $type && ( defined $old || defined $new ) ) { | ||||
22 | return $this->inlineAlert( 'alerts', 'ENCODE_bad_1' ); | ||||
23 | } | ||||
24 | if ( defined $old && !defined $new || !defined $old && defined $new ) { | ||||
25 | return $this->inlineAlert( 'alerts', 'ENCODE_bad_2' ); | ||||
26 | } | ||||
27 | |||||
28 | my $text = $params->{_DEFAULT}; | ||||
29 | $text = '' unless defined $text; | ||||
30 | |||||
31 | if ( defined $old ) { | ||||
32 | my @old = split( ',', $old ); | ||||
33 | my @new = split( ',', $new ); | ||||
34 | while ( scalar(@new) < scalar(@old) ) { | ||||
35 | push( @new, '' ); | ||||
36 | } | ||||
37 | |||||
38 | # The double loop is to make it behave like tr///. The first loop | ||||
39 | # locates the tokens to replace, and the second loop subs them. | ||||
40 | my %toks; # detect repeated tokens | ||||
41 | for ( my $i = 0 ; $i <= $#old ; $i++ ) { | ||||
42 | my $e = _s2d($i); | ||||
43 | my $o = $old[$i]; | ||||
44 | if ( $toks{$o} ) { | ||||
45 | return $this->inlineAlert( 'alerts', 'ENCODE_bad_3', $o ); | ||||
46 | } | ||||
47 | $toks{$o} = 1; | ||||
48 | $o = quotemeta( expandStandardEscapes($o) ); | ||||
49 | $text =~ s/$o/$e/ge; | ||||
50 | } | ||||
51 | for ( my $i = 0 ; $i <= $#new ; $i++ ) { | ||||
52 | my $e = _s2d($i); | ||||
53 | my $n = expandStandardEscapes( $new[$i] ); | ||||
54 | $text =~ s/$e/$n/g; | ||||
55 | } | ||||
56 | return $text; | ||||
57 | } | ||||
58 | |||||
59 | $type ||= 'url'; | ||||
60 | |||||
61 | 15 | 139µs | 60 | 95µs | if ( $type =~ /^entit(y|ies)$/i ) { # spent 95µs making 60 calls to Foswiki::CORE:match, avg 2µs/call |
62 | return entityEncode($text); | ||||
63 | } | ||||
64 | elsif ( $type =~ /^html$/i ) { | ||||
65 | return entityEncode( $text, "\n\r" ); | ||||
66 | } | ||||
67 | elsif ( $type =~ /^quotes?$/i ) { | ||||
68 | |||||
69 | # escape quotes with backslash (Bugs:Item3383 fix) | ||||
70 | $text =~ s/\"/\\"/go; | ||||
71 | return $text; | ||||
72 | } | ||||
73 | elsif ( $type =~ /^url$/i ) { | ||||
74 | |||||
75 | # This is legacy, stretching back to 2001. Checkin comment was: | ||||
76 | # "Fixed URL encoding". At that time it related to the encoding of | ||||
77 | # parameters to the "oops" script exclusively. I'm taking it out | ||||
78 | # because I can't see any situation in which it might have been | ||||
79 | # used in anger. | ||||
80 | # $text =~ s/\r*\n\r*/<br \/>/; | ||||
81 | 15 | 532µs | return urlEncode($text); # spent 532µs making 15 calls to Foswiki::urlEncode, avg 36µs/call | ||
82 | } | ||||
83 | elsif ( $type =~ /^(off|none)$/i ) { | ||||
84 | |||||
85 | # no encoding | ||||
86 | return $text; | ||||
87 | } | ||||
88 | else { # safe | ||||
89 | # entity encode ' " < > and % | ||||
90 | $text =~ s/([<>%'"])/'&#'.ord($1).';'/ge; | ||||
91 | return $text; | ||||
92 | } | ||||
93 | } | ||||
94 | |||||
95 | 1 | 9µs | 1; | ||
96 | __END__ |