diff --git a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt index bf0e32c..b597223 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/bytearray/Operators.kt @@ -25,15 +25,33 @@ private inline fun ByteArray.combineInto( } } -private inline fun ByteArray.combine( - that: ByteArray, +private inline fun ByteArray.combineInto( + that: Byte, + destination: ByteArray, + destinationOffset: Int = 0, + startIndex: Int = 0, + endIndex: Int = size, combine: (Byte, Byte) -> Byte, -): ByteArray { +) { + val length = endIndex - startIndex + require(length >= 0) + require(startIndex in 0..(size - length)) + require(destinationOffset in 0..(destination.size - length)) + + repeat(length) { i -> + destination[destinationOffset + i] = combine(this[startIndex + i], that) + } +} + +private inline fun ByteArray.combine(that: ByteArray, combine: (Byte, Byte) -> Byte): ByteArray { require(this.size == that.size) 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) } + fun ByteArray.invInto( destination: ByteArray, destinationOffset: Int = 0, @@ -62,9 +80,20 @@ fun ByteArray.andInto( thatStartIndex: Int = 0, ) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> 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 } + 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, @@ -74,9 +103,20 @@ fun ByteArray.orInto( 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 } + 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, @@ -86,5 +126,16 @@ fun ByteArray.xorInto( 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 } + 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 } diff --git a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt index ad136d4..d64372c 100644 --- a/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt +++ b/src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt @@ -31,9 +31,26 @@ fun UByteArray.andInto( 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, +) + 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, @@ -50,9 +67,26 @@ fun UByteArray.orInto( 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, +) + 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, @@ -69,5 +103,22 @@ fun UByteArray.xorInto( 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, +) + 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) }