+class String(object):
+ def __init__(self, options):
+ self.name = 'string'
+ self.size = 1
+ self.length_field_packer = BaseTypes('u32')
+ self.limit = options['limit'] if 'limit' in options else None
+
+ def pack(self, list, kwargs=None):
+ if not list:
+ return self.length_field_packer.pack(0) + b""
+ if self.limit and len(list) > self.limit:
+ raise VPPSerializerValueError(
+ "Invalid argument length for: {}, {} maximum {}".
+ format(list, len(list), self.limit))
+
+ return self.length_field_packer.pack(len(list)) + list.encode('utf8')
+
+ def unpack(self, data, offset=0, result=None, ntc=False):
+ length, length_field_size = self.length_field_packer.unpack(data,
+ offset)
+ if length == 0:
+ return b'', 0
+ p = BaseTypes('u8', length)
+ x, size = p.unpack(data, offset + length_field_size)
+ x2 = x.split(b'\0', 1)[0]
+ return (x2.decode('utf8'), size + length_field_size)
+
+