develooper Front page | perl.perl5.porters | Postings from January 2012

Re: pack and ASCII

Thread Previous
From:
Eric Brine
Date:
January 10, 2012 13:47
Subject:
Re: pack and ASCII
Message ID:
CALJW-qH7=Eneu15e7LEOamJmFyW6Hnjx7PXdyCepSTsxfq6ZLQ@mail.gmail.com
On Tue, Jan 10, 2012 at 4:33 PM, Eric Brine <ikegami@adaelis.com> wrote:

> Yeah, you're not looking in the right place. You keep looking at how Perl
> stores the string (which is irrelevant) instead of what the string contains.
>

Showing that it's not only irrelevant in theory:

use strict;
use warnings;
use charnames ':full';

use Inline C => <<'__EOI__';

void testing(SV * sv) {
   STRLEN len;
   char*  buf = SvPVbyte(sv, len);
   STRLEN i;

   printf("%u bytes:", len);  // XXX %u is not portable.
   for (i=0; i<len; ++i)
      printf(" %02X", (unsigned int)(unsigned char)(buf[i]));
   printf("\n");
}

__EOI__

sub _u { my $_ = $_[0]; utf8::upgrade($_);   $_ }
sub _d { my $_ = $_[0]; utf8::downgrade($_); $_ }

# One byte in one string format.
testing(_d(pack("A", chr(0xE9))));             # 1 bytes: E9

# One byte in other string format.
testing(_u(pack("A", chr(0xE9))));             # 1 bytes: E9

# Bug: Forgot to encode.
testing(pack("A", "\N{BLACK SPADE SUIT}"));    # DIES with "Wide character
in subroutine entry"

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About