pyloxi: factor out loxi.generic_util.pad_to
diff --git a/py_gen/templates/_pack.py b/py_gen/templates/_pack.py
index b6e8ef7..534e930 100644
--- a/py_gen/templates/_pack.py
+++ b/py_gen/templates/_pack.py
@@ -57,12 +57,11 @@
:: if length_member_index != None:
length = sum([len(x) for x in packed])
:: if ofclass.name == 'of_action_set_field':
- pad_len = (length + 7)/8*8 - length
- length += pad_len
- packed.append('\x00' * pad_len)
+ packed.append(loxi.generic_util.pad_to(8, length))
+ length += len(packed[-1])
:: #endif
packed[${length_member_index}] = ${gen_pack_expr(length_member.oftype, 'length')}
:: #endif
:: if ofclass.name == 'of_match_v3':
- packed.append('\x00' * ((length + 7)/8*8 - length))
+ packed.append(loxi.generic_util.pad_to(8, length))
:: #endif
diff --git a/py_gen/templates/generic_util.py b/py_gen/templates/generic_util.py
index 53091ed..eca828c 100644
--- a/py_gen/templates/generic_util.py
+++ b/py_gen/templates/generic_util.py
@@ -63,6 +63,13 @@
return deserializer(reader.slice(length), typ)
return unpack_list(reader, wrapper)
+def pad_to(alignment, length):
+ """
+ Return a string of zero bytes that will pad a string of length 'length' to
+ a multiple of 'alignment'.
+ """
+ return "\x00" * ((length + alignment - 1)/alignment*alignment - length)
+
class OFReader(object):
"""
Cursor over a read-only buffer