Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved xml_escape performance in all use cases
  • Loading branch information
kraih committed Jan 30, 2014
1 parent 9514fb5 commit ddff39c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Changes
@@ -1,7 +1,6 @@

4.73 2014-01-30
- Improved xml_escape performance significantly when no escaping is
necessary.
- Improved xml_escape performance significantly.

4.72 2014-01-29
- Added accepts, template_for and template_handler methods to
Expand Down
18 changes: 10 additions & 8 deletions lib/Mojo/Util.pm
Expand Up @@ -35,6 +35,15 @@ for my $line (split "\x0a", slurp(catfile dirname(__FILE__), 'entities.txt')) {
$ENTITIES{$1} = defined $3 ? (chr(hex $2) . chr(hex $3)) : chr(hex $2);
}

# Characters that should be escaped in XML
my %XML = (
'&' => '&',
'<' => '&lt;',
'>' => '&gt;',
'"' => '&quot;',
'\'' => '&#39;'
);

# Encoding cache
my %CACHE;

Expand Down Expand Up @@ -325,14 +334,7 @@ sub url_unescape {

sub xml_escape {
my $str = shift;

return $str unless $str =~ /[&<>"']/;
$str =~ s/&/&amp;/g;
$str =~ s/</&lt;/g;
$str =~ s/>/&gt;/g;
$str =~ s/"/&quot;/g;
$str =~ s/'/&#39;/g;

$str =~ s/([&<>"'])/$XML{$1}/ge;
return $str;
}

Expand Down

0 comments on commit ddff39c

Please sign in to comment.