implement optimizations
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
enum class ByteOrder {
|
||||
BIG_ENDIAN, LITTLE_ENDIAN;
|
||||
}
|
||||
|
||||
fun ByteArray.normalize(
|
||||
order: ByteOrder,
|
||||
): ByteArray = when (order) {
|
||||
BIG_ENDIAN -> this
|
||||
LITTLE_ENDIAN -> reversedArray()
|
||||
}
|
||||
|
||||
fun UByteArray.normalize(
|
||||
order: ByteOrder,
|
||||
): UByteArray = asByteArray().normalize(order).asUByteArray()
|
||||
@@ -1,177 +0,0 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||
|
||||
fun ByteArray.toInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Int {
|
||||
require(size == 4)
|
||||
|
||||
return normalize(order)
|
||||
.fold(0) { acc, byte -> (acc shl 8) or (byte.toInt() and 0xFF) }
|
||||
}
|
||||
|
||||
fun UByteArray.toInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Int = asByteArray().toInt(order)
|
||||
|
||||
fun ByteArray.toUInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UInt = toInt(order).toUInt()
|
||||
|
||||
fun UByteArray.toUInt(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UInt = asByteArray().toUInt(order)
|
||||
|
||||
fun ByteArray.toIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): IntArray {
|
||||
require(size % 4 == 0)
|
||||
|
||||
return asList()
|
||||
.chunked(4) { it.toByteArray().toInt(order) }
|
||||
.toIntArray()
|
||||
}
|
||||
|
||||
fun UByteArray.toIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): IntArray = asByteArray().toIntArray(order)
|
||||
|
||||
fun ByteArray.toUIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UIntArray = toIntArray(order).asUIntArray()
|
||||
|
||||
fun UByteArray.toUIntArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UIntArray = asByteArray().toUIntArray(order)
|
||||
|
||||
fun Int.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(4) { i ->
|
||||
val offset = (3 - i) * 8
|
||||
(this ushr offset).toByte()
|
||||
}
|
||||
return bytes.normalize(order)
|
||||
}
|
||||
|
||||
fun UInt.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray = toInt().toByteArray(order)
|
||||
|
||||
fun Int.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = toByteArray(order).asUByteArray()
|
||||
|
||||
fun UInt.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = toInt().toUByteArray(order)
|
||||
|
||||
fun IntArray.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = mutableListOf<Byte>()
|
||||
for (n in this) {
|
||||
bytes += n.toByteArray(order).toList()
|
||||
}
|
||||
return bytes.toByteArray()
|
||||
}
|
||||
|
||||
fun UIntArray.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray = asIntArray().toByteArray(order)
|
||||
|
||||
fun IntArray.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = toByteArray(order).asUByteArray()
|
||||
|
||||
fun UIntArray.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = asIntArray().toUByteArray(order)
|
||||
|
||||
fun ByteArray.toLong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Long {
|
||||
require(size == 8)
|
||||
|
||||
return normalize(order)
|
||||
.fold(0L) { acc, byte -> (acc shl 8) or (byte.toLong() and 0xFFL) }
|
||||
}
|
||||
|
||||
fun UByteArray.toLong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): Long = asByteArray().toLong(order)
|
||||
|
||||
fun ByteArray.toULong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong = toLong(order).toULong()
|
||||
|
||||
fun UByteArray.toULong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong = asByteArray().toULong(order)
|
||||
|
||||
fun ByteArray.toLongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): LongArray {
|
||||
require(size % 8 == 0)
|
||||
|
||||
return asList()
|
||||
.chunked(8) { it.toByteArray().toLong(order) }
|
||||
.toLongArray()
|
||||
}
|
||||
|
||||
fun UByteArray.toLongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): LongArray = asByteArray().toLongArray(order)
|
||||
|
||||
fun ByteArray.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray = toLongArray(order).asULongArray()
|
||||
|
||||
fun UByteArray.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray = asByteArray().toULongArray(order)
|
||||
|
||||
fun Long.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = ByteArray(8) { i ->
|
||||
val offset = (7 - i) * 8
|
||||
(this ushr offset).toByte()
|
||||
}
|
||||
return bytes.normalize(order)
|
||||
}
|
||||
|
||||
fun ULong.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray = toLong().toByteArray(order)
|
||||
|
||||
fun Long.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = toByteArray(order).asUByteArray()
|
||||
|
||||
fun ULong.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = toLong().toUByteArray(order)
|
||||
|
||||
fun LongArray.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray {
|
||||
val bytes = mutableListOf<Byte>()
|
||||
for (n in this) {
|
||||
bytes += n.toByteArray(order).asList()
|
||||
}
|
||||
return bytes.toByteArray()
|
||||
}
|
||||
|
||||
fun ULongArray.toByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ByteArray = asLongArray().toByteArray(order)
|
||||
|
||||
fun LongArray.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = toByteArray(order).asUByteArray()
|
||||
|
||||
fun ULongArray.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): UByteArray = asLongArray().toUByteArray(order)
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
fun ByteArray.padStart(
|
||||
length: Int,
|
||||
padByte: Byte,
|
||||
): ByteArray {
|
||||
val bytes = mutableListOf<Byte>()
|
||||
repeat(length - size) {
|
||||
bytes += padByte
|
||||
}
|
||||
bytes += this.asList()
|
||||
return bytes.toByteArray()
|
||||
}
|
||||
|
||||
fun UByteArray.padStart(
|
||||
length: Int,
|
||||
padByte: UByte,
|
||||
): UByteArray = asByteArray().padStart(length, padByte.toByte()).asUByteArray()
|
||||
|
||||
fun ByteArray.padEnd(
|
||||
length: Int,
|
||||
padByte: Byte,
|
||||
): ByteArray {
|
||||
val bytes = mutableListOf<Byte>()
|
||||
bytes += this.asList()
|
||||
repeat(length - size) {
|
||||
bytes += padByte
|
||||
}
|
||||
return bytes.toByteArray()
|
||||
}
|
||||
|
||||
fun UByteArray.padEnd(
|
||||
length: Int,
|
||||
padByte: UByte,
|
||||
): UByteArray = asByteArray().padEnd(length, padByte.toByte()).asUByteArray()
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.infendro.bytearray
|
||||
|
||||
import kotlin.experimental.and
|
||||
import kotlin.experimental.or
|
||||
import kotlin.experimental.xor
|
||||
|
||||
infix fun ByteArray.and(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
require(this.size == that.size)
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] and that[i]
|
||||
}
|
||||
}
|
||||
|
||||
infix fun UByteArray.and(
|
||||
that: UByteArray,
|
||||
): UByteArray = (this.asByteArray() and that.asByteArray()).asUByteArray()
|
||||
|
||||
infix fun ByteArray.or(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
require(this.size == that.size)
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] or that[i]
|
||||
}
|
||||
}
|
||||
|
||||
infix fun UByteArray.or(
|
||||
that: UByteArray,
|
||||
): UByteArray = (this.asByteArray() or that.asByteArray()).asUByteArray()
|
||||
|
||||
infix fun ByteArray.xor(
|
||||
that: ByteArray,
|
||||
): ByteArray {
|
||||
require(this.size == that.size)
|
||||
|
||||
return ByteArray(size) { i ->
|
||||
this[i] xor that[i]
|
||||
}
|
||||
}
|
||||
|
||||
infix fun UByteArray.xor(
|
||||
that: UByteArray,
|
||||
): UByteArray = (this.asByteArray() xor that.asByteArray()).asUByteArray()
|
||||
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 }
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.infendro.bytearray
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
fun String.encodeToUByteArray(): UByteArray =
|
||||
encodeToByteArray().asUByteArray()
|
||||
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