data: add Pad type

This commit is contained in:
Shiz 2022-05-10 13:24:58 +02:00
parent 1a544040b3
commit 493c4a03c5
2 changed files with 31 additions and 2 deletions

View File

@ -5,7 +5,7 @@ from .core.expr import BaseExpr, Expr, const, infer, bool_, not_, and_, or_, in_
from .core.meta import Wrapper, Generic
del core
from .types.data import Nothing, Static, Ignored, Data, data, Bits, bit, nibble
from .types.data import Nothing, Static, Ignored, Pad, Data, data, Bits, bit, nibble
from .types.num import (
Bool, Int, Float, bool, int8, uint8, byte,
int16, uint16, int16be, int16le, uint16be, uint16le, word,

View File

@ -27,7 +27,36 @@ class Nothing(Type[None]):
def __repr__(self) -> str:
return '{__name__}.Nothing()'
class Pad(Type[None]):
""" Seek something, yield nothing. """
__slots__ = ('amount', 'value')
def __init__(self, amount=0, value=b'\x00'):
self.amount = amount
self.value = value
def parse(self, context: Context, stream: Stream) -> None:
stream.seek(context.get(self.amount), os.SEEK_CUR)
def dump(self, context: Context, stream: Stream, value: None) -> None:
value = stretch(context.get(self.value), context.get(self.amount))
stream.write(value)
def sizeof(self, context: Context) -> O[Pos]:
return context.peek(self.amount)
def default(self, context: Context) -> None:
return None
def __str__(self) -> str:
return f'[padding: {self.amount}]'
def __repr__(self) -> str:
return f'{__name__}.Pad({self.amount!r}, value={self.value!r})'
class Data(Type[bytes]):
""" Parse/dump and yield bytes. """
__slots__ = ('size',)
def __init__(self, size: U[D, O[int]] = None) -> None:
@ -62,8 +91,8 @@ class Data(Type[bytes]):
data = Data()
class Bits(Type[int]):
""" Parse/dump and yield bits. """
__slots__ = ('amount',)
def __init__(self, amount: U[D, int] = 0) -> None: