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

Re: binary test and position?

Thread Previous | Thread Next
From:
Curt Tilmes
Date:
February 5, 2019 16:30
Subject:
Re: binary test and position?
Message ID:
CAMv20O_i6xvgbNJ-=o83N8FEAx-1LtskyTnCzt4=JkH69CY=KQ@mail.gmail.com
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

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