implement optimizations
This commit is contained in:
5
src/commonMain/kotlin/com/infendro/bytes/ByteOrder.kt
Normal file
5
src/commonMain/kotlin/com/infendro/bytes/ByteOrder.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.infendro.bytes
|
||||
|
||||
enum class ByteOrder {
|
||||
BIG_ENDIAN, LITTLE_ENDIAN;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ubytearray.toInt
|
||||
import com.infendro.bytes.ubytearray.toLong
|
||||
import com.infendro.bytes.ubytearray.toUInt
|
||||
import com.infendro.bytes.ubytearray.toULong
|
||||
|
||||
fun ByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toUInt(startIndex, order)
|
||||
|
||||
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toInt(startIndex, order)
|
||||
|
||||
fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
IntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toULong(startIndex, order)
|
||||
|
||||
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toLong(startIndex, order)
|
||||
|
||||
fun ByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
LongArray(size / 8).also { copyInto(it, order = order) }
|
||||
37
src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt
Normal file
37
src/commonMain/kotlin/com/infendro/bytes/bytearray/Copy.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ubytearray.copyInto
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: UIntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: ULongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ubytearray.*
|
||||
|
||||
fun ByteArray.invInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) = asUByteArray().invInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex)
|
||||
|
||||
fun ByteArray.inv() =
|
||||
(asUByteArray().inv()).asByteArray()
|
||||
|
||||
fun ByteArray.andInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().andInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
|
||||
infix fun ByteArray.and(that: ByteArray) =
|
||||
(this.asUByteArray() and that.asUByteArray()).asByteArray()
|
||||
|
||||
fun ByteArray.orInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().orInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
|
||||
infix fun ByteArray.or(that: ByteArray) =
|
||||
(this.asUByteArray() or that.asUByteArray()).asByteArray()
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().xorInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
|
||||
infix fun ByteArray.xor(that: ByteArray) =
|
||||
(this.asUByteArray() xor that.asUByteArray()).asByteArray()
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
33
src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt
Normal file
33
src/commonMain/kotlin/com/infendro/bytes/int/Copy.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.uint.copyInto
|
||||
|
||||
fun Int.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toUInt().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun Int.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toUInt().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun IntArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun IntArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
33
src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt
Normal file
33
src/commonMain/kotlin/com/infendro/bytes/long/Copy.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ulong.copyInto
|
||||
|
||||
fun Long.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toULong().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun Long.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toULong().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun LongArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asULongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun LongArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asULongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun UByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt {
|
||||
require(startIndex in 0..(size - 4))
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
(this[startIndex].toUInt() shl 24) or (this[startIndex + 1].toUInt() shl 16) or
|
||||
(this[startIndex + 2].toUInt() shl 8) or this[startIndex + 3].toUInt()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
this[startIndex].toUInt() or (this[startIndex + 1].toUInt() shl 8) or
|
||||
(this[startIndex + 2].toUInt() shl 16) or (this[startIndex + 3].toUInt() shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
toUInt(startIndex, order).toInt()
|
||||
|
||||
fun UByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
IntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong {
|
||||
require(startIndex in 0..(size - 8))
|
||||
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
(this[startIndex].toULong() shl 56) or (this[startIndex + 1].toULong() shl 48) or
|
||||
(this[startIndex + 2].toULong() shl 40) or (this[startIndex + 3].toULong() shl 32) or
|
||||
(this[startIndex + 4].toULong() shl 24) or (this[startIndex + 5].toULong() shl 16) or
|
||||
(this[startIndex + 6].toULong() shl 8) or this[startIndex + 7].toULong()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
this[startIndex].toULong() or (this[startIndex + 1].toULong() shl 8) or
|
||||
(this[startIndex + 2].toULong() shl 16) or (this[startIndex + 3].toULong() shl 24) or
|
||||
(this[startIndex + 4].toULong() shl 32) or (this[startIndex + 5].toULong() shl 40) or
|
||||
(this[startIndex + 6].toULong() shl 48) or (this[startIndex + 7].toULong() shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
toULong(startIndex, order).toLong()
|
||||
|
||||
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
LongArray(size / 8).also { copyInto(it, order = order) }
|
||||
85
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt
Normal file
85
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Copy.kt
Normal file
@@ -0,0 +1,85 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: UIntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0 && length % 4 == 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length / 4)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
destination[destinationOffset + i] =
|
||||
(this[startIndex].toUInt() shl 24) or (this[startIndex + 1].toUInt() shl 16) or
|
||||
(this[startIndex + 2].toUInt() shl 8) or this[startIndex + 3].toUInt()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
destination[destinationOffset + i] =
|
||||
this[startIndex].toUInt() or (this[startIndex + 1].toUInt() shl 8) or
|
||||
(this[startIndex + 2].toUInt() shl 16) or (this[startIndex + 3].toUInt() shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUIntArray(), destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: ULongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0 && length % 8 == 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length / 8)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
destination[destinationOffset + i] =
|
||||
(this[startIndex].toULong() shl 56) or (this[startIndex + 1].toULong() shl 48) or
|
||||
(this[startIndex + 2].toULong() shl 40) or (this[startIndex + 3].toULong() shl 32) or
|
||||
(this[startIndex + 4].toULong() shl 24) or (this[startIndex + 5].toULong() shl 16) or
|
||||
(this[startIndex + 6].toULong() shl 8) or this[startIndex + 7].toULong()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
destination[destinationOffset + i] =
|
||||
this[startIndex].toULong() or (this[startIndex + 1].toULong() shl 8) or
|
||||
(this[startIndex + 2].toULong() shl 16) or (this[startIndex + 3].toULong() shl 24) or
|
||||
(this[startIndex + 4].toULong() shl 32) or (this[startIndex + 5].toULong() shl 40) or
|
||||
(this[startIndex + 6].toULong() shl 48) or (this[startIndex + 7].toULong() shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asULongArray(), destinationOffset, startIndex, endIndex, order)
|
||||
105
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt
Normal file
105
src/commonMain/kotlin/com/infendro/bytes/ubytearray/Operators.kt
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
fun UByteArray.invInto(
|
||||
destination: UByteArray,
|
||||
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))
|
||||
|
||||
repeat(length) { i ->
|
||||
destination[destinationOffset + i] = this[startIndex + i].inv()
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.inv() = UByteArray(size).also { invInto(it) }
|
||||
|
||||
private inline fun UByteArray.combineInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
combine: (UByte, UByte) -> UByte,
|
||||
) {
|
||||
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))
|
||||
|
||||
repeat(length) { i ->
|
||||
destination[destinationOffset + i] = combine(this[startIndex + i], that[thatStartIndex + i])
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun UByteArray.combine(
|
||||
that: UByteArray,
|
||||
combine: (UByte, UByte) -> UByte,
|
||||
): UByteArray {
|
||||
require(this.size == that.size)
|
||||
|
||||
return UByteArray(size).also { combineInto(that, it, combine = combine) }
|
||||
}
|
||||
|
||||
fun UByteArray.andInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a and b }
|
||||
|
||||
infix fun UByteArray.and(that: UByteArray) =
|
||||
combine(that) { a, b -> a and b }
|
||||
|
||||
fun UByteArray.orInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a or b }
|
||||
|
||||
infix fun UByteArray.or(that: UByteArray) =
|
||||
combine(that) { a, b -> a or b }
|
||||
|
||||
fun UByteArray.xorInto(
|
||||
that: UByteArray,
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a xor b }
|
||||
|
||||
infix fun UByteArray.xor(that: UByteArray) =
|
||||
combine(that) { a, b -> a xor b }
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
fun String.encodeToUByteArray(): UByteArray =
|
||||
encodeToByteArray().asUByteArray()
|
||||
|
||||
fun UByteArray.decodeToString(): String =
|
||||
asByteArray().decodeToString()
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/uint/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.uint
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
59
src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt
Normal file
59
src/commonMain/kotlin/com/infendro/bytes/uint/Copy.kt
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.infendro.bytes.uint
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun UInt.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
require(destinationOffset in 0..(destination.size - 4))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 24).toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 3] = this.toUByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toUByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun UInt.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, order)
|
||||
|
||||
fun UIntArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length * 4)))
|
||||
|
||||
repeat(length) { i ->
|
||||
this[startIndex + i].copyInto(destination, destinationOffset + (i * 4), order)
|
||||
}
|
||||
}
|
||||
|
||||
fun UIntArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex, order)
|
||||
16
src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt
Normal file
16
src/commonMain/kotlin/com/infendro/bytes/ulong/Conversion.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.infendro.bytes.ulong
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
67
src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt
Normal file
67
src/commonMain/kotlin/com/infendro/bytes/ulong/Copy.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
package com.infendro.bytes.ulong
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun ULong.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
require(destinationOffset in 0..(destination.size - 8))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 56).toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 48).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 40).toUByte()
|
||||
destination[destinationOffset + 3] = (this shr 32).toUByte()
|
||||
destination[destinationOffset + 4] = (this shr 24).toUByte()
|
||||
destination[destinationOffset + 5] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 6] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 7] = this.toUByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toUByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toUByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toUByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toUByte()
|
||||
destination[destinationOffset + 4] = (this shr 32).toUByte()
|
||||
destination[destinationOffset + 5] = (this shr 40).toUByte()
|
||||
destination[destinationOffset + 6] = (this shr 48).toUByte()
|
||||
destination[destinationOffset + 7] = (this shr 56).toUByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ULong.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, order)
|
||||
|
||||
fun ULongArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - (length * 8)))
|
||||
|
||||
repeat(length) { i ->
|
||||
this[startIndex + i].copyInto(destination, destinationOffset + (i * 8), order)
|
||||
}
|
||||
}
|
||||
|
||||
fun ULongArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex, order)
|
||||
Reference in New Issue
Block a user