Improve shorthand bytearray operator functions
This commit is contained in:
@@ -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..<length) {
|
||||
destination[destinationOffset + i] = combine(this[startIndex + i], that[thatStartIndex + i])
|
||||
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun ByteArray.combineInto(
|
||||
fun ByteArray.andInto(
|
||||
that: Byte,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
combine: (Byte, Byte) -> 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..<length) {
|
||||
destination[destinationOffset + i] = combine(this[startIndex + i], that)
|
||||
destination[destinationOffset + i] = this[startIndex + i] and that
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun ByteArray.combine(that: ByteArray, combine: (Byte, Byte) -> 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..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.orInto(
|
||||
@@ -112,15 +85,32 @@ fun ByteArray.orInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> 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..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] or that
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.orWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int) =
|
||||
orInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
fun ByteArray.orWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
orInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.or(that: ByteArray): ByteArray {
|
||||
return combine(that) { a, b -> 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..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
@@ -145,15 +139,32 @@ fun ByteArray.xorInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> 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..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] xor that
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.xorWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int) =
|
||||
xorInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
fun ByteArray.xorWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
xorInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.xor(that: ByteArray): ByteArray {
|
||||
return combine(that) { a, b -> 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)
|
||||
|
||||
Reference in New Issue
Block a user