in reply to string comparison with escape sequences
I think the problem is with identifying whether a given series of bytes is utf8 or not. It's not hard to take a string with escape sequences and turn it into the corresponding bytes. Consider:
use Test::More 'no_plan'; my $escaped = 'st\x{f9}'; my $bytes = "st\x{f9}"; ok( $escaped ne $bytes, '$escaped ne $bytes' ); my $unescaped = $escaped; $unescaped =~ s{ \\x\{ ([a-f0-9]{2}) \} }{ chr hex $1 }xmseg; is( $unescaped, $bytes, '$unescaped eq $bytes' );
My question is, what do you do with a string like "\\x{f9}\x{263a}"? It contains a utf8 character, and it also contains what looks like an escape sequence. It must be a utf8 string, so it should not be unescaped. Now imagine we chop off that last utf8 character. It's still a utf8 string, but all you see is an escape sequence.
In short, if you don't know the character encoding of the bytes you receive, it's hard to interpret them.
All that having been said, I doubt a user is going to feed you anything that matches /\\x\{[a-f0-9]{2}\}/ as a literal string, so it's pretty safe to assume that if that's in the string somewhere, you need to unescape it. Even so, I can't recommend roaming through a stream of bits, whose character encoding you don't know, changing pieces of it, and calling that some kind of progress.
|
|---|