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

@@ -39,24 +39,24 @@ fun ByteArray.padStart(
length: Int,
padByte: Byte,
): ByteArray {
return buildList {
repeat(length - this@padStart.size) {
add(padByte)
}
addAll(this@padStart)
}.toByteArray()
val bytes = mutableListOf<Byte>()
repeat(length - size) {
bytes += padByte
}
bytes += this.toList()
return bytes.toByteArray()
}
fun ByteArray.padEnd(
length: Int,
padByte: Byte,
): ByteArray {
return buildList {
addAll(this@padEnd)
repeat(length - this@padEnd.size) {
add(padByte)
}
}.toByteArray()
val bytes = mutableListOf<Byte>()
bytes += this.toList()
repeat(length - size) {
bytes += padByte
}
return bytes.toByteArray()
}
fun ByteArray.toInt(

View File

@@ -1,9 +0,0 @@
package com.infendro.bytearray
fun MutableCollection<Byte>.addAll(
bytes: ByteArray,
) = addAll(bytes.toList())
fun MutableCollection<UByte>.addAll(
bytes: UByteArray,
) = addAll(bytes.toList())

View File

@@ -15,11 +15,11 @@ fun Int.toByteArray(
fun IntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
return buildList {
for (n in this@toByteArray) {
addAll(n.toByteArray(order))
}
}.toByteArray()
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun Int.toUByteArray(
@@ -35,9 +35,9 @@ fun Int.toUByteArray(
fun IntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
return buildList {
for (n in this@toUByteArray) {
addAll(n.toUByteArray(order))
}
}.toUByteArray()
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -15,11 +15,11 @@ fun Long.toByteArray(
fun LongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
return buildList {
for (n in this@toByteArray) {
addAll(n.toByteArray(order))
}
}.toByteArray()
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun Long.toUByteArray(
@@ -35,9 +35,9 @@ fun Long.toUByteArray(
fun LongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
return buildList {
for (n in this@toUByteArray) {
addAll(n.toUByteArray(order))
}
}.toUByteArray()
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

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()
}

View File

@@ -15,11 +15,11 @@ fun UInt.toByteArray(
fun UIntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
return buildList {
for (n in this@toByteArray) {
addAll(n.toByteArray(order))
}
}.toByteArray()
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun UInt.toUByteArray(
@@ -35,9 +35,9 @@ fun UInt.toUByteArray(
fun UIntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
return buildList {
for (n in this@toUByteArray) {
addAll(n.toUByteArray(order))
}
}.toUByteArray()
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -15,11 +15,11 @@ fun ULong.toByteArray(
fun ULongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
return buildList {
for (n in this@toByteArray) {
addAll(n.toByteArray(order))
}
}.toByteArray()
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun ULong.toUByteArray(
@@ -35,9 +35,9 @@ fun ULong.toUByteArray(
fun ULongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
return buildList {
for (n in this@toUByteArray) {
addAll(n.toUByteArray(order))
}
}.toUByteArray()
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}