Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Bypass utf-8 encoding/decoding?

by ikegami (Pope)
on Nov 30, 2017 at 20:21 UTC ( #1204631=note: print w/replies, xml ) Need Help??


in reply to Bypass utf-8 encoding/decoding?

Replace
SV* my_c_function(SV* sv) { STRLEN len; const char* s = SvPVbyte(sv, len); ... return newSV(...); }

with

SV* my_c_function(SV* sv) { STRLEN len; const char* s = SvPVutf8(sv, len); ... return newSVpvn_utf8(..., 1); }

Example:

use strict; use warnings; use feature qw( say ); use open ":std", ":encoding(UTF-8)"; use Inline C => <<'__EOS__'; static const char hex_syms[16] = { '0', '1', '2', '3', '4', '5', '6', +'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; static void my_c_function(char* dst, const char* src, STRLEN n) { while (n--) { *(dst++) = hex_syms[ ((unsigned char)*src) >> 4 ]; *(dst++) = hex_syms[ *src & 0xF ]; *(dst++) = '.'; ++src; } dst[-1] = 0; } SV* my_xs_function(SV* buf_sv) { STRLEN buf_len; const char* buf = SvPVutf8(buf_sv, buf_len); if (buf_len == 0) return newSVpvs(""); { STRLEN hex_len = buf_len * 3 - 1; char* hex; SV* hex_sv; Newx(hex, hex_len, char); my_c_function(hex, buf, buf_len); hex_sv = newSVpvn_utf8(hex, hex_len, 1); Safefree(hex); return hex_sv; } } __EOS__ my $s = "\x{C9}ric"; utf8::downgrade( my $d = $s ); # Let's test with both utf8::upgrade( my $u = $s ); # string storage formats. say $u eq $d ? "Same" : "Different"; for my $s ($d, $u) { say "UCP: ", sprintf("%vX", $s); # C9.72.69.63 say "UTF-8: ", my_xs_function($s); # C3.89.72.69.63 }

Optimized: (Avoids creating two buffers and copying one into the other. Also protects against memory leaks from long jumps in the C code by mortalizing the allocated memory sooner.)

SV* my_xs_function(SV* buf_sv) { STRLEN buf_len; const char* buf = SvPVutf8(buf_sv, buf_len); if (buf_len == 0) return newSVpvs(""); { STRLEN hex_len = buf_len * 3 - 1; SV* hex_sv = sv_2mortal(newSV(hex_len)); SvPOK_on(hex_sv); SvCUR_set(hex_sv, hex_len); SvUTF8_on(hex_sv); my_c_function(SvPVX(hex_sv), buf, buf_len); return hex_sv; } }

Replies are listed 'Best First'.
Re^2: Bypass utf-8 encoding/decoding?
by karlgoethebier (Monsignor) on Dec 01, 2017 at 08:48 UTC

    But isn't use utf8; mandatory before calling the open pragma?

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      No. use utf8; tells Perl that the source code contains UTF-8 characters (and you can use them in identifiers, too). It's in no way related to how external data are encoded/decoded.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Quite right, of course. But I was surprised today while playing with using the official name for Unicode characters to find that it's not needed in that case:

        use strict; use warnings; use feature 'say'; #use utf8; use open qw( :encoding(UTF-8) :std ); say "\N{FATHER CHRISTMAS} thanks you for all your hard work tonight"; __END__

        🎅 thanks you for all your hard work tonight
        

        use strict; use warnings; use feature 'say';
        #use utf8;
        use open qw( :encoding(UTF-8) :std );
        say "🎅 thanks you for all your hard work tonight";
        __END__
        

        ğŸŽ… thanks you for all your hard work tonight
        


        The way forward always starts with a minimal test.
        "...no way related..."

        I begin to see it clearly now ;-) Thanks and best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      use utf8; simply tells Perl that your source code is encoded using UTF-8 (as opposed to ASCII). It would have no effect in my program.

      Furthermore, the subs provided by the module are always loaded, so I don't need to explicity load the module (use utf8 ();) to access them.

        "...no effect..."

        Yes. Thank you very much ikegami 👍. choroba already advised. Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1204631]
help
Chatterbox?
and not a whimper to be heard...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2018-08-15 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (158 votes). Check out past polls.

    Notices?