From 20c2ba4c240e87a66040bb94817ec514629a6e7a Mon Sep 17 00:00:00 2001 From: Infendro Date: Wed, 11 Mar 2026 22:15:57 +0100 Subject: [PATCH 1/2] Improve shorthand bytearray operator functions --- .../com/infendro/bytes/bytearray/Operator.kt | 132 ++++++++++-------- 1 file changed, 71 insertions(+), 61 deletions(-) diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt index 1923b37..3e198d7 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt @@ -5,14 +5,13 @@ import kotlin.experimental.and import kotlin.experimental.or import kotlin.experimental.xor -private inline fun ByteArray.combineInto( +fun ByteArray.andInto( that: ByteArray, destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0, - combine: (Byte, Byte) -> Byte, ) { val length = endIndex - startIndex require(length >= 0) @@ -21,17 +20,16 @@ private inline fun ByteArray.combineInto( require(destinationOffset in 0..(destination.size - length)) for (i in 0.. Byte, ) { val length = endIndex - startIndex require(length >= 0) @@ -39,55 +37,26 @@ private inline fun ByteArray.combineInto( require(destinationOffset in 0..(destination.size - length)) for (i in 0.. Byte): ByteArray { - require(this.size == that.size) +fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int) = + andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex) - val result = ByteArray(size) - combineInto(that, result, combine = combine) - return result -} - -private inline fun ByteArray.combine(that: Byte, combine: (Byte, Byte) -> Byte): ByteArray { - 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 } -} +fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) = + andInto(that, this, startIndex, startIndex, endIndex) infix fun ByteArray.and(that: ByteArray): ByteArray { - return combine(that) { a, b -> a and b } + val result = ByteArray(size) + andInto(that, result) + return result } infix fun ByteArray.and(that: Byte): ByteArray { - return combine(that) { a, b -> a and b } + val result = ByteArray(size) + andInto(that, result) + return result } fun ByteArray.orInto( @@ -98,11 +67,15 @@ fun ByteArray.orInto( endIndex: Int = size, thatStartIndex: Int = 0, ) { - combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b } -} + val length = endIndex - startIndex + require(length >= 0) + require(startIndex in 0..(size - length)) + require(thatStartIndex in 0..(that.size - length)) + require(destinationOffset in 0..(destination.size - length)) -fun ByteArray.orInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { - orInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset) + for (i in 0.. a or b } + val length = endIndex - startIndex + require(length >= 0) + require(startIndex in 0..(size - length)) + require(destinationOffset in 0..(destination.size - length)) + + for (i in 0.. a or b } + val result = ByteArray(size) + orInto(that, result) + return result } infix fun ByteArray.or(that: Byte): ByteArray { - return combine(that) { a, b -> a or b } + val result = ByteArray(size) + orInto(that, result) + return result } fun ByteArray.xorInto( @@ -131,11 +121,15 @@ fun ByteArray.xorInto( endIndex: Int = size, thatStartIndex: Int = 0, ) { - combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b } -} + val length = endIndex - startIndex + require(length >= 0) + require(startIndex in 0..(size - length)) + require(thatStartIndex in 0..(that.size - length)) + require(destinationOffset in 0..(destination.size - length)) -fun ByteArray.xorInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { - xorInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset) + for (i in 0.. a xor b } + val length = endIndex - startIndex + require(length >= 0) + require(startIndex in 0..(size - length)) + require(destinationOffset in 0..(destination.size - length)) + + for (i in 0.. a xor b } + val result = ByteArray(size) + xorInto(that, result) + return result } infix fun ByteArray.xor(that: Byte): ByteArray { - return combine(that) { a, b -> a xor b } + val result = ByteArray(size) + xorInto(that, result) + return result } fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) { @@ -167,9 +178,8 @@ fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startI } } -fun ByteArray.inv(startIndex: Int = 0, endIndex: Int = size) { +fun ByteArray.inv(startIndex: Int, endIndex: Int) = invInto(this, startIndex, startIndex, endIndex) -} operator fun ByteArray.not(): ByteArray { val result = ByteArray(size) -- 2.49.1 From c80040466bc0069786fbbb65b5ef06d1ae77059a Mon Sep 17 00:00:00 2001 From: Infendro Date: Wed, 11 Mar 2026 22:16:30 +0100 Subject: [PATCH 2/2] Bump version to 1.4.1 --- README.md | 2 +- build.gradle.kts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7390046..9b193a2 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,6 @@ repositories { } commonMain.dependencies { - implementation("com.infendro:bytes:1.4.0") + implementation("com.infendro:bytes:1.4.1") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 0ac7f11..d02c137 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ import com.infendro.plugin.token import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl group = "com.infendro" -version = "1.4.0" +version = "1.4.1" plugins { alias(libs.plugins.infendro) -- 2.49.1