implement bytearray operators with byte
This commit is contained in:
@@ -25,15 +25,33 @@ private inline fun ByteArray.combineInto(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun ByteArray.combine(
|
private inline fun ByteArray.combineInto(
|
||||||
that: ByteArray,
|
that: Byte,
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
combine: (Byte, Byte) -> Byte,
|
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)
|
require(this.size == that.size)
|
||||||
|
|
||||||
return ByteArray(size).also { combineInto(that, it, combine = combine) }
|
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(
|
fun ByteArray.invInto(
|
||||||
destination: ByteArray,
|
destination: ByteArray,
|
||||||
destinationOffset: Int = 0,
|
destinationOffset: Int = 0,
|
||||||
@@ -62,9 +80,20 @@ fun ByteArray.andInto(
|
|||||||
thatStartIndex: Int = 0,
|
thatStartIndex: Int = 0,
|
||||||
) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a and b }
|
) = 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 =
|
infix fun ByteArray.and(that: ByteArray): ByteArray =
|
||||||
combine(that) { a, b -> a and b }
|
combine(that) { a, b -> a and b }
|
||||||
|
|
||||||
|
infix fun ByteArray.and(that: Byte): ByteArray =
|
||||||
|
combine(that) { a, b -> a and b }
|
||||||
|
|
||||||
fun ByteArray.orInto(
|
fun ByteArray.orInto(
|
||||||
that: ByteArray,
|
that: ByteArray,
|
||||||
destination: ByteArray,
|
destination: ByteArray,
|
||||||
@@ -74,9 +103,20 @@ fun ByteArray.orInto(
|
|||||||
thatStartIndex: Int = 0,
|
thatStartIndex: Int = 0,
|
||||||
) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b }
|
) = 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 =
|
infix fun ByteArray.or(that: ByteArray): ByteArray =
|
||||||
combine(that) { a, b -> a or b }
|
combine(that) { a, b -> a or b }
|
||||||
|
|
||||||
|
infix fun ByteArray.or(that: Byte): ByteArray =
|
||||||
|
combine(that) { a, b -> a or b }
|
||||||
|
|
||||||
fun ByteArray.xorInto(
|
fun ByteArray.xorInto(
|
||||||
that: ByteArray,
|
that: ByteArray,
|
||||||
destination: ByteArray,
|
destination: ByteArray,
|
||||||
@@ -86,5 +126,16 @@ fun ByteArray.xorInto(
|
|||||||
thatStartIndex: Int = 0,
|
thatStartIndex: Int = 0,
|
||||||
) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b }
|
) = 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 =
|
infix fun ByteArray.xor(that: ByteArray): ByteArray =
|
||||||
combine(that) { a, b -> a xor b }
|
combine(that) { a, b -> a xor b }
|
||||||
|
|
||||||
|
infix fun ByteArray.xor(that: Byte): ByteArray =
|
||||||
|
combine(that) { a, b -> a xor b }
|
||||||
|
|||||||
@@ -31,9 +31,26 @@ fun UByteArray.andInto(
|
|||||||
thatStartIndex,
|
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 =
|
infix fun UByteArray.and(that: UByteArray): UByteArray =
|
||||||
UByteArray(size).also { andInto(that, it) }
|
UByteArray(size).also { andInto(that, it) }
|
||||||
|
|
||||||
|
infix fun UByteArray.and(that: UByte): UByteArray =
|
||||||
|
UByteArray(size).also { andInto(that, it) }
|
||||||
|
|
||||||
fun UByteArray.orInto(
|
fun UByteArray.orInto(
|
||||||
that: UByteArray,
|
that: UByteArray,
|
||||||
destination: UByteArray,
|
destination: UByteArray,
|
||||||
@@ -50,9 +67,26 @@ fun UByteArray.orInto(
|
|||||||
thatStartIndex,
|
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 =
|
infix fun UByteArray.or(that: UByteArray): UByteArray =
|
||||||
UByteArray(size).also { orInto(that, it) }
|
UByteArray(size).also { orInto(that, it) }
|
||||||
|
|
||||||
|
infix fun UByteArray.or(that: UByte): UByteArray =
|
||||||
|
UByteArray(size).also { orInto(that, it) }
|
||||||
|
|
||||||
fun UByteArray.xorInto(
|
fun UByteArray.xorInto(
|
||||||
that: UByteArray,
|
that: UByteArray,
|
||||||
destination: UByteArray,
|
destination: UByteArray,
|
||||||
@@ -69,5 +103,22 @@ fun UByteArray.xorInto(
|
|||||||
thatStartIndex,
|
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 =
|
infix fun UByteArray.xor(that: UByteArray): UByteArray =
|
||||||
UByteArray(size).also { xorInto(that, it) }
|
UByteArray(size).also { xorInto(that, it) }
|
||||||
|
|
||||||
|
infix fun UByteArray.xor(that: UByte): UByteArray =
|
||||||
|
UByteArray(size).also { xorInto(that, it) }
|
||||||
|
|||||||
Reference in New Issue
Block a user