36
43
def add_copy(self, start_byte, end_byte):
37
44
# The data stream allows >64kB in a copy, but to match the compiled
38
45
# code, we will also limit it to a 64kB copy
39
for start_byte in xrange(start_byte, end_byte, 64*1024):
46
for start_byte in range(start_byte, end_byte, 64*1024):
40
47
num_bytes = min(64*1024, end_byte - start_byte)
41
48
copy_bytes = encode_copy_instruction(start_byte, num_bytes)
42
49
self.out_lines.append(copy_bytes)
48
55
if self.cur_insert_len > 127:
49
56
raise AssertionError('We cannot insert more than 127 bytes'
51
self.out_lines.append(chr(self.cur_insert_len))
58
self.out_lines.append(int2byte(self.cur_insert_len))
52
59
self.index_lines.append(False)
53
60
self.out_lines.extend(self.cur_insert_lines)
54
61
if self.cur_insert_len < self.min_len_to_index:
62
69
# Flush out anything pending
63
70
self._flush_insert()
64
71
line_len = len(line)
65
for start_index in xrange(0, line_len, 127):
72
for start_index in range(0, line_len, 127):
66
73
next_len = min(127, line_len - start_index)
67
self.out_lines.append(chr(next_len))
74
self.out_lines.append(int2byte(next_len))
68
75
self.index_lines.append(False)
69
76
self.out_lines.append(line[start_index:start_index+next_len])
70
77
# We don't index long lines, because we won't be able to match
126
133
matches[line].add(start_idx + idx)
128
matches[line] = set([start_idx + idx])
135
matches[line] = {start_idx + idx}
130
137
def get_matches(self, line):
131
138
"""Return the lines which match the line in right."""
251
258
def _flush_insert(self, start_linenum, end_linenum,
252
259
new_lines, out_lines, index_lines):
253
260
"""Add an 'insert' request to the data stream."""
254
bytes_to_insert = ''.join(new_lines[start_linenum:end_linenum])
261
bytes_to_insert = b''.join(new_lines[start_linenum:end_linenum])
255
262
insert_length = len(bytes_to_insert)
256
263
# Each insert instruction is at most 127 bytes long
257
for start_byte in xrange(0, insert_length, 127):
264
for start_byte in range(0, insert_length, 127):
258
265
insert_count = min(insert_length - start_byte, 127)
259
out_lines.append(chr(insert_count))
266
out_lines.append(int2byte(insert_count))
260
267
# Don't index the 'insert' instruction
261
268
index_lines.append(False)
262
269
insert = bytes_to_insert[start_byte:start_byte+insert_count]
274
281
num_bytes = stop_byte - first_byte
275
282
# The data stream allows >64kB in a copy, but to match the compiled
276
283
# code, we will also limit it to a 64kB copy
277
for start_byte in xrange(first_byte, stop_byte, 64*1024):
284
for start_byte in range(first_byte, stop_byte, 64*1024):
278
285
num_bytes = min(64*1024, stop_byte - start_byte)
279
286
copy_bytes = encode_copy_instruction(start_byte, num_bytes)
280
287
out_lines.append(copy_bytes)
285
292
if bytes_length is None:
286
293
bytes_length = sum(map(len, new_lines))
287
294
# reserved for content type, content length
288
out_lines = ['', '', encode_base128_int(bytes_length)]
295
out_lines = [b'', b'', encode_base128_int(bytes_length)]
289
296
index_lines = [False, False, False]
290
297
output_handler = _OutputHandler(out_lines, index_lines,
291
298
self._MIN_MATCH_BYTES)
312
319
def encode_base128_int(val):
313
320
"""Convert an integer into a 7-bit lsb encoding."""
316
323
while val >= 0x80:
317
bytes.append(chr((val | 0x80) & 0xFF))
324
data.append((val | 0x80) & 0xFF)
319
bytes.append(chr(val))
320
return ''.join(bytes)
323
def decode_base128_int(bytes):
330
def decode_base128_int(data):
324
331
"""Decode an integer from a 7-bit lsb encoding."""
328
bval = ord(bytes[offset])
335
bval = indexbytes(data, offset)
329
336
while bval >= 0x80:
330
337
val |= (bval & 0x7F) << shift
333
bval = ord(bytes[offset])
340
bval = indexbytes(data, offset)
334
341
val |= bval << shift
336
343
return val, offset
360
367
base_byte = length & 0xff
362
369
copy_command |= copy_bit
363
copy_bytes.append(chr(base_byte))
370
copy_bytes.append(int2byte(base_byte))
365
copy_bytes[0] = chr(copy_command)
366
return ''.join(copy_bytes)
372
copy_bytes[0] = int2byte(copy_command)
373
return b''.join(copy_bytes)
369
376
def decode_copy_instruction(bytes, cmd, pos):
388
offset = ord(bytes[pos])
395
offset = indexbytes(bytes, pos)
391
offset = offset | (ord(bytes[pos]) << 8)
398
offset = offset | (indexbytes(bytes, pos) << 8)
394
offset = offset | (ord(bytes[pos]) << 16)
401
offset = offset | (indexbytes(bytes, pos) << 16)
397
offset = offset | (ord(bytes[pos]) << 24)
404
offset = offset | (indexbytes(bytes, pos) << 24)
400
length = ord(bytes[pos])
407
length = indexbytes(bytes, pos)
403
length = length | (ord(bytes[pos]) << 8)
410
length = length | (indexbytes(bytes, pos) << 8)
406
length = length | (ord(bytes[pos]) << 16)
413
length = length | (indexbytes(bytes, pos) << 16)
413
420
def make_delta(source_bytes, target_bytes):
414
421
"""Create a delta from source to target."""
415
if type(source_bytes) is not str:
416
raise TypeError('source is not a str')
417
if type(target_bytes) is not str:
418
raise TypeError('target is not a str')
422
if not isinstance(source_bytes, bytes):
423
raise TypeError('source is not bytes')
424
if not isinstance(target_bytes, bytes):
425
raise TypeError('target is not bytes')
419
426
line_locations = LinesDeltaIndex(osutils.split_lines(source_bytes))
420
427
delta, _ = line_locations.make_delta(osutils.split_lines(target_bytes),
421
428
bytes_length=len(target_bytes))
422
return ''.join(delta)
429
return b''.join(delta)
425
432
def apply_delta(basis, delta):
426
433
"""Apply delta to this object to become new_version_id."""
427
if type(basis) is not str:
428
raise TypeError('basis is not a str')
429
if type(delta) is not str:
430
raise TypeError('delta is not a str')
434
if not isinstance(basis, bytes):
435
raise TypeError('basis is not bytes')
436
if not isinstance(delta, bytes):
437
raise TypeError('delta is not bytes')
431
438
target_length, pos = decode_base128_int(delta)
433
440
len_delta = len(delta)
434
441
while pos < len_delta:
435
cmd = ord(delta[pos])
442
cmd = indexbytes(delta, pos)
438
445
offset, length, pos = decode_copy_instruction(delta, cmd, pos)
446
453
raise ValueError('Command == 0 not supported yet')
447
454
lines.append(delta[pos:pos+cmd])
449
bytes = ''.join(lines)
450
if len(bytes) != target_length:
456
data = b''.join(lines)
457
if len(data) != target_length:
451
458
raise ValueError('Delta claimed to be %d long, but ended up'
452
459
' %d long' % (target_length, len(bytes)))
456
463
def apply_delta_to_source(source, delta_start, delta_end):