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);


Thank you!

Reply via email to