From 03ec68cc409a92c4f57b5d2ec246ffe8a1c87e71 Mon Sep 17 00:00:00 2001 From: Infendro Date: Wed, 4 Feb 2026 18:19:50 +0100 Subject: [PATCH] remove unsigned support --- README.md | 2 +- build.gradle.kts | 4 - .../com/infendro/bytes/byte/Operator.kt | 7 + .../infendro/bytes/bytearray/Conversion.kt | 28 +-- .../com/infendro/bytes/bytearray/Copy.kt | 16 -- .../bytearray/{Operators.kt => Operator.kt} | 226 +++++++++--------- .../com/infendro/bytes/int/Conversion.kt | 10 +- .../kotlin/com/infendro/bytes/int/Copy.kt | 12 +- .../kotlin/com/infendro/bytes/int/Operator.kt | 5 + .../com/infendro/bytes/intarray/Conversion.kt | 11 +- .../com/infendro/bytes/intarray/Copy.kt | 8 - .../com/infendro/bytes/long/Conversion.kt | 10 +- .../kotlin/com/infendro/bytes/long/Copy.kt | 12 +- .../com/infendro/bytes/long/Operator.kt | 5 + .../infendro/bytes/longarray/Conversion.kt | 11 +- .../com/infendro/bytes/longarray/Copy.kt | 8 - .../infendro/bytes/ubytearray/Conversion.kt | 32 --- .../com/infendro/bytes/ubytearray/Copy.kt | 37 --- .../infendro/bytes/ubytearray/Operators.kt | 153 ------------ .../com/infendro/bytes/ubytearray/String.kt | 7 - .../com/infendro/bytes/uint/Conversion.kt | 10 - .../kotlin/com/infendro/bytes/uint/Copy.kt | 17 -- .../infendro/bytes/uintarray/Conversion.kt | 10 - .../com/infendro/bytes/uintarray/Copy.kt | 21 -- .../com/infendro/bytes/ulong/Conversion.kt | 10 - .../kotlin/com/infendro/bytes/ulong/Copy.kt | 17 -- .../infendro/bytes/ulongarray/Conversion.kt | 10 - .../com/infendro/bytes/ulongarray/Copy.kt | 21 -- .../infendro/bytes/bytearray/Conversion.kt | 146 ++++------- .../com/infendro/bytes/bytearray/Operator.kt | 34 +++ .../com/infendro/bytes/bytearray/Operators.kt | 65 ----- .../com/infendro/bytes/int/Conversion.kt | 31 +-- .../com/infendro/bytes/intarray/Conversion.kt | 56 +---- .../com/infendro/bytes/long/Conversion.kt | 31 +-- .../infendro/bytes/longarray/Conversion.kt | 56 +---- .../com/infendro/bytes/ubytearray/String.kt | 21 -- 36 files changed, 286 insertions(+), 874 deletions(-) create mode 100644 src/commonMain/kotlin/com/infendro/bytes/byte/Operator.kt rename src/commonMain/kotlin/com/infendro/bytes/bytearray/{Operators.kt => Operator.kt} (60%) create mode 100644 src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt create mode 100644 src/commonMain/kotlin/com/infendro/bytes/long/Operator.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ubytearray/Conversion.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ubytearray/String.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/uintarray/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt delete mode 100644 src/commonMain/kotlin/com/infendro/bytes/ulongarray/Copy.kt create mode 100644 src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt delete mode 100644 src/commonTest/kotlin/com/infendro/bytes/bytearray/Operators.kt delete mode 100644 src/commonTest/kotlin/com/infendro/bytes/ubytearray/String.kt diff --git a/README.md b/README.md index b0bf649..cb10892 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # bytes-kt -`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `ByteArray` and `UByteArray`. +`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes. ## Installation diff --git a/build.gradle.kts b/build.gradle.kts index f0b339d..c761f4a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -33,10 +33,6 @@ kotlin { implementation(libs.test) } } - - compilerOptions { - freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes") - } } publishing.repositories { diff --git a/src/commonMain/kotlin/com/infendro/bytes/byte/Operator.kt b/src/commonMain/kotlin/com/infendro/bytes/byte/Operator.kt new file mode 100644 index 0000000..41eff31 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/bytes/byte/Operator.kt @@ -0,0 +1,7 @@ +package com.infendro.bytes.byte + +import kotlin.experimental.inv + +operator fun Byte.not(): Byte { + return inv() +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt index 377a32d..2dad050 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt @@ -23,15 +23,6 @@ fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int { } } -fun ByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt = - toInt(startIndex, order).toUInt() - -fun ByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN): IntArray = - IntArray(size / 4).also { copyInto(it, order = order) } - -fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray = - UIntArray(size / 4).also { copyInto(it, order = order) } - fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long { require(startIndex in 0..(size - 8)) @@ -59,11 +50,16 @@ fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long { } } -fun ByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong = - toLong(startIndex, order).toULong() +fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray { + val length = endIndex - startIndex + val result = IntArray(length / 4) + copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order) + return result +} -fun ByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN): LongArray = - LongArray(size / 8).also { copyInto(it, order = order) } - -fun ByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN): ULongArray = - ULongArray(size / 8).also { copyInto(it, order = order) } +fun ByteArray.toLongArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): LongArray { + val length = endIndex - startIndex + val result = LongArray(length / 8) + copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order) + return result +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt index b5ce9f9..9d44849 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt @@ -40,14 +40,6 @@ fun ByteArray.copyInto( } } -fun ByteArray.copyInto( - destination: UIntArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = copyInto(destination.asIntArray(), destinationOffset, startIndex, endIndex, order) - fun ByteArray.copyInto( destination: LongArray, destinationOffset: Int = 0, @@ -91,11 +83,3 @@ fun ByteArray.copyInto( } } } - -fun ByteArray.copyInto( - destination: ULongArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = copyInto(destination.asLongArray(), destinationOffset, startIndex, endIndex, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt similarity index 60% rename from src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt rename to src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt index 78c34c4..1923b37 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt @@ -1,7 +1,7 @@ package com.infendro.bytes.bytearray +import com.infendro.bytes.byte.not import kotlin.experimental.and -import kotlin.experimental.inv import kotlin.experimental.or import kotlin.experimental.xor @@ -20,7 +20,7 @@ private inline fun ByteArray.combineInto( require(thatStartIndex in 0..(that.size - length)) require(destinationOffset in 0..(destination.size - length)) - repeat(length) { i -> + for (i in 0.. + for (i in 0.. Byte): ByteArray { require(this.size == that.size) - return ByteArray(size).also { combineInto(that, it, combine = combine) } + val result = ByteArray(size) + combineInto(that, result, combine = combine) + return result } private inline fun ByteArray.combine(that: Byte, combine: (Byte, Byte) -> Byte): ByteArray { - return ByteArray(size).also { combineInto(that, it, combine = combine) } + val result = ByteArray(size) + combineInto(that, result, combine = combine) + return result +} + +fun ByteArray.andInto( + that: ByteArray, + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, + thatStartIndex: Int = 0, +) { + combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a and b } +} + +fun ByteArray.andInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { + andInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset) +} + +fun ByteArray.andInto( + that: Byte, + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, +) { + combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a and b } +} + +infix fun ByteArray.and(that: ByteArray): ByteArray { + return combine(that) { a, b -> a and b } +} + +infix fun ByteArray.and(that: Byte): ByteArray { + return combine(that) { a, b -> a and b } +} + +fun ByteArray.orInto( + that: ByteArray, + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, + thatStartIndex: Int = 0, +) { + combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b } +} + +fun ByteArray.orInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { + orInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset) +} + +fun ByteArray.orInto( + that: Byte, + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, +) { + combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a or b } +} + +infix fun ByteArray.or(that: ByteArray): ByteArray { + return combine(that) { a, b -> a or b } +} + +infix fun ByteArray.or(that: Byte): ByteArray { + return combine(that) { a, b -> a or b } +} + +fun ByteArray.xorInto( + that: ByteArray, + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, + thatStartIndex: Int = 0, +) { + combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b } +} + +fun ByteArray.xorInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { + xorInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset) +} + +fun ByteArray.xorInto( + that: Byte, + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, +) { + combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a xor b } +} + +infix fun ByteArray.xor(that: ByteArray): ByteArray { + return combine(that) { a, b -> a xor b } +} + +infix fun ByteArray.xor(that: Byte): ByteArray { + return combine(that) { a, b -> a xor b } } fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { @@ -59,112 +162,17 @@ fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startI require(startIndex in 0..(size - length)) require(destinationOffset in 0..(destination.size - length)) - repeat(length) { i -> - destination[destinationOffset + i] = this[startIndex + i].inv() + for (i in 0.. a and b } - -fun ByteArray.andInto( - that: Byte, - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) = combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a and b } - -fun ByteArray.andInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) = - combineInto( - destination, - destination, - destinationOffset, - startIndex, - endIndex, - destinationOffset, - ) { a, b -> a and b } - -infix fun ByteArray.and(that: ByteArray): ByteArray = - combine(that) { a, b -> a and b } - -infix fun ByteArray.and(that: Byte): ByteArray = - combine(that) { a, b -> a and b } - -fun ByteArray.orInto( - that: ByteArray, - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - thatStartIndex: Int = 0, -) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b } - -fun ByteArray.orInto( - that: Byte, - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) = combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a or b } - -fun ByteArray.orInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) = - combineInto( - destination, - destination, - destinationOffset, - startIndex, - endIndex, - destinationOffset, - ) { a, b -> a or b } - -infix fun ByteArray.or(that: ByteArray): ByteArray = - combine(that) { a, b -> a or b } - -infix fun ByteArray.or(that: Byte): ByteArray = - combine(that) { a, b -> a or b } - -fun ByteArray.xorInto( - that: ByteArray, - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - thatStartIndex: Int = 0, -) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b } - -fun ByteArray.xorInto( - that: Byte, - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) = combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a xor b } - -fun ByteArray.xorInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) = - combineInto( - destination, - destination, - destinationOffset, - startIndex, - endIndex, - destinationOffset, - ) { a, b -> a xor b } - -infix fun ByteArray.xor(that: ByteArray): ByteArray = - combine(that) { a, b -> a xor b } - -infix fun ByteArray.xor(that: Byte): ByteArray = - combine(that) { a, b -> a xor b } +operator fun ByteArray.not(): ByteArray { + val result = ByteArray(size) + invInto(result) + return result +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt index 4faff12..00da321 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.int import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(4).also { copyInto(it, order = order) } - -fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(4).also { copyInto(it, order = order) } +fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray { + val result = ByteArray(4) + copyInto(result, order = order) + return result +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt index f66260d..2b6bf0d 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt @@ -4,11 +4,7 @@ import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN -fun Int.copyInto( - destination: ByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) { +fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) { require(destinationOffset in 0..(destination.size - 4)) when (order) { @@ -26,9 +22,3 @@ fun Int.copyInto( } } } - -fun Int.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) = copyInto(destination.asByteArray(), destinationOffset, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt b/src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt new file mode 100644 index 0000000..4a0d9fc --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt @@ -0,0 +1,5 @@ +package com.infendro.bytes.int + +operator fun Int.not(): Int { + return inv() +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt index 2eccdb5..ea24bca 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/intarray/Conversion.kt @@ -3,8 +3,9 @@ package com.infendro.bytes.intarray import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(size * 4).also { copyInto(it, order = order) } - -fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(size * 4).also { copyInto(it, order = order) } +fun IntArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray { + val length = endIndex - startIndex + val result = ByteArray(length * 4) + copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order) + return result +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt index c1d8f1a..bec0b75 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt @@ -39,11 +39,3 @@ fun IntArray.copyInto( } } } - -fun IntArray.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = copyInto(destination.asByteArray(), destinationOffset, startIndex, endIndex, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt index 754b57a..2a6fb31 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt @@ -3,8 +3,8 @@ package com.infendro.bytes.long import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(8).also { copyInto(it, order = order) } - -fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(8).also { copyInto(it, order = order) } +fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray { + val result = ByteArray(8) + copyInto(result, order = order) + return result +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt index 61215db..0cbb9f4 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt @@ -4,11 +4,7 @@ import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN -fun Long.copyInto( - destination: ByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) { +fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) { require(destinationOffset in 0..(destination.size - 8)) when (order) { @@ -34,9 +30,3 @@ fun Long.copyInto( } } } - -fun Long.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) = copyInto(destination.asByteArray(), destinationOffset, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/long/Operator.kt b/src/commonMain/kotlin/com/infendro/bytes/long/Operator.kt new file mode 100644 index 0000000..de38818 --- /dev/null +++ b/src/commonMain/kotlin/com/infendro/bytes/long/Operator.kt @@ -0,0 +1,5 @@ +package com.infendro.bytes.long + +operator fun Long.not(): Long { + return inv() +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt index b98ae0e..9423273 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/longarray/Conversion.kt @@ -3,8 +3,9 @@ package com.infendro.bytes.longarray import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder.BIG_ENDIAN -fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(size * 8).also { copyInto(it, order = order) } - -fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(size * 8).also { copyInto(it, order = order) } +fun LongArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray { + val length = endIndex - startIndex + val result = ByteArray(length * 8) + copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order) + return result +} diff --git a/src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt index 811566b..0021fdb 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt @@ -47,11 +47,3 @@ fun LongArray.copyInto( } } } - -fun LongArray.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = copyInto(destination.asByteArray(), destinationOffset, startIndex, endIndex, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Conversion.kt deleted file mode 100644 index 7663a05..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Conversion.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.infendro.bytes.ubytearray - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN -import com.infendro.bytes.bytearray.toInt -import com.infendro.bytes.bytearray.toLong -import com.infendro.bytes.bytearray.toUInt -import com.infendro.bytes.bytearray.toULong - -fun UByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int = - asByteArray().toInt(startIndex, order) - -fun UByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt = - asByteArray().toUInt(startIndex, order) - -fun UByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN): IntArray = - IntArray(size / 4).also { copyInto(it, order = order) } - -fun UByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray = - UIntArray(size / 4).also { copyInto(it, order = order) } - -fun UByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long = - asByteArray().toLong(startIndex, order) - -fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong = - asByteArray().toULong(startIndex, order) - -fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN): LongArray = - LongArray(size / 8).also { copyInto(it, order = order) } - -fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN): ULongArray = - ULongArray(size / 8).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt deleted file mode 100644 index 5352010..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt +++ /dev/null @@ -1,37 +0,0 @@ -package com.infendro.bytes.ubytearray - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN -import com.infendro.bytes.bytearray.copyInto - -fun UByteArray.copyInto( - destination: IntArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) - -fun UByteArray.copyInto( - destination: UIntArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) - -fun UByteArray.copyInto( - destination: ULongArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) - -fun UByteArray.copyInto( - destination: LongArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt deleted file mode 100644 index 706976e..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt +++ /dev/null @@ -1,153 +0,0 @@ -package com.infendro.bytes.ubytearray - -import com.infendro.bytes.bytearray.andInto -import com.infendro.bytes.bytearray.invInto -import com.infendro.bytes.bytearray.orInto -import com.infendro.bytes.bytearray.xorInto - -fun UByteArray.invInto(destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) = - asByteArray().invInto(destination.asByteArray(), destinationOffset, startIndex, endIndex) - -fun UByteArray.invInto(startIndex: Int = 0, endIndex: Int = size) = - asByteArray().invInto(asByteArray(), startIndex, startIndex, endIndex) - -fun UByteArray.inv(): UByteArray = - UByteArray(size).also { invInto(it) } - -fun UByteArray.andInto( - that: UByteArray, - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - thatStartIndex: Int = 0, -) = asByteArray().andInto( - that.asByteArray(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, - thatStartIndex, -) - -fun UByteArray.andInto( - that: UByte, - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) = asByteArray().andInto( - that.toByte(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, -) - -fun UByteArray.andInto(destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) = - asByteArray().andInto( - destination.asByteArray(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, - destinationOffset, - ) - -infix fun UByteArray.and(that: UByteArray): UByteArray = - UByteArray(size).also { andInto(that, it) } - -infix fun UByteArray.and(that: UByte): UByteArray = - UByteArray(size).also { andInto(that, it) } - -fun UByteArray.orInto( - that: UByteArray, - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - thatStartIndex: Int = 0, -) = asByteArray().orInto( - that.asByteArray(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, - thatStartIndex, -) - -fun UByteArray.orInto( - that: UByte, - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) = asByteArray().orInto( - that.toByte(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, -) - -fun UByteArray.orInto(destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) = - asByteArray().orInto( - destination.asByteArray(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, - destinationOffset, - ) - -infix fun UByteArray.or(that: UByteArray): UByteArray = - UByteArray(size).also { orInto(that, it) } - -infix fun UByteArray.or(that: UByte): UByteArray = - UByteArray(size).also { orInto(that, it) } - -fun UByteArray.xorInto( - that: UByteArray, - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - thatStartIndex: Int = 0, -) = asByteArray().xorInto( - that.asByteArray(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, - thatStartIndex, -) - -fun UByteArray.xorInto( - that: UByte, - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) = asByteArray().xorInto( - that.toByte(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, -) - -fun UByteArray.xorInto(destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) = - asByteArray().xorInto( - destination.asByteArray(), - destination.asByteArray(), - destinationOffset, - startIndex, - endIndex, - destinationOffset, - ) - -infix fun UByteArray.xor(that: UByteArray): UByteArray = - UByteArray(size).also { xorInto(that, it) } - -infix fun UByteArray.xor(that: UByte): UByteArray = - UByteArray(size).also { xorInto(that, it) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/String.kt b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/String.kt deleted file mode 100644 index 9c45806..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/String.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.infendro.bytes.ubytearray - -fun String.encodeToUByteArray(): UByteArray = - encodeToByteArray().asUByteArray() - -fun UByteArray.decodeToString(): String = - asByteArray().decodeToString() diff --git a/src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt deleted file mode 100644 index 714781f..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.infendro.bytes.uint - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN - -fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(4).also { copyInto(it, order = order) } - -fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(4).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt deleted file mode 100644 index 78a7f88..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.infendro.bytes.uint - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN -import com.infendro.bytes.int.copyInto - -fun UInt.copyInto( - destination: ByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) = toInt().copyInto(destination, destinationOffset, order) - -fun UInt.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) = toInt().copyInto(destination, destinationOffset, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt deleted file mode 100644 index 7b88e50..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/uintarray/Conversion.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.infendro.bytes.uintarray - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN - -fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(size * 4).also { copyInto(it, order = order) } - -fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(size * 4).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/uintarray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/uintarray/Copy.kt deleted file mode 100644 index a66bf6b..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/uintarray/Copy.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.infendro.bytes.uintarray - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN -import com.infendro.bytes.intarray.copyInto - -fun UIntArray.copyInto( - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) - -fun UIntArray.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt deleted file mode 100644 index 4e26af5..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.infendro.bytes.ulong - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN - -fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(8).also { copyInto(it, order = order) } - -fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(8).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt deleted file mode 100644 index 1771a2c..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.infendro.bytes.ulong - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN -import com.infendro.bytes.long.copyInto - -fun ULong.copyInto( - destination: ByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) = toLong().copyInto(destination, destinationOffset, order) - -fun ULong.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - order: ByteOrder = BIG_ENDIAN, -) = toLong().copyInto(destination, destinationOffset, order) diff --git a/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt b/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt deleted file mode 100644 index b80cd38..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Conversion.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.infendro.bytes.ulongarray - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN - -fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray = - ByteArray(size * 8).also { copyInto(it, order = order) } - -fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN): UByteArray = - UByteArray(size * 8).also { copyInto(it, order = order) } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Copy.kt b/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Copy.kt deleted file mode 100644 index 78531cc..0000000 --- a/src/commonMain/kotlin/com/infendro/bytes/ulongarray/Copy.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.infendro.bytes.ulongarray - -import com.infendro.bytes.ByteOrder -import com.infendro.bytes.ByteOrder.BIG_ENDIAN -import com.infendro.bytes.longarray.copyInto - -fun ULongArray.copyInto( - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asLongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) - -fun ULongArray.copyInto( - destination: UByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, - order: ByteOrder = BIG_ENDIAN, -) = asLongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) diff --git a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt index f1c53fb..bc52e1b 100644 --- a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt +++ b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt @@ -8,113 +8,65 @@ import kotlin.test.assertEquals class ByteArrayConversionTest { @Test fun toInt() { - val expected = 0x12345678 - val actual = arrayOf( - byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt(), - byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(order = LITTLE_ENDIAN), - ) - assertEquals(expected, actual[0]) - assertEquals(expected, actual[1]) - } - - @Test - fun toUInt() { - val expected = 0x12345678U - val actual = arrayOf( - byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt(), - byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(order = LITTLE_ENDIAN), - ) - assertEquals(expected, actual[0]) - assertEquals(expected, actual[1]) - } - - @Test - fun toIntArray() { - val expected = intArrayOf(0x12345678, 0x78563412) - val actual = arrayOf( - byteArrayOf( - 0x12, 0x34, 0x56, 0x78, - 0x78, 0x56, 0x34, 0x12, - ).toIntArray(), - byteArrayOf( - 0x78, 0x56, 0x34, 0x12, - 0x12, 0x34, 0x56, 0x78, - ).toIntArray(LITTLE_ENDIAN), - ) - assertContentEquals(expected, actual[0]) - assertContentEquals(expected, actual[1]) - } - - @Test - fun toUIntArray() { - val expected = uintArrayOf(0x12345678U, 0x78563412U) - val actual = arrayOf( - byteArrayOf( - 0x12, 0x34, 0x56, 0x78, - 0x78, 0x56, 0x34, 0x12, - ).toUIntArray(), - byteArrayOf( - 0x78, 0x56, 0x34, 0x12, - 0x12, 0x34, 0x56, 0x78, - ).toUIntArray(LITTLE_ENDIAN), - ) - assertContentEquals(expected, actual[0]) - assertContentEquals(expected, actual[1]) + val expected = 0x01234567 + run { + val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67).toInt() + assertEquals(expected, actual) + } + run { + val actual = byteArrayOf(0x67, 0x45, 0x23, 0x01).toInt(order = LITTLE_ENDIAN) + assertEquals(expected, actual) + } } @Test fun toLong() { - val expected = 0x1234567878563412L - val actual = arrayOf( - byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toLong(), - byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toLong(order = LITTLE_ENDIAN), - ) - assertEquals(expected, actual[0]) - assertEquals(expected, actual[1]) + val expected = 0x0123456789abcdefL + run { + val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11).toLong() + assertEquals(expected, actual) + } + run { + val actual = byteArrayOf(-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01).toLong(order = LITTLE_ENDIAN) + assertEquals(expected, actual) + } } @Test - fun toULong() { - val expected = 0x1234567878563412UL - val actual = arrayOf( - byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toULong(), - byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toULong(order = LITTLE_ENDIAN), - ) - assertEquals(expected, actual[0]) - assertEquals(expected, actual[1]) + fun toIntArray() { + val expected = intArrayOf(0x01234567, 0x67452301) + run { + val actual = byteArrayOf( + 0x01, 0x23, 0x45, 0x67, + 0x67, 0x45, 0x23, 0x01, + ).toIntArray() + assertContentEquals(expected, actual) + } + run { + val actual = byteArrayOf( + 0x67, 0x45, 0x23, 0x01, + 0x01, 0x23, 0x45, 0x67, + ).toIntArray(order = LITTLE_ENDIAN) + assertContentEquals(expected, actual) + } } @Test fun toLongArray() { - val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L) - val actual = arrayOf( - byteArrayOf( - 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - ).toLongArray(), - byteArrayOf( - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, - ).toLongArray(LITTLE_ENDIAN), - ) - assertContentEquals(expected, actual[0]) - assertContentEquals(expected, actual[1]) - } - - @Test - fun toULongArray() { - val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL) - val actual = arrayOf( - byteArrayOf( - 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - ).toULongArray(), - byteArrayOf( - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, - ).toULongArray(LITTLE_ENDIAN), - ) - assertContentEquals(expected, actual[0]) - assertContentEquals(expected, actual[1]) + val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL) + run { + val actual = byteArrayOf( + 0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11, + -0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01, + ).toLongArray() + assertContentEquals(expected, actual) + } + run { + val actual = byteArrayOf( + -0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01, + 0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11, + ).toLongArray(order = LITTLE_ENDIAN) + assertContentEquals(expected, actual) + } } } diff --git a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt new file mode 100644 index 0000000..bee8422 --- /dev/null +++ b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operator.kt @@ -0,0 +1,34 @@ +package com.infendro.bytes.bytearray + +import kotlin.test.Test +import kotlin.test.assertContentEquals + +class ByteArrayOperatorTest { + @Test + fun and() { + val expected = byteArrayOf(0x18, 0x00) + val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F) + assertContentEquals(expected, actual) + } + + @Test + fun or() { + val expected = byteArrayOf(0x7E, 0x7F) + val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F) + assertContentEquals(expected, actual) + } + + @Test + fun xor() { + val expected = byteArrayOf(0x66, 0x7F) + val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F) + assertContentEquals(expected, actual) + } + + @Test + fun not() { + val expected = byteArrayOf(-0x01, -0x20) + val actual = !byteArrayOf(0x00, 0x1F) + assertContentEquals(expected, actual) + } +} diff --git a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operators.kt b/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operators.kt deleted file mode 100644 index 8f99cd9..0000000 --- a/src/commonTest/kotlin/com/infendro/bytes/bytearray/Operators.kt +++ /dev/null @@ -1,65 +0,0 @@ -package com.infendro.bytes.bytearray - -import kotlin.test.Test -import kotlin.test.assertContentEquals -import kotlin.test.assertFails - -class ByteArrayOperatorsTest { - @Test - fun inv() { - val expected = ubyteArrayOf(0xFFU, 0xE0U).asByteArray() - val actual = byteArrayOf(0x00, 0x1F).inv() - assertContentEquals(expected, actual) - } - - @Test - fun andSize() { - assertFails { - byteArrayOf() and byteArrayOf(0x00) - } - assertFails { - byteArrayOf(0x00) and byteArrayOf() - } - } - - @Test - fun and() { - val expected = byteArrayOf(0x18, 0x00) - val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F) - assertContentEquals(expected, actual) - } - - @Test - fun orSize() { - assertFails { - byteArrayOf() or byteArrayOf(0x00) - } - assertFails { - byteArrayOf(0x00) or byteArrayOf() - } - } - - @Test - fun or() { - val expected = byteArrayOf(0x7E, 0x7F) - val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F) - assertContentEquals(expected, actual) - } - - @Test - fun xorSize() { - assertFails { - byteArrayOf() xor byteArrayOf(0x00) - } - assertFails { - byteArrayOf(0x00) xor byteArrayOf() - } - } - - @Test - fun xor() { - val expected = byteArrayOf(0x66, 0x7F) - val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F) - assertContentEquals(expected, actual) - } -} diff --git a/src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt b/src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt index efd1e2f..7c9dd89 100644 --- a/src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt +++ b/src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt @@ -7,31 +7,14 @@ import kotlin.test.assertContentEquals class IntConversionTest { @Test fun toByteArray() { - val expected = arrayOf( - byteArrayOf(0x12, 0x34, 0x56, 0x78), - byteArrayOf(0x78, 0x56, 0x34, 0x12), - ) - val actual = arrayOf( - 0x12345678.toByteArray(), - 0x12345678.toByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67) + run { + val actual = 0x01234567.toByteArray() + assertContentEquals(expected, actual) } - } - - @Test - fun toUByteArray() { - val expected = arrayOf( - ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U), - ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U), - ) - val actual = arrayOf( - 0x12345678.toUByteArray(), - 0x12345678.toUByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + run { + val actual = 0x67452301.toByteArray(LITTLE_ENDIAN) + assertContentEquals(expected, actual) } } } diff --git a/src/commonTest/kotlin/com/infendro/bytes/intarray/Conversion.kt b/src/commonTest/kotlin/com/infendro/bytes/intarray/Conversion.kt index aef53dc..85b78a2 100644 --- a/src/commonTest/kotlin/com/infendro/bytes/intarray/Conversion.kt +++ b/src/commonTest/kotlin/com/infendro/bytes/intarray/Conversion.kt @@ -7,55 +7,17 @@ import kotlin.test.assertContentEquals class IntArrayConversionTest { @Test fun toByteArray() { - val expected = arrayOf( - byteArrayOf( - 0x12, 0x34, 0x56, 0x78, - 0x12, 0x34, 0x56, 0x78, - ), - byteArrayOf( - 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, - ), + val expected = byteArrayOf( + 0x01, 0x23, 0x45, 0x67, + 0x67, 0x45, 0x23, 0x01, ) - val actual = arrayOf( - intArrayOf( - 0x12345678, - 0x12345678, - ).toByteArray(), - intArrayOf( - 0x12345678, - 0x12345678, - ).toByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + run { + val actual = intArrayOf(0x01234567, 0x67452301).toByteArray() + assertContentEquals(expected, actual) } - } - - @Test - fun toUByteArray() { - val expected = arrayOf( - ubyteArrayOf( - 0x12U, 0x34U, 0x56U, 0x78U, - 0x12U, 0x34U, 0x56U, 0x78U, - ), - ubyteArrayOf( - 0x78U, 0x56U, 0x34U, 0x12U, - 0x78U, 0x56U, 0x34U, 0x12U, - ), - ) - val actual = arrayOf( - intArrayOf( - 0x12345678, - 0x12345678, - ).toUByteArray(), - intArrayOf( - 0x12345678, - 0x12345678, - ).toUByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + run { + val actual = intArrayOf(0x67452301, 0x01234567).toByteArray(order = LITTLE_ENDIAN) + assertContentEquals(expected, actual) } } } diff --git a/src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt b/src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt index ee3b792..5d368e9 100644 --- a/src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt +++ b/src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt @@ -7,31 +7,14 @@ import kotlin.test.assertContentEquals class LongConversionTest { @Test fun toByteArray() { - val expected = arrayOf( - byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78), - byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12), - ) - val actual = arrayOf( - 0x1234567812345678L.toByteArray(), - 0x1234567812345678L.toByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11) + run { + val actual = 0x0123456789abcdefL.toByteArray() + assertContentEquals(expected, actual) } - } - - @Test - fun toUByteArray() { - val expected = arrayOf( - ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U), - ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U), - ) - val actual = arrayOf( - 0x1234567812345678L.toUByteArray(), - 0x1234567812345678L.toUByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + run { + val actual = (-0x1032547698badcffL).toByteArray(LITTLE_ENDIAN) + assertContentEquals(expected, actual) } } } diff --git a/src/commonTest/kotlin/com/infendro/bytes/longarray/Conversion.kt b/src/commonTest/kotlin/com/infendro/bytes/longarray/Conversion.kt index 4364911..34de9aa 100644 --- a/src/commonTest/kotlin/com/infendro/bytes/longarray/Conversion.kt +++ b/src/commonTest/kotlin/com/infendro/bytes/longarray/Conversion.kt @@ -7,55 +7,17 @@ import kotlin.test.assertContentEquals class LongArrayConversionTest { @Test fun toByteArray() { - val expected = arrayOf( - byteArrayOf( - 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - ), - byteArrayOf( - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, - ), + val expected = byteArrayOf( + 0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11, + -0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01, ) - val actual = arrayOf( - longArrayOf( - 0x1234567812345678L, - 0x1234567812345678L, - ).toByteArray(), - longArrayOf( - 0x1234567812345678L, - 0x1234567812345678L, - ).toByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + run { + val actual = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL).toByteArray() + assertContentEquals(expected, actual) } - } - - @Test - fun toUByteArray() { - val expected = arrayOf( - ubyteArrayOf( - 0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U, - 0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U, - ), - ubyteArrayOf( - 0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U, - 0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U, - ), - ) - val actual = arrayOf( - longArrayOf( - 0x1234567812345678L, - 0x1234567812345678L, - ).toUByteArray(), - longArrayOf( - 0x1234567812345678L, - 0x1234567812345678L, - ).toUByteArray(LITTLE_ENDIAN), - ) - for (i in 0..1) { - assertContentEquals(expected[i], actual[i]) + run { + val actual = longArrayOf(-0x1032547698badcffL, 0x0123456789abcdefL).toByteArray(order = LITTLE_ENDIAN) + assertContentEquals(expected, actual) } } } diff --git a/src/commonTest/kotlin/com/infendro/bytes/ubytearray/String.kt b/src/commonTest/kotlin/com/infendro/bytes/ubytearray/String.kt deleted file mode 100644 index 9b89013..0000000 --- a/src/commonTest/kotlin/com/infendro/bytes/ubytearray/String.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.infendro.bytes.ubytearray - -import kotlin.test.Test -import kotlin.test.assertContentEquals -import kotlin.test.assertEquals - -class UByteArrayStringTest { - @Test - fun encodeToUByteArray() { - val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U) - val actual = "PASSWORD".encodeToUByteArray() - assertContentEquals(expected, actual) - } - - @Test - fun decodeToString() { - val expected = "PASSWORD" - val actual = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U).decodeToString() - assertEquals(expected, actual) - } -}