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

Re: binary test and position?

Thread Previous | Thread Next
Curt Tilmes
February 5, 2019 16:30
Re: binary test and position?
Message ID:
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 =,0,0,1,2,0);
my $needle =,2);

say buf-index($buf, $needle);


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About