diff options
author | Yorhel <git@yorhel.nl> | 2018-02-09 13:17:32 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2018-02-09 13:17:36 +0100 |
commit | 5118fdd27f9a43cb4f4ce20ce6c49d2ce116731e (patch) | |
tree | cffeb4c8ab29bb8b54dd1868cbce90f8af5f0588 | |
parent | b0cf1fecb95515faa56de655f85bf7729010fcb6 (diff) |
TUWF::XML: Small performance improvement of xml_escape()
I've tried various things to make TUWF::XML faster, but so far this is
the only change that actually has some effect. Can shave off about 1% to
3% of the page load time for pages that are heavy on TUWF::XML usage.
And '>' totally doesn't need to be escaped.
-rw-r--r-- | lib/TUWF/XML.pm | 10 | ||||
-rw-r--r-- | lib/TUWF/XML.pod | 4 |
2 files changed, 6 insertions, 8 deletions
diff --git a/lib/TUWF/XML.pm b/lib/TUWF/XML.pm index a64dd46..86d05b1 100644 --- a/lib/TUWF/XML.pm +++ b/lib/TUWF/XML.pm @@ -117,17 +117,15 @@ sub mkclass { # XML escape (not a method) +my %XML = qw/& & < < " "/; sub xml_escape { - local $_ = shift; + local $_ = $_[0]; if(!defined $_) { carp "Attempting to XML-escape an undefined value"; return ''; } - s/&/&/g; - s/</</g; - s/>/>/g; - s/"/"/g; - return $_; + s/([&<"])/$XML{$1}/g; + $_; } # HTML escape, also does \n to <br /> conversion diff --git a/lib/TUWF/XML.pod b/lib/TUWF/XML.pod index 2973a15..7a0e529 100644 --- a/lib/TUWF/XML.pod +++ b/lib/TUWF/XML.pod @@ -130,8 +130,8 @@ Note that the order in which classes are returned may be somewhat random. =head2 xml_escape(string) -Returns the XML-escaped string. The characters C<&>, C<E<lt>>, C<E<gt>> and -C<"> will be replaced with their XML entity. +Returns the XML-escaped string. The characters C<&>, C<E<lt>>, and C<"> will be +replaced with their XML entity. =head2 html_escape(string) |