This commit is contained in:
2025-12-12 21:20:44 +01:00
parent be9a8a4369
commit 01327ef4b2
12 changed files with 197 additions and 179 deletions

View File

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