Re: binary test and position?

2019-02-05 Thread ToddAndMargo via perl6-users
On 2/5/19 8:26 AM, Curt Tilmes wrote: If you have glibc (probably yes for Linux or Mac, probably no for Windows), you can call memmem(): use NativeCall; sub memmem(Blob $haystack, size_t $haystacklen,            Blob $needle,   size_t $needlelen --> Pointer) is native {} sub

Re: binary test and position?

2019-02-05 Thread ToddAndMargo via perl6-users
On 2/5/19 7:55 AM, Brad Gilbert wrote: `index` is an NQP op, which means in this case that it is written in C (assuming you are using MoarVM) https://github.com/MoarVM/MoarVM/blob/ddde09508310a5f60c63474db8f9682bc922700b/src/strings/ops.c#L557-L656 The code I gave for finding a Buf inside of

Re: binary test and position?

2019-02-05 Thread Curt Tilmes
If you have glibc (probably yes for Linux or Mac, probably no for Windows), you can call memmem(): use NativeCall; sub memmem(Blob $haystack, size_t $haystacklen, Blob $needle, size_t $needlelen --> Pointer) is native {} sub buf-index(Blob $buffer, Blob $needle) {

Re: binary test and position?

2019-02-05 Thread Brad Gilbert
`index` is an NQP op, which means in this case that it is written in C (assuming you are using MoarVM) https://github.com/MoarVM/MoarVM/blob/ddde09508310a5f60c63474db8f9682bc922700b/src/strings/ops.c#L557-L656 The code I gave for finding a Buf inside of another one was quickly made in a way to

Re: binary test and position?

2019-02-04 Thread ToddAndMargo via perl6-users
On 2/2/19 9:29 PM, Brad Gilbert wrote: Subs do not need to have a `return` statement if it is returning the last value. You also broke the return value of the subroutine that I wrote by assigning it to a variable. What I wrote would return `Nil` if it failed to find a match, yours will return

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

Re: binary test and position?

2019-02-02 Thread ToddAndMargo via perl6-users
> On Sat, Feb 2, 2019 at 10:05 PM ToddAndMargo via perl6-users > wrote: >> >> On 2/2/19 6:09 AM, Brad Gilbert wrote: >>> sub buf-index ( Buf $buf, +@match ) { >>> my $elems = @match.elems; >>> $buf.rotor( $elems => 1 - $elems ).first(* eqv @match.List, :k) >>> }

Re: binary test and position?

2019-02-02 Thread Brad Gilbert
Subs do not need to have a `return` statement if it is returning the last value. You also broke the return value of the subroutine that I wrote by assigning it to a variable. What I wrote would return `Nil` if it failed to find a match, yours will return an undefined `Int`. It should return

Re: binary test and position?

2019-02-02 Thread ToddAndMargo via perl6-users
On 2/2/19 6:09 AM, Brad Gilbert wrote: sub buf-index ( Buf $buf, +@match ) { my $elems = @match.elems; $buf.rotor( $elems => 1 - $elems ).first(* eqv @match.List, :k) } my $buf = Buf[uint8].new(0x4D, 0x5A, 0x90, 0x00, 0x03); say buf-index( $buf, (0x90,

Re: binary test and position?

2019-02-02 Thread ToddAndMargo via perl6-users
> On Fri, Feb 1, 2019 at 11:02 PM ToddAndMargo via perl6-users> wrote: >> >> On 2/1/19 8:26 PM, ToddAndMargo via perl6-users wrote: >>> On 2/1/19 8:07 PM, ToddAndMargo via perl6-users wrote: On 2/1/19 8:03 PM, ToddAndMargo via perl6-users wrote: > > On Fri, Feb 1, 2019 at 9:37 PM

Re: binary test and position?

2019-02-02 Thread Brad Gilbert
sub buf-index ( Buf $buf, +@match ) { my $elems = @match.elems; $buf.rotor( $elems => 1 - $elems ).first(* eqv @match.List, :k) } my $buf = Buf[uint8].new(0x4D, 0x5A, 0x90, 0x00, 0x03); say buf-index( $buf, (0x90, 0x00, 0x03)); # 2 On Fri, Feb 1, 2019 at 11:02 PM

Re: binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
On 2/1/19 8:26 PM, ToddAndMargo via perl6-users wrote: On 2/1/19 8:07 PM, ToddAndMargo via perl6-users wrote: On 2/1/19 8:03 PM, ToddAndMargo via perl6-users wrote:  > On Fri, Feb 1, 2019 at 9:37 PM ToddAndMargo via perl6-users  > wrote:  >>  >> On 2/1/19 7:22 PM, ToddAndMargo via perl6-users

Re: binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
On 2/1/19 8:07 PM, ToddAndMargo via perl6-users wrote: On 2/1/19 8:03 PM, ToddAndMargo via perl6-users wrote:  > On Fri, Feb 1, 2019 at 9:37 PM ToddAndMargo via perl6-users  > wrote:  >>  >> On 2/1/19 7:22 PM, ToddAndMargo via perl6-users wrote:  >>> Hi All,  >>>  >>> On a type Buf, what do I

Re: binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
On 2/1/19 8:03 PM, ToddAndMargo via perl6-users wrote: > On Fri, Feb 1, 2019 at 9:37 PM ToddAndMargo via perl6-users > wrote: >> >> On 2/1/19 7:22 PM, ToddAndMargo via perl6-users wrote: >>> Hi All, >>> >>> On a type Buf, what do I use to check for the >>> position of a byte pattern?

Re: binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
> On Fri, Feb 1, 2019 at 9:37 PM ToddAndMargo via perl6-users > wrote: >> >> On 2/1/19 7:22 PM, ToddAndMargo via perl6-users wrote: >>> Hi All, >>> >>> On a type Buf, what do I use to check for the >>> position of a byte pattern? >>> >>> >>> Many thanks, >>> -T >> >> >> Basically, what am I

Re: binary test and position?

2019-02-01 Thread Brad Gilbert
`eq` is string equality `==` is numeric equality a Buf is neither. You want `eqv` (equivalent) $b[2..4] eqv (0x90,0x00,0x04) On Fri, Feb 1, 2019 at 9:37 PM ToddAndMargo via perl6-users wrote: > > On 2/1/19 7:22 PM, ToddAndMargo via perl6-users wrote: > > Hi All, > > > > On a type Buf,

Re: binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
On 2/1/19 7:37 PM, ToddAndMargo via perl6-users wrote: On 2/1/19 7:22 PM, ToddAndMargo via perl6-users wrote: Hi All, On a type Buf, what do I use to check for the position of a byte pattern? Many thanks, -T Basically, what am I doing wrong here? $ p6 'my $handle=open("filever.exe",

Re: binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
> > On Fri, Feb 1, 2019 at 9:22 PM ToddAndMargo via perl6-users > wrote: >> >> Hi All, >> >> On a type Buf, what do I use to check for the >> position of a byte pattern? >> >> >> Many thanks, >> -T On 2/1/19 7:35 PM, Brad Gilbert wrote: This would work: my $b = Buf.new( 0,0,0, 1, 2, 0 );

Re: binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
On 2/1/19 7:22 PM, ToddAndMargo via perl6-users wrote: Hi All, On a type Buf, what do I use to check for the position of a byte pattern? Many thanks, -T Basically, what am I doing wrong here? $ p6 'my $handle=open("filever.exe", :bin, :ro); my Buf $b; $b= $handle.read(5); say $b; say

Re: binary test and position?

2019-02-01 Thread Brad Gilbert
This would work: my $b = Buf.new( 0,0,0, 1, 2, 0 ); my $match = Buf.new( 1, 2 ); $b.rotor( $match.elems => 1 - $match.elems ).grep(* eqv $match.List, :k) If you only need the first one, swap out `grep` for `first` Another iffy option is to decode it as latin1

binary test and position?

2019-02-01 Thread ToddAndMargo via perl6-users
Hi All, On a type Buf, what do I use to check for the position of a byte pattern? Many thanks, -T