Is this a valid sub to check if an UTF8 strings contains chars that can be validly translated to Latin-1?
I don't think the following 2 subs take into account all the latin-1 chars which doing tr one way or the other It doesn't include Latin-1 chars that can be represented like /[\x{C0}-\x{C3}][\x{80}-\x{FF}]. How do we write these better to correctly do the translations?sub validUTF8{ ($_)=@_; use utf8; my @a=split "",$_; foreach (@a) { # if (/[\0-\x{ff}]/ or /[\x{C0}-\x{C3}][\x{80}-\x{FF}]/){ + if (/[\0-\x{ff}]/ or /[\x{C2}-\x{C3}][\x{80}-\x{FF}]/){ } else { print STDERR "BAD:$_\n"; no utf8; return 0; } } no utf8; return 1; } #converts from utf8 to latin1
sub utf8tolatin1 { ($_)=@_; use utf8; tr/\0-\x{ff}//UC; #change utf8 to Latin-1 no utf8; return $_; } #converts from latin1 to utf8 sub latin1toutf8 { ($_)=@_; use utf8; tr/\0-\0xff//CU; #change Latin-1 to utf8 no utf8; return $_; }
In reply to UTF8 to Latin-1 and back using Perl-6 by kavita
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |