1.3.1 release #6

Merged
Infendro merged 2 commits from develop into main 2026-01-15 13:18:43 +00:00
35 changed files with 862 additions and 826 deletions
Showing only changes of commit ff0e17b5ad - Show all commits

View File

@@ -1,15 +1,6 @@
# bytes-kt # bytes-kt
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `UByteArray` and `ByteArray`. `bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `ByteArray` and `UByteArray`.
## Targets
| Target | Status |
|------------------|---------------|
| JVM | Supported |
| JavaScript | Supported |
| Native (Linux) | Supported |
| Native (Windows) | Not Supported |
## Installation ## Installation
@@ -20,7 +11,7 @@ repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven") maven("https://git.infendro.com/api/packages/Infendro/maven")
} }
dependencies { commonMain.dependencies {
implementation("com.infendro:bytes:1.3.0") implementation("com.infendro:bytes:1.3.1")
} }
``` ```

View File

@@ -1,8 +1,11 @@
@file:OptIn(ExperimentalWasmDsl::class)
import com.infendro.plugin.infendro import com.infendro.plugin.infendro
import com.infendro.plugin.token import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro" group = "com.infendro"
version = "1.3.0" version = "1.3.1"
plugins { plugins {
alias(libs.plugins.infendro) alias(libs.plugins.infendro)
@@ -13,9 +16,13 @@ plugins {
kotlin { kotlin {
jvm() jvm()
linuxX64() linuxX64()
js { linuxArm64()
nodejs() mingwX64()
} macosX64()
macosArm64()
js { nodejs() }
wasmJs { nodejs() }
wasmWasi { nodejs() }
repositories { repositories {
mavenCentral() mavenCentral()

View File

@@ -2,31 +2,56 @@ package com.infendro.bytes.bytearray
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ubytearray.toInt import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
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) = fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int {
asUByteArray().toUInt(startIndex, order) require(startIndex in 0..(size - 4))
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) = return when (order) {
asUByteArray().toInt(startIndex, 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) = fun ByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt =
UIntArray(size / 4).also { copyInto(it, order = order) } 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) } IntArray(size / 4).also { copyInto(it, order = order) }
fun ByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) = fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray =
asUByteArray().toULong(startIndex, order) UIntArray(size / 4).also { copyInto(it, order = order) }
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) = fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
asUByteArray().toLong(startIndex, order) require(startIndex in 0..(size - 8))
fun ByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) = return when (order) {
ULongArray(size / 8).also { copyInto(it, order = 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) } LongArray(size / 8).also { copyInto(it, order = order) }
fun ByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN): ULongArray =
ULongArray(size / 8).also { copyInto(it, order = order) }

View File

@@ -2,15 +2,7 @@ package com.infendro.bytes.bytearray
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ubytearray.copyInto import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
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( fun ByteArray.copyInto(
destination: IntArray, destination: IntArray,
@@ -18,15 +10,39 @@ fun ByteArray.copyInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, 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( fun ByteArray.copyInto(
destination: ULongArray, destination: UIntArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) ) = copyInto(destination.asIntArray(), destinationOffset, startIndex, endIndex, order)
fun ByteArray.copyInto( fun ByteArray.copyInto(
destination: LongArray, destination: LongArray,
@@ -34,4 +50,40 @@ fun ByteArray.copyInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, 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)

View File

@@ -1,16 +1,57 @@
package com.infendro.bytes.bytearray 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( fun ByteArray.invInto(
destination: ByteArray, destination: ByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, 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() = 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( fun ByteArray.andInto(
that: ByteArray, that: ByteArray,
@@ -19,17 +60,10 @@ fun ByteArray.andInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, thatStartIndex: Int = 0,
) = asUByteArray().andInto( ) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a and b }
that.asUByteArray(),
destination.asUByteArray(),
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
)
infix fun ByteArray.and(that: ByteArray) = infix fun ByteArray.and(that: ByteArray) =
(this.asUByteArray() and that.asUByteArray()).asByteArray() combine(that) { a, b -> a and b }
fun ByteArray.orInto( fun ByteArray.orInto(
that: ByteArray, that: ByteArray,
@@ -38,17 +72,10 @@ fun ByteArray.orInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, thatStartIndex: Int = 0,
) = asUByteArray().orInto( ) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b }
that.asUByteArray(),
destination.asUByteArray(),
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
)
infix fun ByteArray.or(that: ByteArray) = infix fun ByteArray.or(that: ByteArray) =
(this.asUByteArray() or that.asUByteArray()).asByteArray() combine(that) { a, b -> a or b }
fun ByteArray.xorInto( fun ByteArray.xorInto(
that: ByteArray, that: ByteArray,
@@ -57,14 +84,7 @@ fun ByteArray.xorInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, thatStartIndex: Int = 0,
) = asUByteArray().xorInto( ) = combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b }
that.asUByteArray(),
destination.asUByteArray(),
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
)
infix fun ByteArray.xor(that: ByteArray) = infix fun ByteArray.xor(that: ByteArray) =
(this.asUByteArray() xor that.asUByteArray()).asByteArray() combine(that) { a, b -> a xor b }

View File

@@ -3,14 +3,8 @@ package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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) = fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(4).also { copyInto(it, order = order) } ByteArray(4).also { copyInto(it, order = order) }
fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 4).also { copyInto(it, order = order) } UByteArray(4).also { copyInto(it, order = order) }
fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 4).also { copyInto(it, order = order) }

View File

@@ -2,32 +2,33 @@ package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.uint.copyInto import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun Int.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toUInt().copyInto(destination, destinationOffset, order)
fun Int.copyInto( fun Int.copyInto(
destination: ByteArray, destination: ByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN, 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, destination: UByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) ) = copyInto(destination.asByteArray(), destinationOffset, 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)

View File

@@ -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) }

View 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)

View File

@@ -3,14 +3,8 @@ package com.infendro.bytes.long
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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) = fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(8).also { copyInto(it, order = order) } ByteArray(8).also { copyInto(it, order = order) }
fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 8).also { copyInto(it, order = order) } UByteArray(8).also { copyInto(it, order = order) }
fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 8).also { copyInto(it, order = order) }

View File

@@ -2,32 +2,41 @@ package com.infendro.bytes.long
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ulong.copyInto import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun Long.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toULong().copyInto(destination, destinationOffset, order)
fun Long.copyInto( fun Long.copyInto(
destination: ByteArray, destination: ByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN, 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, destination: UByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) = asULongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order) ) = copyInto(destination.asByteArray(), destinationOffset, 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)

View File

@@ -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) }

View 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)

View File

@@ -2,56 +2,31 @@ package com.infendro.bytes.ubytearray
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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 { fun UByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int =
require(startIndex in 0..(size - 4)) asByteArray().toInt(startIndex, order)
return when (order) { fun UByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt =
BIG_ENDIAN -> { asByteArray().toUInt(startIndex, order)
(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) = fun UByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN): IntArray =
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) } IntArray(size / 4).also { copyInto(it, order = order) }
fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong { fun UByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray =
require(startIndex in 0..(size - 8)) UIntArray(size / 4).also { copyInto(it, order = order) }
return when (order) { fun UByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long =
BIG_ENDIAN -> { asByteArray().toLong(startIndex, order)
(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) = fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong =
toULong(startIndex, order).toLong() asByteArray().toULong(startIndex, order)
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) = fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN): LongArray =
ULongArray(size / 8).also { copyInto(it, order = order) }
fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
LongArray(size / 8).also { copyInto(it, order = order) } LongArray(size / 8).also { copyInto(it, order = order) }
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN): ULongArray =
ULongArray(size / 8).also { copyInto(it, order = order) }

View File

@@ -2,39 +2,7 @@ package com.infendro.bytes.ubytearray
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN import com.infendro.bytes.bytearray.copyInto
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)
}
}
}
}
fun UByteArray.copyInto( fun UByteArray.copyInto(
destination: IntArray, destination: IntArray,
@@ -42,7 +10,15 @@ fun UByteArray.copyInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, 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( fun UByteArray.copyInto(
destination: ULongArray, destination: ULongArray,
@@ -50,35 +26,7 @@ fun UByteArray.copyInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) { ) = asByteArray().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].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)
}
}
}
}
fun UByteArray.copyInto( fun UByteArray.copyInto(
destination: LongArray, destination: LongArray,
@@ -86,4 +34,4 @@ fun UByteArray.copyInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asULongArray(), destinationOffset, startIndex, endIndex, order) ) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)

View File

@@ -1,51 +1,19 @@
package com.infendro.bytes.ubytearray 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( fun UByteArray.invInto(
destination: UByteArray, destination: UByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
) { ) = asByteArray().invInto(destination.asByteArray(), 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 -> fun UByteArray.inv() =
destination[destinationOffset + i] = this[startIndex + i].inv() UByteArray(size).also { invInto(it) }
}
}
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( fun UByteArray.andInto(
that: UByteArray, that: UByteArray,
@@ -54,17 +22,17 @@ fun UByteArray.andInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, thatStartIndex: Int = 0,
) = combineInto( ) = asByteArray().andInto(
that, that.asByteArray(),
destination, destination.asByteArray(),
destinationOffset, destinationOffset,
startIndex, startIndex,
endIndex, endIndex,
thatStartIndex, thatStartIndex,
) { a, b -> a and b } )
infix fun UByteArray.and(that: UByteArray) = infix fun UByteArray.and(that: UByteArray) =
combine(that) { a, b -> a and b } UByteArray(size).also { andInto(that, it) }
fun UByteArray.orInto( fun UByteArray.orInto(
that: UByteArray, that: UByteArray,
@@ -73,17 +41,17 @@ fun UByteArray.orInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, thatStartIndex: Int = 0,
) = combineInto( ) = asByteArray().orInto(
that, that.asByteArray(),
destination, destination.asByteArray(),
destinationOffset, destinationOffset,
startIndex, startIndex,
endIndex, endIndex,
thatStartIndex, thatStartIndex,
) { a, b -> a or b } )
infix fun UByteArray.or(that: UByteArray) = infix fun UByteArray.or(that: UByteArray) =
combine(that) { a, b -> a or b } UByteArray(size).also { orInto(that, it) }
fun UByteArray.xorInto( fun UByteArray.xorInto(
that: UByteArray, that: UByteArray,
@@ -92,14 +60,14 @@ fun UByteArray.xorInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, thatStartIndex: Int = 0,
) = combineInto( ) = asByteArray().xorInto(
that, that.asByteArray(),
destination, destination.asByteArray(),
destinationOffset, destinationOffset,
startIndex, startIndex,
endIndex, endIndex,
thatStartIndex, thatStartIndex,
) { a, b -> a xor b } )
infix fun UByteArray.xor(that: UByteArray) = infix fun UByteArray.xor(that: UByteArray) =
combine(that) { a, b -> a xor b } UByteArray(size).also { xorInto(that, it) }

View File

@@ -3,14 +3,8 @@ package com.infendro.bytes.uint
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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) = fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(4).also { copyInto(it, order = order) } ByteArray(4).also { copyInto(it, order = order) }
fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 4).also { copyInto(it, order = order) } UByteArray(4).also { copyInto(it, order = order) }
fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 4).also { copyInto(it, order = order) }

View File

@@ -2,77 +2,16 @@ package com.infendro.bytes.uint
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN import com.infendro.bytes.int.copyInto
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( fun UInt.copyInto(
destination: ByteArray, destination: ByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, order) ) = toInt().copyInto(destination, destinationOffset, order)
fun UIntArray.copyInto( fun UInt.copyInto(
destination: UByteArray, destination: UByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) { ) = toInt().copyInto(destination, destinationOffset, order)
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)

View File

@@ -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) }

View 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)

View File

@@ -3,14 +3,8 @@ package com.infendro.bytes.ulong
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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) = fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(8).also { copyInto(it, order = order) } ByteArray(8).also { copyInto(it, order = order) }
fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) = fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 8).also { copyInto(it, order = order) } UByteArray(8).also { copyInto(it, order = order) }
fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 8).also { copyInto(it, order = order) }

View File

@@ -2,93 +2,16 @@ package com.infendro.bytes.ulong
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN import com.infendro.bytes.long.copyInto
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( fun ULong.copyInto(
destination: ByteArray, destination: ByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, order) ) = toLong().copyInto(destination, destinationOffset, order)
fun ULongArray.copyInto( fun ULong.copyInto(
destination: UByteArray, destination: UByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
) { ) = toLong().copyInto(destination, destinationOffset, order)
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)

View File

@@ -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) }

View 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)

View 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])
}
}

View File

@@ -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)
}
}

View 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])
}
}
}

View File

@@ -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])
}
}
}

View 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])
}
}
}

View File

@@ -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])
}
}
}

View File

@@ -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])
}
}

View File

@@ -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)
}
}

View File

@@ -4,7 +4,7 @@ import kotlin.test.Test
import kotlin.test.assertContentEquals import kotlin.test.assertContentEquals
import kotlin.test.assertEquals import kotlin.test.assertEquals
class `UByteArray String Test` { class UByteArrayStringTest {
@Test @Test
fun encodeToUByteArray() { fun encodeToUByteArray() {
val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U) val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U)

View File

@@ -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])
}
}
}

View File

@@ -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])
}
}
}