From 1195ad5acc0e2b46261135272e899fc558391dba Mon Sep 17 00:00:00 2001 From: Infendro Date: Sun, 14 Dec 2025 00:12:03 +0100 Subject: [PATCH] replace IllegalArgumentException with require --- .../kotlin/com/infendro/bytearray/Conversion.kt | 12 ++++-------- .../kotlin/com/infendro/bytearray/Operators.kt | 9 +++------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/commonMain/kotlin/com/infendro/bytearray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytearray/Conversion.kt index eed9f46..1eface9 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/Conversion.kt @@ -5,8 +5,7 @@ import com.infendro.bytearray.ByteOrder.BIG_ENDIAN fun ByteArray.toInt( order: ByteOrder = BIG_ENDIAN, ): Int { - if (size != 4) - throw IllegalArgumentException() + require(size == 4) return normalize(order) .fold(0) { acc, byte -> (acc shl 8) or (byte.toInt() and 0xFF) } @@ -27,8 +26,7 @@ fun UByteArray.toUInt( fun ByteArray.toIntArray( order: ByteOrder = BIG_ENDIAN, ): IntArray { - if (size % 4 != 0) - throw IllegalArgumentException() + require(size % 4 == 0) return asList() .chunked(4) { it.toByteArray().toInt(order) } @@ -94,8 +92,7 @@ fun UIntArray.toUByteArray( fun ByteArray.toLong( order: ByteOrder = BIG_ENDIAN, ): Long { - if (size != 8) - throw IllegalArgumentException() + require(size == 8) return normalize(order) .fold(0L) { acc, byte -> (acc shl 8) or (byte.toLong() and 0xFFL) } @@ -116,8 +113,7 @@ fun UByteArray.toULong( fun ByteArray.toLongArray( order: ByteOrder = BIG_ENDIAN, ): LongArray { - if (size % 8 != 0) - throw IllegalArgumentException() + require(size % 8 == 0) return asList() .chunked(8) { it.toByteArray().toLong(order) } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytearray/Operators.kt index 2a7b462..285def8 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/Operators.kt @@ -7,8 +7,7 @@ import kotlin.experimental.xor infix fun ByteArray.and( that: ByteArray, ): ByteArray { - if (this.size != that.size) - throw IllegalArgumentException() + require(this.size == that.size) return ByteArray(size) { i -> this[i] and that[i] @@ -22,8 +21,7 @@ infix fun UByteArray.and( infix fun ByteArray.or( that: ByteArray, ): ByteArray { - if (this.size != that.size) - throw IllegalArgumentException() + require(this.size == that.size) return ByteArray(size) { i -> this[i] or that[i] @@ -37,8 +35,7 @@ infix fun UByteArray.or( infix fun ByteArray.xor( that: ByteArray, ): ByteArray { - if (this.size != that.size) - throw IllegalArgumentException() + require(this.size == that.size) return ByteArray(size) { i -> this[i] xor that[i]