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.


In reply to Re: string comparison with escape sequences by kyle
in thread string comparison with escape sequences by danmcb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.