Using BytesIO here makes it slower since it resizes the
memory quite bit, using a list to collect and then joining
that list makes it faster.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
return port.read(n)
+def encode_bvec(x):
+ return _helpers.vuint_encode(len(x)) + x
+
+
def skip_bvec(port):
port.read(read_vuint(port))
return result
def pack(types, *args):
- port = BytesIO()
- send(port, types, *args)
- return port.getvalue()
+ if len(types) != len(args):
+ raise Exception('number of arguments does not match format string')
+ ret = []
+ for (type, value) in zip(types, args):
+ if type == 'V':
+ ret.append(encode_vuint(value))
+ elif type == 'v':
+ ret.append(encode_vint(value))
+ elif type == 's':
+ ret.append(encode_bvec(value))
+ else:
+ raise Exception('unknown xpack format string item "' + type + '"')
+ return b''.join(ret)
def unpack(types, data):
port = BytesIO(data)