It does this in a very naieve way, BTW, that is only valid if the input text is in the code page whose characters 0x80 - 0xFF correspond to Unicode code points U+0080 to U+00FF, which is to say not many of them.

And Perl is exactly this naive as well. You can get this exact same result without writing any bit-twiddling Perl code by instead convincing Perl to promote the string to UTF-8, and then storing the resulting bytes into a Perl byte string (or by just turning off the "is UTF-8" bit on that Perl scalar). For example:

#!/usr/bin/perl -w use strict; require utf8; my $s= pack "C*", 1..255; # Byte string to convert my $u= pack "U*", 1..255; # UTF-8 string my $e= substr($u,0,0); # Empty UTF-8 string my $r= $s; # Convert using regex $r =~ s{ ([^\0-\x7F]) }{ my $o= ord($1); sprintf "%c%c", 0xc0 | ( $o >> 6 ), 0x80 | ( $o & 0x3f ); }gex; my $i= $s.$e; # Convert by implicit upgrade to UTF-8 my $f= $s; # Upgrade via utf8.pm function utf8::upgrade( $f ); my $b= $s; # Upgrade then mark as bytes utf8::encode( $b ); if( $r eq $b ) { print "The regex and utf8::encode() match.\n"; } if( $u eq $i && $i eq $f ) { print "The 3 Unicode strings match.\n"; } if( join(" ",unpack"C*",$r) eq join(" ",unpack"C*",$i) ) { print "The byte- and unicode-strings have the same bytes.\n"; } if( $r ne $i ) { print "The byte- and unicode-strings are not equal.\n"; } print '$s contains ', length($s), " bytes.\n"; print '$i contains ', length($i), " characters.\n"; print '$r contains ', length($r), " bytes.\n";

Which produces:

The regex and utf8::encode() match. The 3 Unicode strings match. The byte- and unicode-strings have the same bytes. The byte- and unicode-strings are not equal. $s contains 255 bytes. $i contains 255 characters. $r contains 383 bytes.

The regex is different in that it doesn't mollest null bytes. If you change "1..255" to "0..255" in the above code, you'll see that when Perl (v5.8.7 on Win32, anyway) converts a byte string to Unicode, it just unceremoniously stops at any bytes of value 0.

- tye        


In reply to Re^2: Intra-Unicode Conversions (naive) by tye
in thread Intra-Unicode Conversions by kettle

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.