Looks okay to me. I thought your test string might be a bit too easy (didn't cover enough possible trouble makers), and I wondered whether putting "decode_entities" before the cp1252 lookup might cause a problem (because when you decode entities like "Ñ" ("Ñ"), you get utf8 byte sequences that include bytes like 0x91, which might get mistreated by the cp1252_lookup).
But then I tried it out, adding "Ñ" and "Ò" to the test string, and they magically came out right:
...
my $str = join('',
chr(0x93), 'double', chr(0x94), ' Ñ Ò ',
chr(0x201C), 'double', chr(0x201D),
'‘single’'
);
...
output:
“double” Ñ Ò “double”‘sing
+le’
which looks like what you would want to get.
Update: based on your reply, I figured it might make sense to try numeric character entities above 0xff -- e.g. Ǒ and ǒ (when converted to utf8, these have 0x91 and 0x92 as the second byte). It still works the way you would want, converting them correctly to hex-coded numeric entities (Ǒ and ǒ, upper and lower case letter o with caron, respectively). | [reply] [Watch: Dir/Any] [d/l] |
93 64 6f 75 62 6c 65 94
d1 20 d2
201c 64 6f 75 62 6c 65 201d
2018 73 69 6e 67 6c 65 2019
xd1 and xd2 are outside the range being checked (x80-x9F), are legal unicode (the cp1252/unicode chart gives the same codes) and encode_entities returned (as you found):
Ñ Ò
Note that, for example, ’ returned x2019 so wouldn't be mucked about by the cp1252 replacement.
The comment in the script:
# "replaces HTML entities...
# with the corresponding Unicode character"
is from the H::E doc and had more significance than I first realised.
__But__ I too am surprised it appears to work. I wrote quite a bit of code to process utf8 and was almost a bit miffed that it seemed unnecessary!
This my first outing in these waters so will be pleased to be corrected if I've got any of this tangled up.
Again, thanks for your comments,
John
unicode.org cp1252 chart
update:
Extract from the chart:
cp1252 unicode
0xD1 0x00D1 #LATIN CAPITAL LETTER N WITH TILDE
0xD2 0x00D2 #LATIN CAPITAL LETTER O WITH GRAVE
| [reply] [Watch: Dir/Any] [d/l] [select] |