fixed bugs with platforms and types. Tests pased

This commit is contained in:
sergeych committed 2023-03-15 08:26:13 +01:00
1 parent e4f2644169
commit e556d4bc3d
4 files changed
+102 -62

No files matched your search

@@ -1,7 +1,5 @@
package net.sergeych.bintools
import kotlin.reflect.typeOf
/**
* data input stream-like abstraction. We need it because
* kotlinx serialization is synchronous and there us nothing
@@ -21,11 +19,12 @@ interface DataSource {
fun readUByte() = readByte().toUByte()
@Suppress("unused")
fun readBytes(size: Int): ByteArray =
ByteArray(size).also { a ->
fun readBytes(size: Int): ByteArray {
return ByteArray(size).also { a ->
for (i in 0 until size)
a[i] = readByte()
}
}
fun readU32(): UInt = bytesToUInt(readBytes(4))
fun readI32(): Int = bytesToInt(readBytes(4))
@@ -33,7 +32,7 @@ interface DataSource {
fun readDouble() = Double.fromBits(readI64())
fun readFloat() = Float.fromBits(readI32())
fun readFloat() = Float.fromBits(readI32()).toFloat()
}
@@ -45,11 +44,11 @@ fun ByteArray.toDataSource(): DataSource =
override fun readByte(): Byte =
if (position < size) this@toDataSource[position++]
else throw DataSource.EndOfData()
override fun toString(): String {
return "ASrc[$position]: ${encodeToHex()}"
}
}
inline fun <reified T : Any> DataSource.readNumber(): T = when (typeOf<T>()) {
typeOf<Double>() -> readDouble() as T
typeOf<Float>() -> readFloat() as T
else -> Smartint.decode(this)
}
inline fun <reified T : Any> DataSource.readNumber(): T = Smartint.decode(this) as T