in reply to Re: Re: Re: Re: Re: Re: length in bytes of utf8 string
in thread length in bytes of utf8 string

Perl is supposed to treat 0xFC as a single byte. UTF-8 is the oddball here.

  • Comment on Re: Re: Re: Re: Re: Re: Re: length in bytes of utf8 string

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: Re: length in bytes of utf8 string
by bart (Canon) on Jun 27, 2003 at 14:34 UTC
    Perl is supposed to treat 0xFC as a single byte.
    No it's not. There are actually basically two different encodings for the same character: As one byte "\xFC" (ISO-Latin-1 string), and as 2 UTF-8 bytes, "\xC3\xBC" (UTF-8 string). That's one of the most annoying things about Perl's "smart" automatic UTF-8 processing: that it occasionally does the wrong thing, i.e. not what you mean, and that you're left to pick up the pieces.

    These strings are actually marked as different, internally: the latter will have the UTF-8 flag set, the former, not. Perl can turn the former into the latter by joining it with a UTF-8 marked string. I'm not totally convinced that is something to cheer at. But anyway: it may even be a zero length string.

    $x = "\xFC"; # ISO-Latin-1: 1 byte $x .= pack 'U0'; # append a zero length UTF-8 string # which will turn the whole thing into UTF-8! # hex dump: local($\, $,) = ("\n", " "); print map { sprintf "%02X", $_ } unpack 'C*', $x;
    Result:
    C3 BC
    Using tricks like these, one can force perl into submission, even without use bytes (works even for pre 5.8 perls):
    $l1 = pack 'C0a*', $s; # copies the bytes out of $s and marks the r +esult as non-UTF-8 $u8 = pack 'U0a*', $s; # copies the bytes out of $s and marks the r +esult as UTF-8 -- even if the bytes don't form proper UTF-8 strings!
      Ouch!

      Just when I thought I'm starting to undestand... :)

      I thought \xfc is the utf8 code for it! That's because I don't actually have this char on my keyboard and I was using vim's method of entering the utf8 code to actually get the charcter in a particular file. This means tha vim also does Latin-1 to utf8 conversions behind my back. Sad ... or maybe not.



      A determined individual can write garbage code in any language. -- Alan Holub
Re: Re: Re: Re: Re: Re: Re: Re: length in bytes of utf8 string
by mrd (Beadle) on Jun 27, 2003 at 13:51 UTC
    I agree. I thought that written as \x{fc} would get "special treatment".


    A determined individual can write garbage code in any language. -- Alan Holub

      For what its worth, I thought it might as well.