From 32968f652f0f8266c0a35decfc8a72f86c9c713d Mon Sep 17 00:00:00 2001 From: Mingun Date: Fri, 27 Nov 2020 23:55:27 +0500 Subject: [PATCH] Remove redundant dataTypeByteSize and fix non-exhaustive warnings in CalculateSeqSize --- .../struct/precompile/CalculateSeqSizes.scala | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala b/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala index 61f21ae69..5db0311cb 100644 --- a/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala +++ b/shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala @@ -80,43 +80,46 @@ object CalculateSeqSizes { * Determines how many bits occupies given data type. * * @param dataType data type to analyze - * @return number of bits or None, if it's impossible to determine a priori + * @return number of bits or [[DynamicSized]], if it's impossible to determine a priori */ def dataTypeBitsSize(dataType: DataType): Sized = { dataType match { case BitsType1(_) => FixedSized(1) - case BitsType(width, _) => FixedSized(width) + case CalcBooleanType => DynamicSized + case EnumType(_, basedOn) => dataTypeBitsSize(basedOn) + case ut: UserTypeInstream => getSeqSize(ut.classSpec.get) - case _ => - dataTypeByteSize(dataType) match { - case FixedSized(x) => FixedSized(x * 8) - case otherSize => otherSize - } - } - } + case ut: UserTypeFromBytes => dataTypeBitsSize(ut.bytes) + case ut: CalcUserTypeFromBytes => dataTypeBitsSize(ut.bytes) + case _: StructType => DynamicSized + + case BitsType(width, _) => FixedSized(width) + case Int1Type(_) => FixedSized(8) + case IntMultiType(_, width, _) => FixedSized(width.width * 8) + case CalcIntType => DynamicSized + + case FloatMultiType(width, _) => FixedSized(width.width * 8) + case CalcFloatType => DynamicSized - /** - * Determines how many bytes occupies a given data type. - * - * @param dataType data type to analyze - * @return number of bytes or None, if it's impossible to determine a priori - */ - def dataTypeByteSize(dataType: DataType): Sized = { - dataType match { - case _: Int1Type => FixedSized(1) - case IntMultiType(_, width, _) => FixedSized(width.width) - case FloatMultiType(width, _) => FixedSized(width.width) case _: BytesEosType => DynamicSized case blt: BytesLimitType => blt.size.evaluateIntConst match { - case Some(x) => FixedSized(x.toInt) + case Some(x) => FixedSized(x.toInt * 8) case None => DynamicSized } case _: BytesTerminatedType => DynamicSized - case StrFromBytesType(basedOn, _) => dataTypeByteSize(basedOn) - case utb: UserTypeFromBytes => dataTypeByteSize(utb.bytes) - case cutb: CalcUserTypeFromBytes => dataTypeByteSize(cutb.bytes) + case CalcBytesType => DynamicSized + + case StrFromBytesType(basedOn, _) => dataTypeBitsSize(basedOn) + case CalcStrType => DynamicSized + case st: SwitchType => DynamicSized // FIXME: it's really possible get size if st.hasSize + + case OwnedKaitaiStreamType | KaitaiStreamType => DynamicSized + + // TODO: Add special type or attribute to ArrayType for arrays of known size + case _: ArrayType => DynamicSized + case AnyType => DynamicSized } } }