Skip to content

Commit 279ec24

Browse files
committed
Remove redundant dataTypeByteSize and fix non-exhaustive warnings in CalculateSeqSize
1 parent bd7e979 commit 279ec24

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

shared/src/main/scala/io/kaitai/struct/precompile/CalculateSeqSizes.scala

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,43 +80,46 @@ object CalculateSeqSizes {
8080
* Determines how many bits occupies given data type.
8181
*
8282
* @param dataType data type to analyze
83-
* @return number of bits or None, if it's impossible to determine a priori
83+
* @return number of bits or [[DynamicSized]], if it's impossible to determine a priori
8484
*/
8585
def dataTypeBitsSize(dataType: DataType): Sized = {
8686
dataType match {
8787
case BitsType1(_) => FixedSized(1)
88-
case BitsType(width, _) => FixedSized(width)
88+
case CalcBooleanType => DynamicSized
89+
8990
case EnumType(_, basedOn) => dataTypeBitsSize(basedOn)
91+
9092
case ut: UserTypeInstream => getSeqSize(ut.classSpec.get)
91-
case _ =>
92-
dataTypeByteSize(dataType) match {
93-
case FixedSized(x) => FixedSized(x * 8)
94-
case otherSize => otherSize
95-
}
96-
}
97-
}
93+
case ut: UserTypeFromBytes => dataTypeBitsSize(ut.bytes)
94+
case ut: CalcUserTypeFromBytes => dataTypeBitsSize(ut.bytes)
95+
case _: StructType => DynamicSized
96+
97+
case BitsType(width, _) => FixedSized(width)
98+
case Int1Type(_) => FixedSized(8)
99+
case IntMultiType(_, width, _) => FixedSized(width.width * 8)
100+
case CalcIntType => DynamicSized
101+
102+
case FloatMultiType(width, _) => FixedSized(width.width * 8)
103+
case CalcFloatType => DynamicSized
98104

99-
/**
100-
* Determines how many bytes occupies a given data type.
101-
*
102-
* @param dataType data type to analyze
103-
* @return number of bytes or None, if it's impossible to determine a priori
104-
*/
105-
def dataTypeByteSize(dataType: DataType): Sized = {
106-
dataType match {
107-
case _: Int1Type => FixedSized(1)
108-
case IntMultiType(_, width, _) => FixedSized(width.width)
109-
case FloatMultiType(width, _) => FixedSized(width.width)
110105
case _: BytesEosType => DynamicSized
111106
case blt: BytesLimitType => blt.size.evaluateIntConst match {
112-
case Some(x) => FixedSized(x.toInt)
107+
case Some(x) => FixedSized(x.toInt * 8)
113108
case None => DynamicSized
114109
}
115110
case _: BytesTerminatedType => DynamicSized
116-
case StrFromBytesType(basedOn, _, _) => dataTypeByteSize(basedOn)
117-
case utb: UserTypeFromBytes => dataTypeByteSize(utb.bytes)
118-
case cutb: CalcUserTypeFromBytes => dataTypeByteSize(cutb.bytes)
111+
case CalcBytesType => DynamicSized
112+
113+
case StrFromBytesType(basedOn, _, _) => dataTypeBitsSize(basedOn)
114+
case CalcStrType => DynamicSized
115+
119116
case st: SwitchType => DynamicSized // FIXME: it's really possible get size if st.hasSize
117+
118+
case OwnedKaitaiStreamType | KaitaiStreamType => DynamicSized
119+
120+
// TODO: Add special type or attribute to ArrayType for arrays of known size
121+
case _: ArrayType => DynamicSized
122+
case AnyType => DynamicSized
120123
}
121124
}
122125
}

0 commit comments

Comments
 (0)