Are all characters in the range 0-255, ie latin-1 characters?

You could then try: my $str =  $buf.decode("latin-1");

There's one potential  issue if your data could contain DOS end of lines
("\r\n"), which will get translated to a single logical "\n" in the decoded
string.

- David


On Sun, Feb 3, 2019 at 7:16 PM Brad Gilbert <b2gi...@gmail.com> wrote:

> This:
>
>     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;
>
> or
>
>     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.
>
> 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
> >
> > --
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > A computer without Microsoft is like
> > a chocolate cake without the mustard
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>

Reply via email to