From f18adc08b9c69a8228d742bce4d8d7f7f0e37af9 Mon Sep 17 00:00:00 2001 From: Infendro Date: Tue, 3 Feb 2026 23:01:19 +0100 Subject: [PATCH] implement shorthand bytearray operations --- .../com/infendro/bytes/bytearray/Operators.kt | 45 +++++++++++++++---- .../infendro/bytes/ubytearray/Operators.kt | 41 ++++++++++++++--- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt index b597223..78c34c4 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt @@ -49,15 +49,11 @@ private inline fun ByteArray.combine(that: ByteArray, combine: (Byte, Byte) -> B return ByteArray(size).also { combineInto(that, it, combine = combine) } } -private inline fun ByteArray.combine(that: Byte, combine: (Byte, Byte) -> Byte): ByteArray = - ByteArray(size).also { combineInto(that, it, combine = combine) } +private inline fun ByteArray.combine(that: Byte, combine: (Byte, Byte) -> Byte): ByteArray { + return ByteArray(size).also { combineInto(that, it, combine = combine) } +} -fun ByteArray.invInto( - destination: ByteArray, - destinationOffset: Int = 0, - startIndex: Int = 0, - endIndex: Int = size, -) { +fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { val length = endIndex - startIndex require(length >= 0) require(startIndex in 0..(size - length)) @@ -68,6 +64,9 @@ fun ByteArray.invInto( } } +fun ByteArray.invInto(startIndex: Int = 0, endIndex: Int = size) = + invInto(this, startIndex, startIndex, endIndex) + fun ByteArray.inv(): ByteArray = ByteArray(size).also { invInto(it) } @@ -88,6 +87,16 @@ fun ByteArray.andInto( 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 } @@ -111,6 +120,16 @@ fun ByteArray.orInto( 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 } @@ -134,6 +153,16 @@ fun ByteArray.xorInto( 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 } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt index d64372c..706976e 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt @@ -5,12 +5,11 @@ 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(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) } @@ -45,6 +44,16 @@ fun UByteArray.andInto( 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) } @@ -81,6 +90,16 @@ fun UByteArray.orInto( 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) } @@ -117,6 +136,16 @@ fun UByteArray.xorInto( 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) }