[PATCH] bdiff: implement cffi version of bdiff

Yuya Nishihara yuya at tcha.org
Wed Aug 24 09:20:18 EDT 2016


On Sat, 20 Aug 2016 23:06:32 +0200, Maciej Fijalkowski wrote:
> # HG changeset patch
> # User Maciej Fijalkowski <fijall at gmail.com>
> # Date 1471727161 -7200
> #      Sat Aug 20 23:06:01 2016 +0200
> # Node ID 02c99939ee6a6157de657bf8fffb7b998cd827b0
> # Parent  17bb55a4276ea6d85e10c128fa2b83c7e170e9de
> bdiff: implement cffi version of bdiff

Looks good per comparison with bdiff_module.c.
Queued, thanks.

> +        def bdiff(sa, sb):
> +            a = ffi.new("struct bdiff_line**")
> +            b = ffi.new("struct bdiff_line**")
> +            ac = ffi.new("char[]", sa)
> +            bc = ffi.new("char[]", sb)
> +            l = ffi.new("struct bdiff_hunk*")
> +            try:
> +                an = lib.bdiff_splitlines(ac, len(sa), a)
> +                bn = lib.bdiff_splitlines(bc, len(sb), b)
> +                if not a[0] or not b[0]:
> +                    raise MemoryError
> +                count = lib.bdiff_diff(a[0], an, b[0], bn, l)
> +                if count < 0:
> +                    raise MemoryError
> +                rl = []
> +                h = l.next
> +                la = lb = 0
> +                while h:
> +                    if h.a1 != la or h.b1 != lb:
> +                        lgt = (b[0] + h.b1).l - (b[0] + lb).l
> +                        rl.append(struct.pack(">lll", (a[0] + la).l - a[0].l,
> +                            (a[0] + h.a1).l - a[0].l, lgt))

This appears not to agree with the C implementation [1], in which the maximum
length is UINT_MAX, but pure codes always pack/unpack them as signed integers.
So ">lll" should be right.

 [1]: https://selenic.com/repo/hg/rev/d6fb7bbec16a


More information about the Mercurial-devel mailing list