Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Fixing utf8 errors

by graff (Chancellor)
on Oct 31, 2015 at 04:07 UTC ( [id://1146553]=note: print w/replies, xml ) Need Help??


in reply to Fixing utf8 errors

Can you define or describe what "bad encoding (for my purposes)" refers to? Can you provide or describe a few samples of "bad encoding" and "ok encoding"?

Is the string that is passed to "_checkit" supposed to be a (perl-internal) UTF8 string? Has STDOUT been set to use UTF8 encoding before "_checkit" is called?

When the error message from a failed eval matches /U\+(\S+)/, what is the full text of the error message? (Do you get error messages that don't match that regex, and if so, what are they?) Please be explicit.

The point is that I don't understand what sort of error you're "catching". There probably is a better way to catch it, but since I don't know what you're dealing with, I don't know what to suggest.

A few lines of code that reads a few lines of relevant __DATA__ and calls "_checkit" would answer most or all of my questions.

Replies are listed 'Best First'.
Re^2: Fixing utf8 errors
by Rodster001 (Pilgrim) on Oct 31, 2015 at 16:07 UTC
    Here is an example:
    print Dumper $text; $VAR1 = { 'string' => "Text\x{daed}", }; print $text->{'string'}; 'Unicode surrogate U+DAED is illegal in UTF-8 at line 5.
      It's possible for UTF8-encoded text to contain code points in the range U+D800 - U+DFFF (Surrogate Pair Area), but this is always a sign that something bad was done to the data when it was converted into UTF8 encoding.

      I don't recall offhand what particular mistake(s) in transcoding would cause these code points to show up in UTF8 data, but there's a chance that your data originated in some 16-bit (fixed-width) encoding that was incorrectly converted to UTF8, and there may be better ways of "fixing" the problem than just deleting these odd characters.

      Still, if your experience with the data in question indicates that deletion is the "right thing" to do, it's easy to detect and "fix" the problem:

      # Assuming $string is utf-8 endcoded: if ( my @badch = ( $string =~ /[\x{d800}-\x{dfff}]/g )) { warn sprintf( "%d surrogate code point(s) found\n", scalar @badch +); $string =~ tr/\x{d800}-\x{dfff}//d; }
      (Update - forgot to mention: you can read more about surrogate-pair characters in perlunicode. It may be that your UTF8 data started out as UTF16, but was incorrectly treated as if it were UCS2 when it was converted to UTF8. Converting to UTF8 in the correct way would yield different results.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found