switch to signed-first approach
All checks were successful
/ test (pull_request) Successful in 2m47s
All checks were successful
/ test (pull_request) Successful in 2m47s
It turns out signed math is more performant
This commit is contained in:
@@ -2,31 +2,56 @@ 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
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun ByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toUInt(startIndex, order)
|
||||
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int {
|
||||
require(startIndex in 0..(size - 4))
|
||||
|
||||
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toInt(startIndex, order)
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
(this[startIndex].toInt() shl 24) or (this[startIndex + 1].toInt() shl 16) or
|
||||
(this[startIndex + 2].toInt() shl 8) or this[startIndex + 3].toInt()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
this[startIndex].toInt() or (this[startIndex + 1].toInt() shl 8) or
|
||||
(this[startIndex + 2].toInt() shl 16) or (this[startIndex + 3].toInt() shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
||||
fun ByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt =
|
||||
toInt(startIndex, order).toUInt()
|
||||
|
||||
fun ByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
fun ByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN): IntArray =
|
||||
IntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toULong(startIndex, order)
|
||||
fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray =
|
||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
asUByteArray().toLong(startIndex, order)
|
||||
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
|
||||
require(startIndex in 0..(size - 8))
|
||||
|
||||
fun ByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
return when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
(this[startIndex].toLong() shl 56) or (this[startIndex + 1].toLong() shl 48) or
|
||||
(this[startIndex + 2].toLong() shl 40) or (this[startIndex + 3].toLong() shl 32) or
|
||||
(this[startIndex + 4].toLong() shl 24) or (this[startIndex + 5].toLong() shl 16) or
|
||||
(this[startIndex + 6].toLong() shl 8) or this[startIndex + 7].toLong()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
this[startIndex].toLong() or (this[startIndex + 1].toLong() shl 8) or
|
||||
(this[startIndex + 2].toLong() shl 16) or (this[startIndex + 3].toLong() shl 24) or
|
||||
(this[startIndex + 4].toLong() shl 32) or (this[startIndex + 5].toLong() shl 40) or
|
||||
(this[startIndex + 6].toLong() shl 48) or (this[startIndex + 7].toLong() shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
fun ByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong =
|
||||
toLong(startIndex, order).toULong()
|
||||
|
||||
fun ByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN): LongArray =
|
||||
LongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN): ULongArray =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
@@ -2,15 +2,7 @@ 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)
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
@@ -18,15 +10,39 @@ fun ByteArray.copyInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
) {
|
||||
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 ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toInt() shl 24) or (this[offset + 1].toInt() shl 16) or
|
||||
(this[offset + 2].toInt() shl 8) or this[offset + 3].toInt()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
this[offset].toInt() or (this[offset + 1].toInt() shl 8) or
|
||||
(this[offset + 2].toInt() shl 16) or (this[offset + 3].toInt() shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: ULongArray,
|
||||
destination: UIntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
) = copyInto(destination.asIntArray(), destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
@@ -34,4 +50,40 @@ fun ByteArray.copyInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
) {
|
||||
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 / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toLong() shl 56) or (this[offset + 1].toLong() shl 48) or
|
||||
(this[offset + 2].toLong() shl 40) or (this[offset + 3].toLong() shl 32) or
|
||||
(this[offset + 4].toLong() shl 24) or (this[offset + 5].toLong() shl 16) or
|
||||
(this[offset + 6].toLong() shl 8) or this[offset + 7].toLong()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
this[offset].toLong() or (this[offset + 1].toLong() shl 8) or
|
||||
(this[offset + 2].toLong() shl 16) or (this[offset + 3].toLong() shl 24) or
|
||||
(this[offset + 4].toLong() shl 32) or (this[offset + 5].toLong() shl 40) or
|
||||
(this[offset + 6].toLong() shl 48) or (this[offset + 7].toLong() shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: ULongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asLongArray(), destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
@@ -1,16 +1,57 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ubytearray.*
|
||||
import kotlin.experimental.and
|
||||
import kotlin.experimental.inv
|
||||
import kotlin.experimental.or
|
||||
import kotlin.experimental.xor
|
||||
|
||||
fun ByteArray.invInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) = asUByteArray().invInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex)
|
||||
) {
|
||||
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 ByteArray.inv() =
|
||||
(asUByteArray().inv()).asByteArray()
|
||||
ByteArray(size).also { invInto(it) }
|
||||
|
||||
private inline fun ByteArray.combineInto(
|
||||
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)
|
||||
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 ByteArray.combine(
|
||||
that: ByteArray,
|
||||
combine: (Byte, Byte) -> Byte,
|
||||
): ByteArray {
|
||||
require(this.size == that.size)
|
||||
|
||||
return ByteArray(size).also { combineInto(that, it, combine = combine) }
|
||||
}
|
||||
|
||||
fun ByteArray.andInto(
|
||||
that: ByteArray,
|
||||
@@ -19,17 +60,10 @@ fun ByteArray.andInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().andInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a and b }
|
||||
|
||||
infix fun ByteArray.and(that: ByteArray) =
|
||||
(this.asUByteArray() and that.asUByteArray()).asByteArray()
|
||||
combine(that) { a, b -> a and b }
|
||||
|
||||
fun ByteArray.orInto(
|
||||
that: ByteArray,
|
||||
@@ -38,17 +72,10 @@ fun ByteArray.orInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().orInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b }
|
||||
|
||||
infix fun ByteArray.or(that: ByteArray) =
|
||||
(this.asUByteArray() or that.asUByteArray()).asByteArray()
|
||||
combine(that) { a, b -> a or b }
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
that: ByteArray,
|
||||
@@ -57,14 +84,7 @@ fun ByteArray.xorInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = asUByteArray().xorInto(
|
||||
that.asUByteArray(),
|
||||
destination.asUByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
)
|
||||
) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b }
|
||||
|
||||
infix fun ByteArray.xor(that: ByteArray) =
|
||||
(this.asUByteArray() xor that.asUByteArray()).asByteArray()
|
||||
combine(that) { a, b -> a xor b }
|
||||
|
||||
@@ -3,14 +3,8 @@ 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) }
|
||||
fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
@@ -2,32 +2,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)
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Int.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toUInt().copyInto(destination, destinationOffset, order)
|
||||
) {
|
||||
require(destinationOffset in 0..(destination.size - 4))
|
||||
|
||||
fun IntArray.copyInto(
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 24).toByte()
|
||||
destination[destinationOffset + 1] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 3] = this.toByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Int.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)
|
||||
) = copyInto(destination.asByteArray(), destinationOffset, order)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.infendro.bytes.intarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
49
src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt
Normal file
49
src/commonMain/kotlin/com/infendro/bytes/intarray/Copy.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.infendro.bytes.intarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun IntArray.copyInto(
|
||||
destination: ByteArray,
|
||||
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)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 4)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = (value shr 24).toByte()
|
||||
destination[offset + 1] = (value shr 16).toByte()
|
||||
destination[offset + 2] = (value shr 8).toByte()
|
||||
destination[offset + 3] = value.toByte()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 4)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = value.toByte()
|
||||
destination[offset + 1] = (value shr 8).toByte()
|
||||
destination[offset + 2] = (value shr 16).toByte()
|
||||
destination[offset + 3] = (value shr 24).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun IntArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asByteArray(), destinationOffset, startIndex, endIndex, order)
|
||||
@@ -3,14 +3,8 @@ 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) }
|
||||
fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
@@ -2,32 +2,41 @@ 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)
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Long.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = toULong().copyInto(destination, destinationOffset, order)
|
||||
) {
|
||||
require(destinationOffset in 0..(destination.size - 8))
|
||||
|
||||
fun LongArray.copyInto(
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
destination[destinationOffset] = (this shr 56).toByte()
|
||||
destination[destinationOffset + 1] = (this shr 48).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 40).toByte()
|
||||
destination[destinationOffset + 3] = (this shr 32).toByte()
|
||||
destination[destinationOffset + 4] = (this shr 24).toByte()
|
||||
destination[destinationOffset + 5] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 6] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 7] = this.toByte()
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
destination[destinationOffset] = this.toByte()
|
||||
destination[destinationOffset + 1] = (this shr 8).toByte()
|
||||
destination[destinationOffset + 2] = (this shr 16).toByte()
|
||||
destination[destinationOffset + 3] = (this shr 24).toByte()
|
||||
destination[destinationOffset + 4] = (this shr 32).toByte()
|
||||
destination[destinationOffset + 5] = (this shr 40).toByte()
|
||||
destination[destinationOffset + 6] = (this shr 48).toByte()
|
||||
destination[destinationOffset + 7] = (this shr 56).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Long.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)
|
||||
) = copyInto(destination.asByteArray(), destinationOffset, order)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.infendro.bytes.longarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
57
src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt
Normal file
57
src/commonMain/kotlin/com/infendro/bytes/longarray/Copy.kt
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.infendro.bytes.longarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun LongArray.copyInto(
|
||||
destination: ByteArray,
|
||||
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)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 8)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = (value shr 56).toByte()
|
||||
destination[offset + 1] = (value shr 48).toByte()
|
||||
destination[offset + 2] = (value shr 40).toByte()
|
||||
destination[offset + 3] = (value shr 32).toByte()
|
||||
destination[offset + 4] = (value shr 24).toByte()
|
||||
destination[offset + 5] = (value shr 16).toByte()
|
||||
destination[offset + 6] = (value shr 8).toByte()
|
||||
destination[offset + 7] = value.toByte()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 8)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = value.toByte()
|
||||
destination[offset + 1] = (value shr 8).toByte()
|
||||
destination[offset + 2] = (value shr 16).toByte()
|
||||
destination[offset + 3] = (value shr 24).toByte()
|
||||
destination[offset + 4] = (value shr 32).toByte()
|
||||
destination[offset + 5] = (value shr 40).toByte()
|
||||
destination[offset + 6] = (value shr 48).toByte()
|
||||
destination[offset + 7] = (value shr 56).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun LongArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asByteArray(), destinationOffset, startIndex, endIndex, order)
|
||||
@@ -2,56 +2,31 @@ package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import com.infendro.bytes.bytearray.toInt
|
||||
import com.infendro.bytes.bytearray.toLong
|
||||
import com.infendro.bytes.bytearray.toUInt
|
||||
import com.infendro.bytes.bytearray.toULong
|
||||
|
||||
fun UByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt {
|
||||
require(startIndex in 0..(size - 4))
|
||||
fun UByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int =
|
||||
asByteArray().toInt(startIndex, order)
|
||||
|
||||
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.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt =
|
||||
asByteArray().toUInt(startIndex, order)
|
||||
|
||||
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) =
|
||||
fun UByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN): IntArray =
|
||||
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))
|
||||
fun UByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray =
|
||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
||||
|
||||
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): Long =
|
||||
asByteArray().toLong(startIndex, order)
|
||||
|
||||
fun UByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
|
||||
toULong(startIndex, order).toLong()
|
||||
fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong =
|
||||
asByteArray().toULong(startIndex, order)
|
||||
|
||||
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN): LongArray =
|
||||
LongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN): ULongArray =
|
||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
||||
|
||||
@@ -2,39 +2,7 @@ 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 ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toUInt() shl 24) or (this[offset + 1].toUInt() shl 16) or
|
||||
(this[offset + 2].toUInt() shl 8) or this[offset + 3].toUInt()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
this[offset].toUInt() or (this[offset + 1].toUInt() shl 8) or
|
||||
(this[offset + 2].toUInt() shl 16) or (this[offset + 3].toUInt() shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
import com.infendro.bytes.bytearray.copyInto
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
@@ -42,7 +10,15 @@ fun UByteArray.copyInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUIntArray(), destinationOffset, startIndex, endIndex, order)
|
||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: UIntArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: ULongArray,
|
||||
@@ -50,35 +26,7 @@ fun UByteArray.copyInto(
|
||||
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 / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toULong() shl 56) or (this[offset + 1].toULong() shl 48) or
|
||||
(this[offset + 2].toULong() shl 40) or (this[offset + 3].toULong() shl 32) or
|
||||
(this[offset + 4].toULong() shl 24) or (this[offset + 5].toULong() shl 16) or
|
||||
(this[offset + 6].toULong() shl 8) or this[offset + 7].toULong()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
this[offset].toULong() or (this[offset + 1].toULong() shl 8) or
|
||||
(this[offset + 2].toULong() shl 16) or (this[offset + 3].toULong() shl 24) or
|
||||
(this[offset + 4].toULong() shl 32) or (this[offset + 5].toULong() shl 40) or
|
||||
(this[offset + 6].toULong() shl 48) or (this[offset + 7].toULong() shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun UByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
@@ -86,4 +34,4 @@ fun UByteArray.copyInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asULongArray(), destinationOffset, startIndex, endIndex, order)
|
||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
@@ -1,51 +1,19 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.bytearray.andInto
|
||||
import com.infendro.bytes.bytearray.invInto
|
||||
import com.infendro.bytes.bytearray.orInto
|
||||
import com.infendro.bytes.bytearray.xorInto
|
||||
|
||||
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))
|
||||
) = asByteArray().invInto(destination.asByteArray(), destinationOffset, startIndex, endIndex)
|
||||
|
||||
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.inv() =
|
||||
UByteArray(size).also { invInto(it) }
|
||||
|
||||
fun UByteArray.andInto(
|
||||
that: UByteArray,
|
||||
@@ -54,17 +22,17 @@ fun UByteArray.andInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
) = asByteArray().andInto(
|
||||
that.asByteArray(),
|
||||
destination.asByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a and b }
|
||||
)
|
||||
|
||||
infix fun UByteArray.and(that: UByteArray) =
|
||||
combine(that) { a, b -> a and b }
|
||||
UByteArray(size).also { andInto(that, it) }
|
||||
|
||||
fun UByteArray.orInto(
|
||||
that: UByteArray,
|
||||
@@ -73,17 +41,17 @@ fun UByteArray.orInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
) = asByteArray().orInto(
|
||||
that.asByteArray(),
|
||||
destination.asByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a or b }
|
||||
)
|
||||
|
||||
infix fun UByteArray.or(that: UByteArray) =
|
||||
combine(that) { a, b -> a or b }
|
||||
UByteArray(size).also { orInto(that, it) }
|
||||
|
||||
fun UByteArray.xorInto(
|
||||
that: UByteArray,
|
||||
@@ -92,14 +60,14 @@ fun UByteArray.xorInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) = combineInto(
|
||||
that,
|
||||
destination,
|
||||
) = asByteArray().xorInto(
|
||||
that.asByteArray(),
|
||||
destination.asByteArray(),
|
||||
destinationOffset,
|
||||
startIndex,
|
||||
endIndex,
|
||||
thatStartIndex,
|
||||
) { a, b -> a xor b }
|
||||
)
|
||||
|
||||
infix fun UByteArray.xor(that: UByteArray) =
|
||||
combine(that) { a, b -> a xor b }
|
||||
UByteArray(size).also { xorInto(that, it) }
|
||||
|
||||
@@ -3,14 +3,8 @@ 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) }
|
||||
fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(4).also { copyInto(it, order = order) }
|
||||
|
||||
@@ -2,77 +2,16 @@ 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
import com.infendro.bytes.int.copyInto
|
||||
|
||||
fun UInt.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, order)
|
||||
) = toInt().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun UIntArray.copyInto(
|
||||
fun UInt.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)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 4)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = (value shr 24).toUByte()
|
||||
destination[offset + 1] = (value shr 16).toUByte()
|
||||
destination[offset + 2] = (value shr 8).toUByte()
|
||||
destination[offset + 3] = value.toUByte()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 4)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = value.toUByte()
|
||||
destination[offset + 1] = (value shr 8).toUByte()
|
||||
destination[offset + 2] = (value shr 16).toUByte()
|
||||
destination[offset + 3] = (value shr 24).toUByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
) = toInt().copyInto(destination, destinationOffset, order)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.infendro.bytes.uintarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
|
||||
fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 4).also { copyInto(it, order = order) }
|
||||
21
src/commonMain/kotlin/com/infendro/bytes/uintarray/Copy.kt
Normal file
21
src/commonMain/kotlin/com/infendro/bytes/uintarray/Copy.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.infendro.bytes.uintarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.intarray.copyInto
|
||||
|
||||
fun UIntArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun UIntArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
@@ -3,14 +3,8 @@ 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) }
|
||||
fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(8).also { copyInto(it, order = order) }
|
||||
|
||||
@@ -2,93 +2,16 @@ 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
import com.infendro.bytes.long.copyInto
|
||||
|
||||
fun ULong.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = copyInto(destination.asUByteArray(), destinationOffset, order)
|
||||
) = toLong().copyInto(destination, destinationOffset, order)
|
||||
|
||||
fun ULongArray.copyInto(
|
||||
fun ULong.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)))
|
||||
|
||||
when (order) {
|
||||
BIG_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 8)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = (value shr 56).toUByte()
|
||||
destination[offset + 1] = (value shr 48).toUByte()
|
||||
destination[offset + 2] = (value shr 40).toUByte()
|
||||
destination[offset + 3] = (value shr 32).toUByte()
|
||||
destination[offset + 4] = (value shr 24).toUByte()
|
||||
destination[offset + 5] = (value shr 16).toUByte()
|
||||
destination[offset + 6] = (value shr 8).toUByte()
|
||||
destination[offset + 7] = value.toUByte()
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length) { i ->
|
||||
val offset = destinationOffset + (i * 8)
|
||||
val value = this[startIndex + i]
|
||||
destination[offset] = value.toUByte()
|
||||
destination[offset + 1] = (value shr 8).toUByte()
|
||||
destination[offset + 2] = (value shr 16).toUByte()
|
||||
destination[offset + 3] = (value shr 24).toUByte()
|
||||
destination[offset + 4] = (value shr 32).toUByte()
|
||||
destination[offset + 5] = (value shr 40).toUByte()
|
||||
destination[offset + 6] = (value shr 48).toUByte()
|
||||
destination[offset + 7] = (value shr 56).toUByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
) = toLong().copyInto(destination, destinationOffset, order)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.infendro.bytes.ulongarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
|
||||
fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
||||
UByteArray(size * 8).also { copyInto(it, order = order) }
|
||||
21
src/commonMain/kotlin/com/infendro/bytes/ulongarray/Copy.kt
Normal file
21
src/commonMain/kotlin/com/infendro/bytes/ulongarray/Copy.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.infendro.bytes.ulongarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.longarray.copyInto
|
||||
|
||||
fun ULongArray.copyInto(
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asLongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
|
||||
fun ULongArray.copyInto(
|
||||
destination: UByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) = asLongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
||||
Reference in New Issue
Block a user