From df0f38af83f972f026d7e14150f3d37b95f13484 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Wed, 14 Dec 2016 13:46:43 +0100 Subject: packfile: improve delta copy operation encoding code (#186) --- plumbing/format/packfile/diff_delta.go | 44 ++++++++++------------------------ 1 file changed, 13 insertions(+), 31 deletions(-) (limited to 'plumbing') diff --git a/plumbing/format/packfile/diff_delta.go b/plumbing/format/packfile/diff_delta.go index 7a32d5d..fb05a79 100644 --- a/plumbing/format/packfile/diff_delta.go +++ b/plumbing/format/packfile/diff_delta.go @@ -134,39 +134,21 @@ func encodeCopyOperation(offset, length int) []byte { code := 0x80 var opcodes []byte - if offset&0xff != 0 { - opcodes = append(opcodes, byte(offset&0xff)) - code |= 0x01 - } - - if offset&0xff00 != 0 { - opcodes = append(opcodes, byte((offset&0xff00)>>8)) - code |= 0x02 - } - - if offset&0xff0000 != 0 { - opcodes = append(opcodes, byte((offset&0xff0000)>>16)) - code |= 0x04 - } - - if offset&0xff000000 != 0 { - opcodes = append(opcodes, byte((offset&0xff000000)>>24)) - code |= 0x08 - } - - if length&0xff != 0 { - opcodes = append(opcodes, byte(length&0xff)) - code |= 0x10 - } - - if length&0xff00 != 0 { - opcodes = append(opcodes, byte((length&0xff00)>>8)) - code |= 0x20 + var i uint + for i = 0; i < 4; i++ { + f := 0xff << (i * 8) + if offset&f != 0 { + opcodes = append(opcodes, byte(offset&f>>(i*8))) + code |= 0x01 << i + } } - if length&0xff0000 != 0 { - opcodes = append(opcodes, byte((length&0xff0000)>>16)) - code |= 0x40 + for i = 0; i < 3; i++ { + f := 0xff << (i * 8) + if length&f != 0 { + opcodes = append(opcodes, byte(length&f>>(i*8))) + code |= 0x10 << i + } } return append([]byte{byte(code)}, opcodes...) -- cgit