Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Faster utf8 escaping.

by kyle (Abbot)
on Apr 08, 2009 at 01:05 UTC ( #756200=note: print w/replies, xml ) Need Help??

in reply to Re: Faster utf8 escaping.
in thread Faster utf8 escaping.

Thank you! I like this solution. You're right, core modules can often get you most of the way to where you want to go.

That said, it needed some modification to work with the larger battery of tests I have locally. The first thing I noticed is that Encode doesn't always produce a four digit hex entity. I took care of that by allowing the regular expression to match two or four characters. Then came the string that encoded as "Mar&#ed;a; F", so I made the "characters" match only hex digits.

Now the replacement looks like this:

$s =~ s/&#x([a-f0-9]{2})?([a-f0-9]{2});/'\\u' . ($1||'00') . $2/ieg;

That still doesn't take care of the problem that ikegami raises. I figure I can preprocess with s/&/&x/g and then back with s/&x/&/g when it's done (or something), but then we're back up to five full scans over the input string. It might still be faster, but I haven't tested that yet.

Thanks again.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://756200]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2018-06-19 13:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.