Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Faster utf8 escaping.

by ikegami (Patriarch)
on Apr 07, 2009 at 22:20 UTC ( [id://756165]=note: print w/replies, xml ) Need Help??


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

First, it's important to note that Encode::FB_XMLCREF doesn't work on from_to when using 5.8.8's Encode 2.12.

$ perl -MEncode=encode,from_to -wle'$s = encode("UTF-8", chr(0x2660)); + from_to($s, "UTF-8", "ascii", Encode::FB_XMLCREF); print $s' | od -c 0000000 342 231 240 \n 0000004

The problem appears to be fixed in 2.13. Using Encode 2.33:

$ perl -MEncode=encode,from_to -wle'$s = encode("UTF-8", chr(0x2660)); + from_to($s, "UTF-8", "ascii", Encode::FB_XMLCREF); print $s' | od -c 0000000 & # x 2 6 6 0 ; \n 0000011

So it's not a solid solution to being with. (You'd have to use decode+encode instead of from_to.) But it's buggy even with a bug-free version of Encode.

1..1 not ok 1 - With XML/HTML 4 digit hex entity # Failed test 'With XML/HTML 4 digit hex entity' # at a.pl line 24. # got: '\u2660\u2660' # expected: '♠\u2660' # Looks like you failed 1 test of 1.
#!/usr/bin/perl -T use strict; use warnings; # 2.13 has a necessary bug fix in from_to use Encode 2.13 qw( encode ); my @tests; BEGIN { @tests = ( [ encode('UTF-8', "♠\x{2660}"), "♠\\u2660", 'Wit +h XML/HTML 4 digit hex entity' ], ); } use Test::More tests => 0+@tests; sub escape_with_encode { return $_[0] unless $_[0] =~ /[\x80-\xff]/; my $s = shift; Encode::from_to($s, "utf-8", "ascii", Encode::FB_XMLCREF); $s =~ s/&#x(....);/\\u$1/g; return $s; } is( escape_with_encode($_->[0]), $_->[1], $_->[2] ) for @tests;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://756165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-16 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found