develooper Front page | perl.perl6.users | Postings from February 2019

Re: binary test and position?

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
February 6, 2019 04:49
Subject:
Re: binary test and position?
Message ID:
e64e90eb-e144-f8b6-a808-1e5071519062@zoho.com
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 buf-index(Blob $buffer, Blob $needle) {
>      (memmem($buffer, $buffer.bytes, $needle, $needle.bytes) // return)
>          - nativecast(Pointer, $buffer)
> }
> 
> my $buf = Buf.new(0,0,0,1,2,0);
> my $needle = Buf.new(1,2);
> 
> say buf-index($buf, $needle);
> 
> Curt
> 

Thank you!

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About