> On Sat, Feb 2, 2019 at 9:23 PM ToddAndMargo via perl6-users
> <perl6-users@perl.org> wrote:
>> Hi All,
>> I need to read a file into a buffer (NO CONVERSIONS!)
>> and then convert it to a string (again with no
>> conversions).
>> I have been doing this:
>>      for ( @$BinaryFile ) -> $Char { $StrFile ~= chr($Char); }
>> But it takes a bit of time.  What is the fastest way to do this?
>> I guess there is not a way to create/declare a variable that is
>> both Buf and Str at the same time?  That would mean I did not
>> have to convert anything.  I use to get away with this under
>> Module 2 all the time.
>> $ p6 'my $B = Buf.new(0x66, 0x66, 0x77); $B.Str ~= "z";'
>> Cannot use a Buf as a string, but you called the Str method on it
>>     in block <unit> at -e line 1
>> $ p6 'my $B = Buf.new(0x66, 0x66, 0x77); Str($B) ~= "z";'
>> Cannot use a Buf as a string, but you called the Str method on it
>>     in block <unit> at -e line 1
>> Many thanks,
>> -T

On 2/2/19 10:15 PM, Brad Gilbert wrote:

     for ( @$BinaryFile ) -> $Char { $StrFile ~= chr($Char); }

is better written as

     my $StrFile = $BinaryFile.map(*.chr).reduce(* ~ *);

It is also exactly equivalent to just e

     # if $BinaryFile is a Buf
     my $StrFile = $BinaryFile.decode('latin1');

     # if it isn't
     my $StrFile = Buf.new($BinaryFile).decode('latin1');

If you don't otherwise need $BinaryFile

     my $fh = open 'test', :enc('latin1');
     my $StrFile = $fh.slurp;


     my $StrFile = 'test'.IO.slurp(:enc('latin1'));


Buf and Str used to be treated more alike, and it was very confusing.

There should be more methods on Buf that work like the methods on Str,
but that is about it.

Having a string act like a buffer in Modula 2 probably works fine
because it barely supports Unicode at all.

Here is an example of why it can't work like that in Perl6:

     my $a = 'a';
     my $b = "\c[COMBINING ACUTE ACCENT]";

     my $c = $a ~ $b;
     my $d = $a.encode ~ $b.encode;
     my $e = Buf.new($a.encode) ~ Buf.new($b.encode);

     say $a.encode; # utf8:0x<61>
     say $b.encode; # utf8:0x<CC 81>

     say $c.encode; # utf8:0x<C3 A1>

     say $d; # utf8:0x<61 CC 81>
     say $e; # Buf:0x<61 CC 81>

Notice that `$c.encode` and `$d` are different even though they are
made from the same parts.
`$d` and `$e` are similar because they are dealing with lists of
numbers not strings.

Hi Brad,

Thank you!

I want ZERO decoding.  I want exactly the same bytes in the
string as are in the Buffer.  And it has to be done FAST.

Are you saying this the fastest way?

    my $StrFile = $BinaryFile.map(*.chr).reduce(* ~ *);

Please keep in mind.  NO DECODING!


Reply via email to