Merge pull request '1.3.1 release' (#6) from develop into main
All checks were successful
/ publish (push) Successful in 2m14s
All checks were successful
/ publish (push) Successful in 2m14s
Reviewed-on: Infendro/bytes-kt#6
This commit was merged in pull request #6.
This commit is contained in:
15
README.md
15
README.md
@@ -1,15 +1,6 @@
|
||||
# bytes-kt
|
||||
|
||||
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `UByteArray` and `ByteArray`.
|
||||
|
||||
## Targets
|
||||
|
||||
| Target | Status |
|
||||
|------------------|---------------|
|
||||
| JVM | Supported |
|
||||
| JavaScript | Supported |
|
||||
| Native (Linux) | Supported |
|
||||
| Native (Windows) | Not Supported |
|
||||
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `ByteArray` and `UByteArray`.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -20,7 +11,7 @@ repositories {
|
||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("com.infendro:bytearray:1.3.0")
|
||||
commonMain.dependencies {
|
||||
implementation("com.infendro:bytes:1.3.1")
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
@file:OptIn(ExperimentalWasmDsl::class)
|
||||
|
||||
import com.infendro.plugin.infendro
|
||||
import com.infendro.plugin.token
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.3.0"
|
||||
version = "1.3.1"
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.infendro)
|
||||
@@ -13,9 +16,13 @@ plugins {
|
||||
kotlin {
|
||||
jvm()
|
||||
linuxX64()
|
||||
js {
|
||||
nodejs()
|
||||
}
|
||||
linuxArm64()
|
||||
mingwX64()
|
||||
macosX64()
|
||||
macosArm64()
|
||||
js { nodejs() }
|
||||
wasmJs { nodejs() }
|
||||
wasmWasi { nodejs() }
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -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)
|
||||
120
src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt
Normal file
120
src/commonTest/kotlin/com/infendro/bytes/bytearray/Conversion.kt
Normal file
@@ -0,0 +1,120 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ByteArrayConversionTest {
|
||||
@Test
|
||||
fun toInt() {
|
||||
val expected = 0x12345678
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt(),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUInt() {
|
||||
val expected = 0x12345678U
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt(),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toIntArray() {
|
||||
val expected = intArrayOf(0x12345678, 0x78563412)
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
).toIntArray(),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
).toIntArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUIntArray() {
|
||||
val expected = uintArrayOf(0x12345678U, 0x78563412U)
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
).toUIntArray(),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
).toUIntArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLong() {
|
||||
val expected = 0x1234567878563412L
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toLong(),
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toLong(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toULong() {
|
||||
val expected = 0x1234567878563412UL
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toULong(),
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toULong(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLongArray() {
|
||||
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
).toLongArray(),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
).toLongArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toULongArray() {
|
||||
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
|
||||
val actual = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
).toULongArray(),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
).toULongArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertFails
|
||||
|
||||
class ByteArrayOperatorsTest {
|
||||
@Test
|
||||
fun inv() {
|
||||
val expected = ubyteArrayOf(0xFFU, 0xE0U).asByteArray()
|
||||
val actual = byteArrayOf(0x00, 0x1F).inv()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun andSize() {
|
||||
assertFails {
|
||||
byteArrayOf() and byteArrayOf(0x00)
|
||||
}
|
||||
assertFails {
|
||||
byteArrayOf(0x00) and byteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun and() {
|
||||
val expected = byteArrayOf(0x18, 0x00)
|
||||
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun orSize() {
|
||||
assertFails {
|
||||
byteArrayOf() or byteArrayOf(0x00)
|
||||
}
|
||||
assertFails {
|
||||
byteArrayOf(0x00) or byteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun or() {
|
||||
val expected = byteArrayOf(0x7E, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xorSize() {
|
||||
assertFails {
|
||||
byteArrayOf() xor byteArrayOf(0x00)
|
||||
}
|
||||
assertFails {
|
||||
byteArrayOf(0x00) xor byteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xor() {
|
||||
val expected = byteArrayOf(0x66, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
37
src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
37
src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class IntConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x12345678.toByteArray(),
|
||||
0x12345678.toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x12345678.toUByteArray(),
|
||||
0x12345678.toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.infendro.bytes.intarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class IntArrayConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
intArrayOf(
|
||||
0x12345678,
|
||||
0x12345678,
|
||||
).toByteArray(),
|
||||
intArrayOf(
|
||||
0x12345678,
|
||||
0x12345678,
|
||||
).toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
intArrayOf(
|
||||
0x12345678,
|
||||
0x12345678,
|
||||
).toUByteArray(),
|
||||
intArrayOf(
|
||||
0x12345678,
|
||||
0x12345678,
|
||||
).toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
37
src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class LongConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x1234567812345678L.toByteArray(),
|
||||
0x1234567812345678L.toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x1234567812345678L.toUByteArray(),
|
||||
0x1234567812345678L.toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.infendro.bytes.longarray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class LongArrayConversionTest {
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
longArrayOf(
|
||||
0x1234567812345678L,
|
||||
0x1234567812345678L,
|
||||
).toByteArray(),
|
||||
longArrayOf(
|
||||
0x1234567812345678L,
|
||||
0x1234567812345678L,
|
||||
).toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
longArrayOf(
|
||||
0x1234567812345678L,
|
||||
0x1234567812345678L,
|
||||
).toUByteArray(),
|
||||
longArrayOf(
|
||||
0x1234567812345678L,
|
||||
0x1234567812345678L,
|
||||
).toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `UByteArray Conversion Test` {
|
||||
@Test
|
||||
fun toUInt() {
|
||||
val expected = 0x12345678U
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt(),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toInt() {
|
||||
val expected = 0x12345678
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt(),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toUIntArray() {
|
||||
val expected = uintArrayOf(0x12345678U, 0x12345678U)
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
).toUIntArray(),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
).toUIntArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toIntArray() {
|
||||
val expected = intArrayOf(0x12345678, 0x12345678)
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
).toIntArray(),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
).toIntArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toULong() {
|
||||
val expected = 0x1234567812345678UL
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong(),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLong() {
|
||||
val expected = 0x1234567812345678L
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong(),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(order = LITTLE_ENDIAN),
|
||||
)
|
||||
assertEquals(expected, actual[0])
|
||||
assertEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toULongArray() {
|
||||
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
).toULongArray(),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
).toULongArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLongArray() {
|
||||
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
|
||||
val actual = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
).toLongArray(),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
).toLongArray(LITTLE_ENDIAN),
|
||||
)
|
||||
assertContentEquals(expected, actual[0])
|
||||
assertContentEquals(expected, actual[1])
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package com.infendro.bytes.ubytearray
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertFails
|
||||
|
||||
class `UByteArray Operators Test` {
|
||||
@Test
|
||||
fun inv() {
|
||||
val expected = ubyteArrayOf(0xFFU, 0xE0U)
|
||||
val actual = ubyteArrayOf(0x00U, 0x1FU).inv()
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `and - size`() {
|
||||
assertFails {
|
||||
ubyteArrayOf() and ubyteArrayOf(0x00U)
|
||||
}
|
||||
assertFails {
|
||||
ubyteArrayOf(0x00U) and ubyteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun and() {
|
||||
val expected = ubyteArrayOf(0x18U, 0x00U)
|
||||
val actual = ubyteArrayOf(0x5CU, 0x70U) and ubyteArrayOf(0x3AU, 0x0FU)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `or - size`() {
|
||||
assertFails {
|
||||
ubyteArrayOf() or ubyteArrayOf(0x00U)
|
||||
}
|
||||
assertFails {
|
||||
ubyteArrayOf(0x00U) or ubyteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun or() {
|
||||
val expected = ubyteArrayOf(0x7EU, 0x7FU)
|
||||
val actual = ubyteArrayOf(0x5CU, 0x70U) or ubyteArrayOf(0x3AU, 0x0FU)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `xor - size`() {
|
||||
assertFails {
|
||||
ubyteArrayOf() xor ubyteArrayOf(0x00U)
|
||||
}
|
||||
assertFails {
|
||||
ubyteArrayOf(0x00U) xor ubyteArrayOf()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xor() {
|
||||
val expected = ubyteArrayOf(0x66U, 0x7FU)
|
||||
val actual = ubyteArrayOf(0x5CU, 0x70U) xor ubyteArrayOf(0x3AU, 0x0FU)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `UByteArray String Test` {
|
||||
class UByteArrayStringTest {
|
||||
@Test
|
||||
fun encodeToUByteArray() {
|
||||
val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U)
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
package com.infendro.bytes.uint
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class `UInt Conversion Test` {
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x12345678U.toUByteArray(),
|
||||
0x12345678U.toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x12345678U.toByteArray(),
|
||||
0x12345678U.toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Array toUByteArray`() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U,
|
||||
),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
uintArrayOf(
|
||||
0x12345678U,
|
||||
0x12345678U,
|
||||
).toUByteArray(),
|
||||
uintArrayOf(
|
||||
0x12345678U,
|
||||
0x12345678U,
|
||||
).toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Array toByteArray`() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
0x78, 0x56, 0x34, 0x12,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
uintArrayOf(
|
||||
0x12345678U,
|
||||
0x12345678U,
|
||||
).toByteArray(),
|
||||
uintArrayOf(
|
||||
0x12345678U,
|
||||
0x12345678U,
|
||||
).toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package com.infendro.bytes.ulong
|
||||
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
|
||||
class `ULong Conversion Test` {
|
||||
@Test
|
||||
fun toUByteArray() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
|
||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x1234567812345678UL.toUByteArray(),
|
||||
0x1234567812345678UL.toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toByteArray() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
|
||||
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
0x1234567812345678UL.toByteArray(),
|
||||
0x1234567812345678UL.toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Array toUByteArray`() {
|
||||
val expected = arrayOf(
|
||||
ubyteArrayOf(
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||
),
|
||||
ubyteArrayOf(
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toUByteArray(),
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toUByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Array toByteArray`() {
|
||||
val expected = arrayOf(
|
||||
byteArrayOf(
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||
),
|
||||
byteArrayOf(
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||
),
|
||||
)
|
||||
val actual = arrayOf(
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toByteArray(),
|
||||
ulongArrayOf(
|
||||
0x1234567812345678UL,
|
||||
0x1234567812345678UL,
|
||||
).toByteArray(LITTLE_ENDIAN),
|
||||
)
|
||||
for (i in 0..1) {
|
||||
assertContentEquals(expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user