From aa18910e4dd78463c07f7f3ef84e1c5b137219cb Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 1 Nov 2015 17:23:11 +0200 Subject: Handle number of strings with struct.pack This avoids a bit of extra parsing. It limits us to lists of 2**64-1 items, but I think we can live with that. --- obnamlib/obj_serialiser.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/obnamlib/obj_serialiser.py b/obnamlib/obj_serialiser.py index ccc0a722..b39505d9 100644 --- a/obnamlib/obj_serialiser.py +++ b/obnamlib/obj_serialiser.py @@ -187,13 +187,16 @@ def _deserialise_dict(serialised): def _serialise_str_list(strings): n = len(strings) + encoded_n = struct.pack('!Q', n) encoded_lengths = struct.pack('!' + 'Q' * n, *[len(s) for s in strings]) - return _serialise_integer(n) + encoded_lengths + ''.join(strings) + return encoded_n + encoded_lengths + ''.join(strings) def _deserialise_str_list(serialised, pos): - n, pos = _deserialise_prefix(serialised, pos) int_size = struct.calcsize('!Q') + n = struct.unpack('!Q', serialised[pos:pos+int_size])[0] + pos += int_size + lengths = struct.unpack('!' + 'Q' * n, serialised[pos:pos + n*int_size]) pos += n * int_size strings = [] -- cgit v1.2.1