Rev 3910: Change encode_copy_instruction to return just the bytestring. in http://bzr.arbash-meinel.com/branches/bzr/brisbane/vilajam

John Arbash Meinel john at arbash-meinel.com
Wed Mar 25 19:31:34 GMT 2009


At http://bzr.arbash-meinel.com/branches/bzr/brisbane/vilajam

------------------------------------------------------------
revno: 3910
revision-id: john at arbash-meinel.com-20090325193113-7crd62vmi7ryobh5
parent: john at arbash-meinel.com-20090325192307-yhus35dm17i9531s
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: vilajam
timestamp: Wed 2009-03-25 14:31:13 -0500
message:
  Change encode_copy_instruction to return just the bytestring.
-------------- next part --------------
=== modified file 'bzrlib/groupcompress.py'
--- a/bzrlib/groupcompress.py	2009-03-25 19:23:07 +0000
+++ b/bzrlib/groupcompress.py	2009-03-25 19:31:13 +0000
@@ -87,7 +87,7 @@
 def encode_copy_instruction(offset, length):
     """Convert this offset into a control code and bytes."""
     copy_command = 0x80
-    copy_bytes = []
+    copy_bytes = [None]
 
     for copy_bit in (0x01, 0x02, 0x04, 0x08):
         base_byte = offset & 0xff
@@ -97,7 +97,8 @@
         offset >>= 8
     if length is None:
         # None is used by the test suite
-        return copy_command, copy_bytes
+        copy_bytes[0] = chr(copy_command)
+        return ''.join(copy_bytes)
     if length > 0x10000:
         raise ValueError("we don't emit copy records for lengths > 64KiB")
     if length == 0:
@@ -111,7 +112,8 @@
                 copy_command |= copy_bit
                 copy_bytes.append(chr(base_byte))
             length >>= 8
-    return copy_command, copy_bytes
+    copy_bytes[0] = chr(copy_command)
+    return ''.join(copy_bytes)
 
 
 def sort_gc_optimal(parent_map):
@@ -921,25 +923,8 @@
         # code, we will also limit it to a 64kB copy
         for start_byte in xrange(first_byte, stop_byte, 64*1024):
             num_bytes = min(64*1024, stop_byte - first_byte)
-            copy_command = 0x80
-            copy_bytes = []
-            base_byte = start_byte & 0x000000ff
-            if base_byte:
-                copy_command |= 0x01
-                assert 0 <= base_byte <= 255
-                copy_bytes.append(chr(base_byte))
-            base_byte = (start_byte & 0x0000ff00) >> 8
-            if base_byte:
-                copy_bytes |= 0x02
-                copy_bytes.append(chr(base_byte))
-            base_byte = (start_byte & 0x00ff0000) >> 16
-            if base_byte:
-                copy_bytes |= 0x03
-                copy_bytes.append(chr(base_byte))
-            base_byte = (start_byte & 0xff000000) >> 24
-            if base_byte:
-                copy_bytes |= 0x04
-                copy_bytes.append(chr(base_byte))
+            copy_command, copy_bytes = encode_copy_instruction(start_byte,
+                                                               num_bytes)
 
     def flush_range(self, new_line_start, source_line_start, match_num_lines,
                     new_lines, out_lines, index_lines):

=== modified file 'bzrlib/tests/test_groupcompress.py'
--- a/bzrlib/tests/test_groupcompress.py	2009-03-25 19:23:07 +0000
+++ b/bzrlib/tests/test_groupcompress.py	2009-03-25 19:31:13 +0000
@@ -176,55 +176,50 @@
 
 class TestEncodeCopyInstruction(tests.TestCase):
 
-    def assertCopyInstruction(self, control, bytes, offset, length):
-        self.assertEqual((control, bytes),
-                         groupcompress.encode_copy_instruction(offset, length))
+    def assertCopyInstruction(self, expected, offset, length):
+        bytes = groupcompress.encode_copy_instruction(offset, length)
+        if expected != bytes:
+            self.assertEqual([hex(ord(e)) for e in expected],
+                             [hex(ord(b)) for b in bytes])
 
     def test_encode_no_length(self):
-        self.assertCopyInstruction(0x80, [], 0, None)
-        self.assertCopyInstruction(0x81, ['\x01'], 1, None)
-        self.assertCopyInstruction(0x81, ['\x0a'], 10, None)
-        self.assertCopyInstruction(0x81, ['\xff'], 255, None)
-        self.assertCopyInstruction(0x82, ['\x01'], 256, None)
-        self.assertCopyInstruction(0x83, ['\x01', '\x01'], 257, None)
-        self.assertCopyInstruction(0x8F, ['\xff', '\xff', '\xff', '\xff'],
-                                   0xFFFFFFFF, None)
-        self.assertCopyInstruction(0x8E, ['\xff', '\xff', '\xff'],
-                                   0xFFFFFF00, None)
-        self.assertCopyInstruction(0x8D, ['\xff', '\xff', '\xff'],
-                                   0xFFFF00FF, None)
-        self.assertCopyInstruction(0x8B, ['\xff', '\xff', '\xff'],
-                                   0xFF00FFFF, None)
-        self.assertCopyInstruction(0x87, ['\xff', '\xff', '\xff'],
-                                   0x00FFFFFF, None)
-        self.assertCopyInstruction(0x8F, ['\x04', '\x03', '\x02', '\x01'],
-                                   0x01020304, None)
+        self.assertCopyInstruction('\x80', 0, None)
+        self.assertCopyInstruction('\x81\x01', 1, None)
+        self.assertCopyInstruction('\x81\x0a', 10, None)
+        self.assertCopyInstruction('\x81\xff', 255, None)
+        self.assertCopyInstruction('\x82\x01', 256, None)
+        self.assertCopyInstruction('\x83\x01\x01', 257, None)
+        self.assertCopyInstruction('\x8F\xff\xff\xff\xff', 0xFFFFFFFF, None)
+        self.assertCopyInstruction('\x8E\xff\xff\xff', 0xFFFFFF00, None)
+        self.assertCopyInstruction('\x8D\xff\xff\xff', 0xFFFF00FF, None)
+        self.assertCopyInstruction('\x8B\xff\xff\xff', 0xFF00FFFF, None)
+        self.assertCopyInstruction('\x87\xff\xff\xff', 0x00FFFFFF, None)
+        self.assertCopyInstruction('\x8F\x04\x03\x02\x01', 0x01020304, None)
 
     def test_encode_no_offset(self):
-        self.assertCopyInstruction(0x90, ['\x01'], 0, 1)
-        self.assertCopyInstruction(0x90, ['\x0a'], 0, 10)
-        self.assertCopyInstruction(0x90, ['\xff'], 0, 255)
-        self.assertCopyInstruction(0xA0, ['\x01'], 0, 256)
-        self.assertCopyInstruction(0xB0, ['\x01', '\x01'], 0, 257)
-        self.assertCopyInstruction(0xB0, ['\x01', '\x01'], 0, 257)
-        self.assertCopyInstruction(0xB0, ['\xff', '\xff'], 0, 0xFFFF)
+        self.assertCopyInstruction('\x90\x01', 0, 1)
+        self.assertCopyInstruction('\x90\x0a', 0, 10)
+        self.assertCopyInstruction('\x90\xff', 0, 255)
+        self.assertCopyInstruction('\xA0\x01', 0, 256)
+        self.assertCopyInstruction('\xB0\x01\x01', 0, 257)
+        self.assertCopyInstruction('\xB0\x01\x01', 0, 257)
+        self.assertCopyInstruction('\xB0\xff\xff', 0, 0xFFFF)
         # Special case, if copy == 64KiB, then we store exactly 0
         # Note that this puns with a copy of exactly 0 bytes, but we don't care
         # about that, as we would never actually copy 0 bytes
-        self.assertCopyInstruction(0x80, [], 0, 64*1024)
+        self.assertCopyInstruction('\x80', 0, 64*1024)
 
     def test_encode(self):
-        self.assertCopyInstruction(0x91, ['\x01', '\x01'], 1, 1)
-        self.assertCopyInstruction(0x91, ['\x09', '\x0a'], 9, 10)
-        self.assertCopyInstruction(0x91, ['\xfe', '\xff'], 254, 255)
-        self.assertCopyInstruction(0xA2, ['\x02', '\x01'], 512, 256)
-        self.assertCopyInstruction(0xB3, ['\x02', '\x01', '\x01', '\x01'],
-                                   258, 257)
-        self.assertCopyInstruction(0xB0, ['\x01', '\x01'], 0, 257)
+        self.assertCopyInstruction('\x91\x01\x01', 1, 1)
+        self.assertCopyInstruction('\x91\x09\x0a', 9, 10)
+        self.assertCopyInstruction('\x91\xfe\xff', 254, 255)
+        self.assertCopyInstruction('\xA2\x02\x01', 512, 256)
+        self.assertCopyInstruction('\xB3\x02\x01\x01\x01', 258, 257)
+        self.assertCopyInstruction('\xB0\x01\x01', 0, 257)
         # Special case, if copy == 64KiB, then we store exactly 0
         # Note that this puns with a copy of exactly 0 bytes, but we don't care
         # about that, as we would never actually copy 0 bytes
-        self.assertCopyInstruction(0x81, ['\x0a'], 10, 64*1024)
+        self.assertCopyInstruction('\x81\x0a', 10, 64*1024)
 
 
 class TestBase128Int(tests.TestCase):



More information about the bazaar-commits mailing list