From 01327ef4b2341f2313460c11d32dacda59650fd4 Mon Sep 17 00:00:00 2001 From: Infendro Date: Fri, 12 Dec 2025 21:20:44 +0100 Subject: [PATCH] refactor --- .../com/infendro/bytearray/ByteArray.kt | 65 ++++++++++-------- .../com/infendro/bytearray/ByteOrder.kt | 52 +++++--------- .../kotlin/com/infendro/bytearray/Int.kt | 24 +------ .../kotlin/com/infendro/bytearray/IntArray.kt | 23 +++++++ .../kotlin/com/infendro/bytearray/Long.kt | 24 +------ .../com/infendro/bytearray/LongArray.kt | 23 +++++++ .../kotlin/com/infendro/bytearray/String.kt | 3 + .../com/infendro/bytearray/UByteArray.kt | 68 +++++++++++-------- .../kotlin/com/infendro/bytearray/UInt.kt | 24 +------ .../com/infendro/bytearray/UIntArray.kt | 23 +++++++ .../kotlin/com/infendro/bytearray/ULong.kt | 24 +------ .../com/infendro/bytearray/ULongArray.kt | 23 +++++++ 12 files changed, 197 insertions(+), 179 deletions(-) create mode 100644 src/commonMain/kotlin/com/infendro/bytearray/IntArray.kt create mode 100644 src/commonMain/kotlin/com/infendro/bytearray/LongArray.kt create mode 100644 src/commonMain/kotlin/com/infendro/bytearray/UIntArray.kt create mode 100644 src/commonMain/kotlin/com/infendro/bytearray/ULongArray.kt diff --git a/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt index c6743ec..b62c1e9 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt @@ -8,7 +8,8 @@ import kotlin.experimental.xor infix fun ByteArray.and( that: ByteArray, ): ByteArray { - if (this.size != that.size) throw Exception() + if (this.size != that.size) + throw IllegalArgumentException() return ByteArray(size) { i -> this[i] and that[i] @@ -18,7 +19,8 @@ infix fun ByteArray.and( infix fun ByteArray.or( that: ByteArray, ): ByteArray { - if (this.size != that.size) throw Exception() + if (this.size != that.size) + throw IllegalArgumentException() return ByteArray(size) { i -> this[i] or that[i] @@ -28,7 +30,8 @@ infix fun ByteArray.or( infix fun ByteArray.xor( that: ByteArray, ): ByteArray { - if (this.size != that.size) throw Exception() + if (this.size != that.size) + throw IllegalArgumentException() return ByteArray(size) { i -> this[i] xor that[i] @@ -62,18 +65,20 @@ fun ByteArray.padEnd( fun ByteArray.toInt( order: ByteOrder = BIG_ENDIAN, ): Int { - if (size != 4) throw Exception() + if (size != 4) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0) { acc, byte -> - (acc shl 8) or (byte.toInt() and 0xFF) - } + return normalize(order) + .fold(0) { acc, byte -> + (acc shl 8) or (byte.toInt() and 0xFF) + } } fun ByteArray.toIntArray( order: ByteOrder = BIG_ENDIAN, ): IntArray { - if (size % 4 != 0) throw Exception() + if (size % 4 != 0) + throw IllegalArgumentException() return toList() .chunked(4) { it.toByteArray() } @@ -84,18 +89,20 @@ fun ByteArray.toIntArray( fun ByteArray.toLong( order: ByteOrder = BIG_ENDIAN, ): Long { - if (size != 8) throw Exception() + if (size != 8) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0L) { acc, byte -> - (acc shl 8) or (byte.toLong() and 0xFFL) - } + return normalize(order) + .fold(0L) { acc, byte -> + (acc shl 8) or (byte.toLong() and 0xFFL) + } } fun ByteArray.toLongArray( order: ByteOrder = BIG_ENDIAN, ): LongArray { - if (size % 8 != 0) throw Exception() + if (size % 8 != 0) + throw IllegalArgumentException() return toList() .chunked(8) { it.toByteArray() } @@ -106,18 +113,20 @@ fun ByteArray.toLongArray( fun ByteArray.toUInt( order: ByteOrder = BIG_ENDIAN, ): UInt { - if (size != 4) throw Exception() + if (size != 4) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0U) { acc, byte -> - (acc shl 8) or (byte.toUInt() and 0xFFU) - } + return normalize(order) + .fold(0U) { acc, byte -> + (acc shl 8) or (byte.toUInt() and 0xFFU) + } } fun ByteArray.toUIntArray( order: ByteOrder = BIG_ENDIAN, ): UIntArray { - if (size % 4 != 0) throw Exception() + if (size % 4 != 0) + throw IllegalArgumentException() return toList() .chunked(4) { it.toByteArray() } @@ -128,18 +137,20 @@ fun ByteArray.toUIntArray( fun ByteArray.toULong( order: ByteOrder = BIG_ENDIAN, ): ULong { - if (size != 8) throw Exception() + if (size != 8) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0UL) { acc, byte -> - (acc shl 8) or (byte.toULong() and 0xFFUL) - } + return normalize(order) + .fold(0UL) { acc, byte -> + (acc shl 8) or (byte.toULong() and 0xFFUL) + } } fun ByteArray.toULongArray( order: ByteOrder = BIG_ENDIAN, ): ULongArray { - if (size % 8 != 0) throw Exception() + if (size % 8 != 0) + throw IllegalArgumentException() return toList() .chunked(8) { it.toByteArray() } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/ByteOrder.kt b/src/commonMain/kotlin/com/infendro/bytearray/ByteOrder.kt index f13d085..6c48639 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/ByteOrder.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/ByteOrder.kt @@ -1,38 +1,22 @@ package com.infendro.bytearray +import com.infendro.bytearray.ByteOrder.BIG_ENDIAN +import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN + enum class ByteOrder { - BIG_ENDIAN { - override fun normalize( - bytes: ByteArray, - ): ByteArray { - return bytes - } - - override fun normalize( - bytes: UByteArray, - ): UByteArray { - return bytes - } - }, - LITTLE_ENDIAN { - override fun normalize( - bytes: ByteArray, - ): ByteArray { - return bytes.reversedArray() - } - - override fun normalize( - bytes: UByteArray, - ): UByteArray { - return bytes.reversedArray() - } - }; - - internal abstract fun normalize( - bytes: ByteArray, - ): ByteArray - - internal abstract fun normalize( - bytes: UByteArray, - ): UByteArray + BIG_ENDIAN, LITTLE_ENDIAN; +} + +fun ByteArray.normalize( + order: ByteOrder, +): ByteArray = when (order) { + BIG_ENDIAN -> this + LITTLE_ENDIAN -> reversedArray() +} + +fun UByteArray.normalize( + order: ByteOrder, +): UByteArray = when (order) { + BIG_ENDIAN -> this + LITTLE_ENDIAN -> reversedArray() } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/Int.kt b/src/commonMain/kotlin/com/infendro/bytearray/Int.kt index 46a3484..ea65324 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/Int.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/Int.kt @@ -9,17 +9,7 @@ fun Int.toByteArray( val offset = (3 - i) * 8 (this ushr offset).toByte() } - return order.normalize(bytes) -} - -fun IntArray.toByteArray( - order: ByteOrder = BIG_ENDIAN, -): ByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toByteArray(order).toList() - } - return bytes.toByteArray() + return bytes.normalize(order) } fun Int.toUByteArray( @@ -29,15 +19,5 @@ fun Int.toUByteArray( val offset = (3 - i) * 8 (this ushr offset).toUByte() } - return order.normalize(bytes) -} - -fun IntArray.toUByteArray( - order: ByteOrder = BIG_ENDIAN, -): UByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toUByteArray(order) - } - return bytes.toUByteArray() + return bytes.normalize(order) } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/IntArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/IntArray.kt new file mode 100644 index 0000000..e68c58d --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/bytearray/IntArray.kt @@ -0,0 +1,23 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.BIG_ENDIAN + +fun IntArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toByteArray(order).toList() + } + return bytes.toByteArray() +} + +fun IntArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toUByteArray(order) + } + return bytes.toUByteArray() +} diff --git a/src/commonMain/kotlin/com/infendro/bytearray/Long.kt b/src/commonMain/kotlin/com/infendro/bytearray/Long.kt index 07ce1a9..08657ea 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/Long.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/Long.kt @@ -9,17 +9,7 @@ fun Long.toByteArray( val offset = (7 - i) * 8 (this ushr offset).toByte() } - return order.normalize(bytes) -} - -fun LongArray.toByteArray( - order: ByteOrder = BIG_ENDIAN, -): ByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toByteArray(order).toList() - } - return bytes.toByteArray() + return bytes.normalize(order) } fun Long.toUByteArray( @@ -29,15 +19,5 @@ fun Long.toUByteArray( val offset = (7 - i) * 8 (this ushr offset).toUByte() } - return order.normalize(bytes) -} - -fun LongArray.toUByteArray( - order: ByteOrder = BIG_ENDIAN, -): UByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toUByteArray(order) - } - return bytes.toUByteArray() + return bytes.normalize(order) } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/LongArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/LongArray.kt new file mode 100644 index 0000000..32decbd --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/bytearray/LongArray.kt @@ -0,0 +1,23 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.BIG_ENDIAN + +fun LongArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toByteArray(order).toList() + } + return bytes.toByteArray() +} + +fun LongArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toUByteArray(order) + } + return bytes.toUByteArray() +} diff --git a/src/commonMain/kotlin/com/infendro/bytearray/String.kt b/src/commonMain/kotlin/com/infendro/bytearray/String.kt index 7adf8b9..f2c922c 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/String.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/String.kt @@ -2,3 +2,6 @@ package com.infendro.bytearray fun String.encodeToUByteArray(): UByteArray = encodeToByteArray().toUByteArray() + +fun UByteArray.decodeToString(): String = + toByteArray().decodeToString() diff --git a/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt index 584c1b8..b9e70b9 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt @@ -2,13 +2,11 @@ package com.infendro.bytearray import com.infendro.bytearray.ByteOrder.BIG_ENDIAN -fun UByteArray.decodeToString(): String = - toByteArray().decodeToString() - infix fun UByteArray.and( that: UByteArray, ): UByteArray { - if (this.size != that.size) throw Exception() + if (this.size != that.size) + throw IllegalArgumentException() return UByteArray(size) { i -> this[i] and that[i] @@ -18,7 +16,8 @@ infix fun UByteArray.and( infix fun UByteArray.or( that: UByteArray, ): UByteArray { - if (this.size != that.size) throw Exception() + if (this.size != that.size) + throw IllegalArgumentException() return UByteArray(size) { i -> this[i] or that[i] @@ -28,7 +27,8 @@ infix fun UByteArray.or( infix fun UByteArray.xor( that: UByteArray, ): UByteArray { - if (this.size != that.size) throw Exception() + if (this.size != that.size) + throw IllegalArgumentException() return UByteArray(size) { i -> this[i] xor that[i] @@ -62,18 +62,20 @@ fun UByteArray.padEnd( fun UByteArray.toInt( order: ByteOrder = BIG_ENDIAN, ): Int { - if (size != 4) throw Exception() + if (size != 4) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0) { acc, byte -> - (acc shl 8) or (byte.toInt() and 0xFF) - } + return normalize(order) + .fold(0) { acc, byte -> + (acc shl 8) or (byte.toInt() and 0xFF) + } } fun UByteArray.toIntArray( order: ByteOrder = BIG_ENDIAN, ): IntArray { - if (size % 4 != 0) throw Exception() + if (size % 4 != 0) + throw IllegalArgumentException() return chunked(4) { it.toUByteArray() } .map { it.toInt(order) } @@ -83,18 +85,20 @@ fun UByteArray.toIntArray( fun UByteArray.toLong( order: ByteOrder = BIG_ENDIAN, ): Long { - if (size != 8) throw Exception() + if (size != 8) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0L) { acc, byte -> - (acc shl 8) or (byte.toLong() and 0xFFL) - } + return normalize(order) + .fold(0L) { acc, byte -> + (acc shl 8) or (byte.toLong() and 0xFFL) + } } fun UByteArray.toLongArray( order: ByteOrder = BIG_ENDIAN, ): LongArray { - if (size % 8 != 0) throw Exception() + if (size % 8 != 0) + throw IllegalArgumentException() return chunked(8) { it.toUByteArray() } .map { it.toLong(order) } @@ -104,18 +108,20 @@ fun UByteArray.toLongArray( fun UByteArray.toUInt( order: ByteOrder = BIG_ENDIAN, ): UInt { - if (size != 4) throw Exception() + if (size != 4) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0U) { acc, byte -> - (acc shl 8) or (byte.toUInt() and 0xFFU) - } + return normalize(order) + .fold(0U) { acc, byte -> + (acc shl 8) or (byte.toUInt() and 0xFFU) + } } fun UByteArray.toUIntArray( order: ByteOrder = BIG_ENDIAN, ): UIntArray { - if (size % 4 != 0) throw Exception() + if (size % 4 != 0) + throw IllegalArgumentException() return chunked(4) { it.toUByteArray() } .map { it.toUInt(order) } @@ -125,18 +131,20 @@ fun UByteArray.toUIntArray( fun UByteArray.toULong( order: ByteOrder = BIG_ENDIAN, ): ULong { - if (size != 8) throw Exception() + if (size != 8) + throw IllegalArgumentException() - val bytes = order.normalize(this) - return bytes.fold(0UL) { acc, byte -> - (acc shl 8) or (byte.toULong() and 0xFFUL) - } + return normalize(order) + .fold(0UL) { acc, byte -> + (acc shl 8) or (byte.toULong() and 0xFFUL) + } } fun UByteArray.toULongArray( order: ByteOrder = BIG_ENDIAN, ): ULongArray { - if (size % 8 != 0) throw Exception() + if (size % 8 != 0) + throw IllegalArgumentException() return chunked(8) { it.toUByteArray() } .map { it.toULong(order) } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt b/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt index bdc8640..3d56c61 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt @@ -9,17 +9,7 @@ fun UInt.toByteArray( val offset = (3 - i) * 8 (this shr offset).toByte() } - return order.normalize(bytes) -} - -fun UIntArray.toByteArray( - order: ByteOrder = BIG_ENDIAN, -): ByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toByteArray(order).toList() - } - return bytes.toByteArray() + return bytes.normalize(order) } fun UInt.toUByteArray( @@ -29,15 +19,5 @@ fun UInt.toUByteArray( val offset = (3 - i) * 8 (this shr offset).toUByte() } - return order.normalize(bytes) -} - -fun UIntArray.toUByteArray( - order: ByteOrder = BIG_ENDIAN, -): UByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toUByteArray(order) - } - return bytes.toUByteArray() + return bytes.normalize(order) } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/UIntArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/UIntArray.kt new file mode 100644 index 0000000..681776a --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/bytearray/UIntArray.kt @@ -0,0 +1,23 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.BIG_ENDIAN + +fun UIntArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toByteArray(order).toList() + } + return bytes.toByteArray() +} + +fun UIntArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toUByteArray(order) + } + return bytes.toUByteArray() +} diff --git a/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt b/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt index 3d723bf..2d3ae19 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt @@ -9,17 +9,7 @@ fun ULong.toByteArray( val offset = (7 - i) * 8 (this shr offset).toByte() } - return order.normalize(bytes) -} - -fun ULongArray.toByteArray( - order: ByteOrder = BIG_ENDIAN, -): ByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toByteArray(order).toList() - } - return bytes.toByteArray() + return bytes.normalize(order) } fun ULong.toUByteArray( @@ -29,15 +19,5 @@ fun ULong.toUByteArray( val offset = (7 - i) * 8 (this shr offset).toUByte() } - return order.normalize(bytes) -} - -fun ULongArray.toUByteArray( - order: ByteOrder = BIG_ENDIAN, -): UByteArray { - val bytes = mutableListOf() - for (n in this) { - bytes += n.toUByteArray(order) - } - return bytes.toUByteArray() + return bytes.normalize(order) } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/ULongArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/ULongArray.kt new file mode 100644 index 0000000..3e8e1f3 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/bytearray/ULongArray.kt @@ -0,0 +1,23 @@ +package com.infendro.bytearray + +import com.infendro.bytearray.ByteOrder.BIG_ENDIAN + +fun ULongArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toByteArray(order).toList() + } + return bytes.toByteArray() +} + +fun ULongArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + val bytes = mutableListOf() + for (n in this) { + bytes += n.toUByteArray(order) + } + return bytes.toUByteArray() +}