189 lines
5.4 KiB
Kotlin
189 lines
5.4 KiB
Kotlin
package com.infendro.bytes.bytearray
|
|
|
|
import com.infendro.bytes.byte.not
|
|
import kotlin.experimental.and
|
|
import kotlin.experimental.or
|
|
import kotlin.experimental.xor
|
|
|
|
fun ByteArray.andInto(
|
|
that: ByteArray,
|
|
destination: ByteArray,
|
|
destinationOffset: Int = 0,
|
|
startIndex: Int = 0,
|
|
endIndex: Int = size,
|
|
thatStartIndex: Int = 0,
|
|
) {
|
|
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))
|
|
|
|
for (i in 0..<length) {
|
|
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
|
|
}
|
|
}
|
|
|
|
fun ByteArray.andInto(
|
|
that: Byte,
|
|
destination: ByteArray,
|
|
destinationOffset: Int = 0,
|
|
startIndex: Int = 0,
|
|
endIndex: Int = size,
|
|
) {
|
|
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] and that
|
|
}
|
|
}
|
|
|
|
fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int) =
|
|
andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
|
|
|
fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
|
andInto(that, this, startIndex, startIndex, endIndex)
|
|
|
|
infix fun ByteArray.and(that: ByteArray): ByteArray {
|
|
val result = ByteArray(size)
|
|
andInto(that, result)
|
|
return result
|
|
}
|
|
|
|
infix fun ByteArray.and(that: Byte): ByteArray {
|
|
val result = ByteArray(size)
|
|
andInto(that, result)
|
|
return result
|
|
}
|
|
|
|
fun ByteArray.orInto(
|
|
that: ByteArray,
|
|
destination: ByteArray,
|
|
destinationOffset: Int = 0,
|
|
startIndex: Int = 0,
|
|
endIndex: Int = size,
|
|
thatStartIndex: Int = 0,
|
|
) {
|
|
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))
|
|
|
|
for (i in 0..<length) {
|
|
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
|
|
}
|
|
}
|
|
|
|
fun ByteArray.orInto(
|
|
that: Byte,
|
|
destination: ByteArray,
|
|
destinationOffset: Int = 0,
|
|
startIndex: Int = 0,
|
|
endIndex: Int = size,
|
|
) {
|
|
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 {
|
|
val result = ByteArray(size)
|
|
orInto(that, result)
|
|
return result
|
|
}
|
|
|
|
infix fun ByteArray.or(that: Byte): ByteArray {
|
|
val result = ByteArray(size)
|
|
orInto(that, result)
|
|
return result
|
|
}
|
|
|
|
fun ByteArray.xorInto(
|
|
that: ByteArray,
|
|
destination: ByteArray,
|
|
destinationOffset: Int = 0,
|
|
startIndex: Int = 0,
|
|
endIndex: Int = size,
|
|
thatStartIndex: Int = 0,
|
|
) {
|
|
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))
|
|
|
|
for (i in 0..<length) {
|
|
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
|
|
}
|
|
}
|
|
|
|
fun ByteArray.xorInto(
|
|
that: Byte,
|
|
destination: ByteArray,
|
|
destinationOffset: Int = 0,
|
|
startIndex: Int = 0,
|
|
endIndex: Int = size,
|
|
) {
|
|
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 {
|
|
val result = ByteArray(size)
|
|
xorInto(that, result)
|
|
return result
|
|
}
|
|
|
|
infix fun ByteArray.xor(that: Byte): ByteArray {
|
|
val result = ByteArray(size)
|
|
xorInto(that, result)
|
|
return result
|
|
}
|
|
|
|
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
|
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]
|
|
}
|
|
}
|
|
|
|
fun ByteArray.inv(startIndex: Int, endIndex: Int) =
|
|
invInto(this, startIndex, startIndex, endIndex)
|
|
|
|
operator fun ByteArray.not(): ByteArray {
|
|
val result = ByteArray(size)
|
|
invInto(result)
|
|
return result
|
|
}
|