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 3, 2019 04:05
Subject:
Re: binary test and position?
Message ID:
a7297736-9465-5b9c-c3d7-f1cd08c42b09@zoho.com
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, 0x00, 0x03)); # 2

What did I do wrong?

First I did a byte wise conversion of

    Buf $BinaryFile   to   Str $StrFile

and

    Buf $VersionInfoBuf  to  Str $VersionInfoStr



sub Buf-Index ( Buf $Buffer, +@SubBuf ) {
    # `index` for buffers
    # $Buffer is the buffer to search through
    # $ +@SubBuf is the sub buffer pattern to search for in $Buffer
    # returns the first instance of a match, Nil if no match

    my Int $Position = Nil;
    my $Elems = @SubBuf.elems;

    $Position = $Buffer.rotor( $Elems => 1 - $Elems ).first( * eqv 
@SubBuf.List, :k );
    return $Position;
}

    $i  = index(     $StrFile,    $VersionInfoStr );
    $bi = Buf-Index( $BinaryFile, $VersionInfoBuf );
    say "i = <$i>   bi = <$bi>";




$ FileVer.pl6
i = <11371>   bi = <>


11371 is correct.



What did I do wrong?

Many thanks,
-T

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