(x,y)
Show differences between 2 binary strings
Show differences between 2 binary strings
Show differences between 2 binary strings
def hexdiff(x,y):
    """Show differences between 2 binary strings"""
    x=str(x)[::-1]
    y=str(y)[::-1]
    SUBST=1
    INSERT=1
    d={}
    d[-1,-1] = 0,(-1,-1)
    for j in range(len(y)):
        d[-1,j] = d[-1,j-1][0]+INSERT, (-1,j-1)
    for i in range(len(x)):
        d[i,-1] = d[i-1,-1][0]+INSERT, (i-1,-1)

    for j in range(len(y)):
        for i in range(len(x)):
            d[i,j] = min( ( d[i-1,j-1][0]+SUBST*(x[i] != y[j]), (i-1,j-1) ),
                          ( d[i-1,j][0]+INSERT, (i-1,j) ),
                          ( d[i,j-1][0]+INSERT, (i,j-1) ) )
                          

    backtrackx = []
    backtracky = []
    i=len(x)-1
    j=len(y)-1
    while not (i == j == -1):
        i2,j2 = d[i,j][1]
        backtrackx.append(x[i2+1:i+1])
        backtracky.append(y[j2+1:j+1])
        i,j = i2,j2

        

    x = y = i = 0
    colorize = { 0: lambda x:x,
                -1: conf.color_theme.left,
                 1: conf.color_theme.right }
    
    dox=1
    doy=0
    l = len(backtrackx)
    while i < l:
        separate=0
        linex = backtrackx[i:i+16]
        liney = backtracky[i:i+16]
        xx = sum(len(k) for k in linex)
        yy = sum(len(k) for k in liney)
        if dox and not xx:
            dox = 0
            doy = 1
        if dox and linex == liney:
            doy=1
            
        if dox:
            xd = y
            j = 0
            while not linex[j]:
                j += 1
                xd -= 1
            print colorize[doy-dox]("%04x" % xd),
            x += xx
            line=linex
        else:
            print "    ",
        if doy:
            yd = y
            j = 0
            while not liney[j]:
                j += 1
                yd -= 1
            print colorize[doy-dox]("%04x" % yd),
            y += yy
            line=liney
        else:
            print "    ",
            
        print " ",
        
        cl = ""
        for j in range(16):
            if i+j < l:
                if line[j]:
                    col = colorize[(linex[j]!=liney[j])*(doy-dox)]
                    print col("%02X" % ord(line[j])),
                    if linex[j]==liney[j]:
                        cl += sane_color(line[j])
                    else:
                        cl += col(sane(line[j]))
                else:
                    print "  ",
                    cl += " "
            else:
                print "  ",
            if j == 7:
                print "",


        print " ",cl

        if doy or not yy:
            doy=0
            dox=1
            i += 16
        else:
            if yy:
                dox=0
                doy=1
            else:
                i += 16
def hexdiff ( x , y ) : x = str ( x ) [ : : - 1 ] y = str ( y ) [ : : - 1 ] SUBST = 1 INSERT = 1 d = { } d [ - 1 , - 1 ] = 0 , ( - 1 , - 1 ) for j in range ( len ( y ) ) : d [ - 1 , j ] = d [ - 1 , j - 1 ] [ 0 ] + INSERT , ( - 1 , j - 1 ) for i in range ( len ( x ) ) : d [ i , - 1 ] = d [ i - 1 , - 1 ] [ 0 ] + INSERT , ( i - 1 , - 1 ) for j in range ( len ( y ) ) : for i in range ( len ( x ) ) : d [ i , j ] = min ( ( d [ i - 1 , j - 1 ] [ 0 ] + SUBST * ( x [ i ] != y [ j ] ) , ( i - 1 , j - 1 ) ) , ( d [ i - 1 , j ] [ 0 ] + INSERT , ( i - 1 , j ) ) , ( d [ i , j - 1 ] [ 0 ] + INSERT , ( i , j - 1 ) ) ) backtrackx = [ ] backtracky = [ ] i = len ( x ) - 1 j = len ( y ) - 1 while not ( i == j == - 1 ) : i2 , j2 = d [ i , j ] [ 1 ] backtrackx . append ( x [ i2 + 1 : i + 1 ] ) backtracky . append ( y [ j2 + 1 : j + 1 ] ) i , j = i2 , j2 x = y = i = 0 colorize = { 0 : lambda x : x , - 1 : conf . color_theme . left , 1 : conf . color_theme . right } dox = 1 doy = 0 l = len ( backtrackx ) while i < l : separate = 0 linex = backtrackx [ i : i + 16 ] liney = backtracky [ i : i + 16 ] xx = sum ( len ( k ) for k in linex ) yy = sum ( len ( k ) for k in liney ) if dox and not xx : dox = 0 doy = 1 if dox and linex == liney : doy = 1 if dox : xd = y j = 0 while not linex [ j ] : j += 1 xd -= 1 print colorize [ doy - dox ] ( "%04x" % xd ) , x += xx line = linex else : print "    " , if doy : yd = y j = 0 while not liney [ j ] : j += 1 yd -= 1 print colorize [ doy - dox ] ( "%04x" % yd ) , y += yy line = liney else : print "    " , print " " , cl = "" for j in range ( 16 ) : if i + j < l : if line [ j ] : col = colorize [ ( linex [ j ] != liney [ j ] ) * ( doy - dox ) ] print col ( "%02X" % ord ( line [ j ] ) ) , if linex [ j ] == liney [ j ] : cl += sane_color ( line [ j ] ) else : cl += col ( sane ( line [ j ] ) ) else : print "  " , cl += " " else : print "  " , if j == 7 : print "" , print " " , cl if doy or not yy : doy = 0 dox = 1 i += 16 else : if yy : dox = 0 doy = 1 else : i += 16