refactor and simplify functions

This commit is contained in:
2025-12-01 17:48:45 +01:00
parent 4fbadfba1e
commit fa2c6b6621
7 changed files with 68 additions and 81 deletions

View File

@@ -36,24 +36,24 @@ fun UByteArray.padStart(
length: Int,
padByte: UByte,
): UByteArray {
return buildList {
repeat(length - this@padStart.size) {
add(padByte)
}
addAll(this@padStart)
}.toUByteArray()
val bytes = mutableListOf<UByte>()
repeat(length - size) {
bytes += padByte
}
bytes += this
return bytes.toUByteArray()
}
fun UByteArray.padEnd(
length: Int,
padByte: UByte,
): UByteArray {
return buildList {
addAll(this@padEnd)
repeat(length - this@padEnd.size) {
add(padByte)
}
}.toUByteArray()
val bytes = mutableListOf<UByte>()
bytes += this
repeat(length - size) {
bytes += padByte
}
return bytes.toUByteArray()
}
fun UByteArray.toInt(
@@ -72,8 +72,7 @@ fun UByteArray.toIntArray(
): IntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4) { it.toUByteArray() }
return chunked(4) { it.toUByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
@@ -94,8 +93,7 @@ fun UByteArray.toLongArray(
): LongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8) { it.toUByteArray() }
return chunked(8) { it.toUByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
@@ -116,8 +114,7 @@ fun UByteArray.toUIntArray(
): UIntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4) { it.toUByteArray() }
return chunked(4) { it.toUByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
@@ -138,8 +135,7 @@ fun UByteArray.toULongArray(
): ULongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8) { it.toUByteArray() }
return chunked(8) { it.toUByteArray() }
.map { it.toULong(order) }
.toULongArray()
}