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)