Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] Fix hexdump command used for mercurial dirstate parsing



Daniel Shahaf wrote on Sun, Oct 30, 2016 at 15:55:30 +0000:
> Frank Terbeck wrote on Sun, Oct 30, 2016 at 12:11:01 +0100:
> > It would be conceivable to make the command that is called in these
> > places configurable via styles. But that is a separate issue to the
> > one that fixes our use of hexdump.
>
> Couldn't we replace hexdump by a pure zsh solution?  We simply need to
> read 20 octets and print their hexadecimal values.

This seems to do the job:

% f() {
    local fname=$1 bytecount=$2
    # Read 20 bytes
    setopt localoptions nomultibyte
    integer fd; local val
    exec {fd}<$fname
    read -k $bytecount -r -u $fd val
    exec {fd}<&-
    # Convert them to hex
    setopt localoptions extendedglob
    local -r -A pad=( $'\0' 0 $'\1' 0 $'\2' 0 $'\3' 0 $'\4' 0 $'\5' 0 $'\6' 0 $'\7' 0 $'\10' 0 $'\11' 0 $'\12' 0 $'\13' 0 $'\14' 0 $'\15' 0 $'\16' 0 $'\17' 0 )
    REPLY=${(L)val//(#b)(?)/$pad[$match[1]]$(( [##16] ##${match[1]} ))}
}
% f dirstate 20 
% typeset -p REPLY
typeset REPLY=77bba665e970146bd2be0b2da40092e340408804
% xxd -l20 -p < dirstate
77bba665e970146bd2be0b2da40092e340408804
% 

Would something along these lines be preferable?

Thanks to Mikael for the inner loop.

Cheers,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author