inbus

changeset 4:bc0130c17f61

Add array unpacker.
author Eric Hopper <hopper@omnifarious.org>
date Mon, 25 Feb 2008 19:08:01 -0800
parents 6953723da57c
children 8ecb08252c8a
files inbus.py
diffstat 1 files changed, 22 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/inbus.py	Mon Feb 25 08:41:19 2008 -0800
     1.2 +++ b/inbus.py	Mon Feb 25 19:08:01 2008 -0800
     1.3 @@ -190,6 +190,28 @@
     1.4          raise ValueError("Ran off the end of the string to unpack while parsing tuple")
     1.5      return composeunpacker(tuplefinishunpacker, unpacker), offset + 1
     1.6  
     1.7 +def make_array_unpacker(s):
     1.8 +    if s[0] != '[':
     1.9 +        raise ValueError("Array type tag ('a') must be followed by '['")
    1.10 +    offset = 1
    1.11 +    elunpacker, consumed = create_unpacker(buffer(s, offset))
    1.12 +    offset += consumed
    1.13 +    if (offset >= len(s)) or (s[offset] != ']'):
    1.14 +        raise ValueError("Array type tag must end with ']' after one type tag.")
    1.15 +    def unpack_array(dectup):
    1.16 +        result, s, offset = dectup
    1.17 +        array = []
    1.18 +        if len(s) <= offset:
    1.19 +            raise ValueError("End of string while parsing array.")
    1.20 +        while s[offset] != '\0':
    1.21 +            out, s, offset = elunpacker(([], s, offset + 1))
    1.22 +            if len(s) <= offset:
    1.23 +                raise ValueError("End of string while parsing array.")
    1.24 +            array.extend(out)
    1.25 +        result.append(array)
    1.26 +        return result, s, offset + 1
    1.27 +    return unpack_array, offset + 1
    1.28 +
    1.29  def packl(lnum, pad = 1):
    1.30      if lnum < 0:
    1.31          raise RangeError("Cannot use packl to convert a negative integer "