reassigning values to variables question

2019-02-03 Thread ToddAndMargo via perl6-users

Hi All,

If I have a variable of type Buf which 1000 bytes in it
and I find the five bytes I want, is it faster, slower,
or no difference in speed to overwrite the same variable
with the five bytes?  Or is it faster to put the five bytes
from the first variable into a second variable?

Many thanks,
-T


Re: "read" options?

2019-02-03 Thread ToddAndMargo via perl6-users

On 2/3/19 8:46 PM, Norman Gaywood wrote:


On Mon, 4 Feb 2019 at 15:12, ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> wrote:


https://docs.perl6.org/routine/read

Where is the list of the options this thing will take, such
as :ro and :bin?


Those are options for open()

https://docs.perl6.org/routine/open


Thank you!


Re: "read" options?

2019-02-03 Thread Norman Gaywood
On Mon, 4 Feb 2019 at 15:12, ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> https://docs.perl6.org/routine/read
>
> Where is the list of the options this thing will take, such
> as :ro and :bin?
>
>
Those are options for open()

https://docs.perl6.org/routine/open

-- 
Norman Gaywood, Computer Systems Officer
School of Science and Technology
University of New England
Armidale NSW 2351, Australia

ngayw...@une.edu.au  http://turing.une.edu.au/~ngaywood
Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062

Please avoid sending me Word or Power Point attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html


"read" options?

2019-02-03 Thread ToddAndMargo via perl6-users

Hi All,

https://docs.perl6.org/routine/read

Where is the list of the options this thing will take, such
as :ro and :bin?


Many thanks,
-T


Re: decode options?

2019-02-03 Thread ToddAndMargo via perl6-users

On 2/3/19 4:56 PM, ToddAndMargo via perl6-users wrote:

Hi All,

Uhhh,

     https://docs.perl6.org/routine/decode

    role Blob

    From Blob
    (Blob) method decode

    Defined as:

    multi method decode(Blob:D: Str:D $encoding = 'UTF-8' --> Str:D)

    Applies an encoding to turn the blob into a Str.

    my Blob $blob = "string".encode('utf-8');
    say $blob.decode('utf-8'); # OUTPUT: «string␤»

    On malformed utf-8 .decode will throw X::AdHoc. To handle
    sloppy utf-8 use utf8-c8.

where are the decode options?  I only see utf-8 and utf8-c8.
Am I blind?


Many thanks,
-T


Found it and I am not blind.  The Docs just stink.  I would
write an RFE for this, but, the odds of them accepting it are
so close to zero that it is not worth the effort.

And what is with?
 Str:D $encoding = 'UTF-8'

$encoding can accept a whole bunch of things.  And
What the heck is `'UTF-8'`?  It sure ain't "utf8" or
Rakudo's "utf-8".


My own docs:

https://docs.perl6.org/type/IO::Handle#___top

   utf8
   utf16
   utf16le
   utf16be
   utf8-c8
   iso-8859-1
   windows-1251
   windows-1252
   windows-932
   ascii

   Rakudo extensions:

  latin-1 (iso-8859-1)
  utf-8
  utf-16


Re: Fastest way to convert from a Buf to a Str?

2019-02-03 Thread ToddAndMargo via perl6-users

On 2/3/19 5:26 PM, Darren Duncan wrote:

On 2019-02-02 7:22 PM, ToddAndMargo via perl6-users wrote:

I need to read a file into a buffer (NO CONVERSIONS!)
and then convert it to a string (again with no
conversions).


I think you're making an impossible request.  


Don't forget that I think everywhere on this list is
a bloody genius.

If preserving exact bytes 
is important, then you want to keep your data in a type that represents 
a sequence of bytes, such as Blob of Buf.  A Str represents a sequence 
of characters, which are NOT bytes, so if you're wanting to have a Str 
that is saying you don't care about the bytes.  Given what you keep 
saying, I'd say skip the Str and just use Buf or Blob etc full stop. -- 
Darren Duncan




Hi Darren,

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

Does the trick, but it takes up to 15 seconds.  Way
too slow.

I have another post looking to see if any of the other
decodes will work.  So maybe...

My big issue is that the data I am looking through uses four
nuls in a row as a delimiter.  If these get dropped, I
won't be able to find anything.

Your idea about just skipping Str is along the line I have
also been thinking.  Brad has been helping me with "index" for
a Buf.  I haven't had a shot at trying his corrections to my
code yet.

-T


Re: Fastest way to convert from a Buf to a Str?

2019-02-03 Thread Darren Duncan

On 2019-02-02 7:22 PM, ToddAndMargo via perl6-users wrote:

I need to read a file into a buffer (NO CONVERSIONS!)
and then convert it to a string (again with no
conversions).


I think you're making an impossible request.  If preserving exact bytes is 
important, then you want to keep your data in a type that represents a sequence 
of bytes, such as Blob of Buf.  A Str represents a sequence of characters, which 
are NOT bytes, so if you're wanting to have a Str that is saying you don't care 
about the bytes.  Given what you keep saying, I'd say skip the Str and just use 
Buf or Blob etc full stop. -- Darren Duncan


decode options?

2019-02-03 Thread ToddAndMargo via perl6-users

Hi All,

Uhhh,

https://docs.perl6.org/routine/decode

   role Blob

   From Blob
   (Blob) method decode

   Defined as:

   multi method decode(Blob:D: Str:D $encoding = 'UTF-8' --> Str:D)

   Applies an encoding to turn the blob into a Str.

   my Blob $blob = "string".encode('utf-8');
   say $blob.decode('utf-8'); # OUTPUT: «string␤»

   On malformed utf-8 .decode will throw X::AdHoc. To handle
   sloppy utf-8 use utf8-c8.

where are the decode options?  I only see utf-8 and utf8-c8.
Am I blind?


Many thanks,
-T


Re: "index" source code?

2019-02-03 Thread Brad Gilbert
The Rakudo Perl6 compilier is written in Perl6 (and a subset of Perl6 named NQP)

The `index` subroutines are at
https://github.com/rakudo/rakudo/blob/64c88f919841c58f5a6dffd3581770e06a8fd6a5/src/core/Cool.pm6#L276-L282

proto sub index($, $, $?, *%) {*}
multi sub index(Cool $s, Cool $needle --> Int:D) {
$s.index($needle)
}
multi sub index(Cool $s, Cool $needle, Cool $pos --> Int:D) {
$s.index($needle,$pos)
}

They basically only call the `index` and `rindex` methods

To keep from showing you all of the methods, they all coerce the value
to Str then call `index` on that.

The location of the Str.index methods is
https://github.com/rakudo/rakudo/blob/3d581c8d23e0b47fd09616a1165f84568531a4aa/src/core/Str.pm6#L255-L284

proto method index(|) {*}
multi method index(Str:D: Cool:D $needle --> Int:D) {
self.index: $needle.Str
}
multi method index(Str:D: Str:D $needle --> Int:D) {
nqp::if(
  nqp::islt_i((my int $i =
nqp::index($!value,nqp::getattr($needle,Str,'$!value'))),
0
  ),
  Nil,
  nqp::p6box_i($i)
)
}
multi method index(Str:D: Cool:D $needle, Cool:D $pos --> Int:D) {
self.index: $needle.Str, $pos.Int
}
multi method index(Str:D: Str:D $needle, Int:D $pos --> Int:D) {
nqp::if(
  nqp::isbig_I(nqp::decont($pos)) || nqp::islt_i($pos,0),
  self!INDEX-OOR($pos),
  nqp::if(
nqp::islt_i((my int $i = nqp::index(
  $!value,nqp::getattr($needle,Str,'$!value'),$pos
)),0),
Nil,
nqp::p6box_i($i)
  )
)
}

Note that they heavily use NQP opcodes, so you shouldn't try and copy them.

On Sun, Feb 3, 2019 at 12:35 AM ToddAndMargo via perl6-users
 wrote:
>
> Hi All,
>
> Was the subroutine "index" written Perl6?  If so,
> where can I view the source code?
>
> Many thanks,
> -T


Re: binary test and position?

2019-02-03 Thread ToddAndMargo via perl6-users

On 2/2/19 9:29 PM, Brad Gilbert wrote:

It is also weird that you are using CamelCase for variables,
and a mixture of CamelCase and snake-case for the subroutine name.


Hi Brad,

An explanation.  I do this for "maintainability".

I have been able to "type" since high school typing
class.   Upper and lower are no difference in speed
to me.  (I do realize that "hunt and peckers" go nuts
with upper and lower case.)

So, Camel Case tells me instantly that a variable and
sub name came from me and are not something I copied
from somewhere else (zef), or a reserved word or a
build-in sub name.  There is no "Did I write this
or ...?" when I go months later to fix/add some
something.

Since I come from Modula 2, I live and breath modules.
Awful nice to be able to know at a glance if I wrote
something or I used someone else's module.

And I absolutely ADORE the way Perl 6 does their modules.
Well, importing them needs some work.  They need to
restore Perl 5's functionality where you can explicitly
state which subs you are importing from a module:

 use Term::ANSIColor qw ( BOLD BLUE RED GREEN RESET );
 use Term::ReadKey qw ( ReadKey ReadMode );

Again, for maintainability, so I know where the blazes
something came from.

I work around the problem with my own imposed comments:

 use CurlUtils;# qw[ CurlDownloadFile  CurlGetWebSite 
CurlGetHeader CurlExists CurlSendMail, CurlGetRedirectUrl ];


and

   #`{

 Interface to curl from Perl 6

 To use these, place the following at the top(ish) of your program
use lib "/home/linuxutil";
use CurlUtils;  # qx[ CurlDownloadFile  CurlGetWebSite 
CurlGetHeader CurlExists CurlSendMail ]

  }

Oh ya, and I live and die in "Top Down", so I GOT TO HAVE my subs!

Perl is a "Write Only language" if you let it be.

-T


Re: Fastest way to convert from a Buf to a Str?

2019-02-03 Thread ToddAndMargo via perl6-users

On 2/3/19 1:55 AM, David Warring wrote:

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


Hi David,

It has to be an exact match.  That includes all carriage returns,
line feeds, page feeds, eofs, tabs, etc..  But thank you anyway.
:-)

-T


Re: Fastest way to convert from a Buf to a Str?

2019-02-03 Thread David Warring
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  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
>
> say $c.encode; # utf8:0x
>
> 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
>  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  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  at -e line 1
> >
> >
> > Many thanks,
> > -T
> >
> > --
> > 
> > A computer without Microsoft is like
> > a chocolate cake without the mustard
> > 
>