str: fix up parse and add to API

This commit is contained in:
Shiz 2021-06-26 05:29:38 +02:00
parent 3741e353e3
commit fd7476a434
2 changed files with 10 additions and 5 deletions

View File

@ -6,6 +6,7 @@ from .core.expr import Expr
from .types.data import Nothing, Data, data
from .types.num import *
from .types.str import Str, StrType
from .types.struct import StructType, Struct
from .types.seq import Arr, Tuple
from .types.transforms import Default, Sized, Ref, Transform, Mapped, Enum, Switch
@ -17,7 +18,7 @@ __all__ = [x.__name__ for x in {
Wrapper, Default, Sized, Ref, Transform, Mapped, Enum, Switch,
AlignTo, AlignedTo,
Nothing, Data,
Int, Bool, Float,
Int, Bool, Float, Str, StrType,
Arr, Tuple,
StructType, Struct, Generic,
}] + [

View File

@ -2,6 +2,7 @@ import enum
from typing import Optional as O, Union as U
from ..core.base import PossibleDynamic as D, Type, Context
from ..core.io import Stream
from ..core.util import stretch
from .num import uint8
@ -27,22 +28,25 @@ class Str(Type[str]):
if type == StrType.Raw:
if length is None:
raise ValueError('tried to parse raw string with no specified length')
data = stream.read(length * char_size)
data = stream.read()
else:
data = stream.read(length * char_size)
elif type == StrType.C:
terminator = context.get(self.terminator)
if len(terminator) != char_size:
terminator = stretch(terminator, char_size)
data = b''
while True:
d = stream.read(char_size)
if d == terminator:
break
data += d
if length is not None and len(data) >= length:
if length is not None and len(data) >= length * char_size:
break
elif type == StrType.Pascal:
length_type = context.get(self.length_type)
plength = context.parse(length_type, stream)
return ''
return data.decode(encoding)
def dump(self, context: Context, stream: Stream, value: str) -> None:
pass