Compare commits

..

5 Commits

Author SHA1 Message Date
1e04a3aff4 bump version to 1.2.3
All checks were successful
/ publish (push) Successful in 4m7s
2025-12-01 17:53:42 +01:00
401d169cc0 upgrade kotlin version 2025-12-01 17:53:06 +01:00
fa2c6b6621 refactor and simplify functions 2025-12-01 17:48:45 +01:00
4fbadfba1e implement tests 2025-12-01 16:24:58 +01:00
eb2cdbea73 implement typed array to byte array conversions 2025-12-01 16:24:35 +01:00
15 changed files with 429 additions and 55 deletions

View File

@@ -1,5 +1,5 @@
group = "com.infendro"
version = "1.2.2"
version = "1.2.3"
repositories {
mavenCentral()
@@ -17,6 +17,12 @@ kotlin {
}
linuxX64()
sourceSets {
commonTest.dependencies {
implementation(libs.test)
}
}
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}

View File

@@ -1,5 +1,8 @@
[versions]
kotlin = "2.1.20"
kotlin = "2.2.21"
[plugins]
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
[libraries]
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }

View File

@@ -39,24 +39,24 @@ fun ByteArray.padStart(
length: Int,
padByte: Byte,
): ByteArray {
return buildList {
repeat(length - this@padStart.size) {
add(padByte)
val bytes = mutableListOf<Byte>()
repeat(length - size) {
bytes += padByte
}
addAll(this@padStart)
}.toByteArray()
bytes += this.toList()
return bytes.toByteArray()
}
fun ByteArray.padEnd(
length: Int,
padByte: Byte,
): ByteArray {
return buildList {
addAll(this@padEnd)
repeat(length - this@padEnd.size) {
add(padByte)
val bytes = mutableListOf<Byte>()
bytes += this.toList()
repeat(length - size) {
bytes += padByte
}
}.toByteArray()
return bytes.toByteArray()
}
fun ByteArray.toInt(
@@ -76,8 +76,8 @@ fun ByteArray.toIntArray(
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toByteArray().toInt(order) }
.chunked(4) { it.toByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
@@ -98,8 +98,8 @@ fun ByteArray.toLongArray(
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toByteArray().toLong(order) }
.chunked(8) { it.toByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
@@ -120,8 +120,8 @@ fun ByteArray.toUIntArray(
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toByteArray().toUInt(order) }
.chunked(4) { it.toByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
@@ -142,7 +142,7 @@ fun ByteArray.toULongArray(
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toByteArray().toULong(order) }
.chunked(8) { it.toByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -1,9 +0,0 @@
package com.infendro.bytearray
fun MutableCollection<Byte>.addAll(
bytes: ByteArray,
) = addAll(bytes.toList())
fun MutableCollection<UByte>.addAll(
bytes: UByteArray,
) = addAll(bytes.toList())

View File

@@ -12,6 +12,16 @@ fun Int.toByteArray(
return order.normalize(bytes)
}
fun IntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
@@ -21,3 +31,13 @@ fun Int.toUByteArray(
}
return order.normalize(bytes)
}
fun IntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -12,6 +12,16 @@ fun Long.toByteArray(
return order.normalize(bytes)
}
fun LongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
@@ -21,3 +31,13 @@ fun Long.toUByteArray(
}
return order.normalize(bytes)
}
fun LongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -36,24 +36,24 @@ fun UByteArray.padStart(
length: Int,
padByte: UByte,
): UByteArray {
return buildList {
repeat(length - this@padStart.size) {
add(padByte)
val bytes = mutableListOf<UByte>()
repeat(length - size) {
bytes += padByte
}
addAll(this@padStart)
}.toUByteArray()
bytes += this
return bytes.toUByteArray()
}
fun UByteArray.padEnd(
length: Int,
padByte: UByte,
): UByteArray {
return buildList {
addAll(this@padEnd)
repeat(length - this@padEnd.size) {
add(padByte)
val bytes = mutableListOf<UByte>()
bytes += this
repeat(length - size) {
bytes += padByte
}
}.toUByteArray()
return bytes.toUByteArray()
}
fun UByteArray.toInt(
@@ -72,9 +72,8 @@ fun UByteArray.toIntArray(
): IntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toUByteArray().toInt(order) }
return chunked(4) { it.toUByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
@@ -94,9 +93,8 @@ fun UByteArray.toLongArray(
): LongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toUByteArray().toLong(order) }
return chunked(8) { it.toUByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
@@ -116,9 +114,8 @@ fun UByteArray.toUIntArray(
): UIntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toUByteArray().toUInt(order) }
return chunked(4) { it.toUByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
@@ -138,8 +135,7 @@ fun UByteArray.toULongArray(
): ULongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toUByteArray().toULong(order) }
return chunked(8) { it.toUByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -12,6 +12,16 @@ fun UInt.toByteArray(
return order.normalize(bytes)
}
fun UIntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
@@ -21,3 +31,13 @@ fun UInt.toUByteArray(
}
return order.normalize(bytes)
}
fun UIntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -12,6 +12,16 @@ fun ULong.toByteArray(
return order.normalize(bytes)
}
fun ULongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
@@ -21,3 +31,13 @@ fun ULong.toUByteArray(
}
return order.normalize(bytes)
}
fun ULongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -0,0 +1,87 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFails
class `ByteArray Test` {
@Test
fun `and() - size`() {
assertFails {
byteArrayOf() and byteArrayOf(0x00)
}
assertFails {
byteArrayOf(0x00) and byteArrayOf()
}
}
@Test
fun `padStart()`() {
val padded = byteArrayOf(0x02, 0x02).padStart(8, 0x01)
assertEquals(8, padded.size)
assertContentEquals(
byteArrayOf(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02),
padded
)
}
@Test
fun `padEnd()`() {
val padded = byteArrayOf(0x02, 0x02).padEnd(8, 0x01)
assertEquals(8, padded.size)
assertContentEquals(
byteArrayOf(0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01),
padded
)
}
@Test
fun `toInt()`() {
assertEquals(
0x12345678,
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt()
)
assertEquals(
0x12345678,
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN)
)
}
@Test
fun `toLong()`() {
assertEquals(
0x1234567812345678L,
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toLong()
)
assertEquals(
0x1234567812345678L,
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN)
)
}
@Test
fun `toUInt()`() {
assertEquals(
0x12345678U,
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt()
)
assertEquals(
0x12345678U,
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN)
)
}
@Test
fun `toULong()`() {
assertEquals(
0x1234567812345678UL,
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toULong()
)
assertEquals(
0x1234567812345678UL,
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN)
)
}
}

View File

@@ -0,0 +1,31 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Int Test` {
@Test
fun `toByteArray()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
0x12345678.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12),
0x12345678.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
0x12345678.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
0x12345678.toUByteArray(LITTLE_ENDIAN)
)
}
}

View File

@@ -0,0 +1,31 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `Long Test` {
@Test
fun `toByteArray()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
0x1234567812345678L.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
0x1234567812345678L.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
0x1234567812345678L.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
0x1234567812345678L.toUByteArray(LITTLE_ENDIAN)
)
}
}

View File

@@ -0,0 +1,87 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFails
class `UByteArray Test` {
@Test
fun `and() - size`() {
assertFails {
ubyteArrayOf() and ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) and ubyteArrayOf()
}
}
@Test
fun `padStart()`() {
val padded = ubyteArrayOf(0x02U, 0x02U).padStart(8, 0x01U)
assertEquals(8, padded.size)
assertContentEquals(
ubyteArrayOf(0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x02U, 0x02U),
padded
)
}
@Test
fun `padEnd()`() {
val padded = ubyteArrayOf(0x02U, 0x02U).padEnd(8, 0x01U)
assertEquals(8, padded.size)
assertContentEquals(
ubyteArrayOf(0x02U, 0x02U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U),
padded
)
}
@Test
fun `toInt()`() {
assertEquals(
0x12345678,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt()
)
assertEquals(
0x12345678,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN)
)
}
@Test
fun `toLong()`() {
assertEquals(
0x1234567812345678L,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong()
)
assertEquals(
0x1234567812345678L,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN)
)
}
@Test
fun `toUInt()`() {
assertEquals(
0x12345678U,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt()
)
assertEquals(
0x12345678U,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN)
)
}
@Test
fun `toULong()`() {
assertEquals(
0x1234567812345678UL,
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong()
)
assertEquals(
0x1234567812345678UL,
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(LITTLE_ENDIAN)
)
}
}

View File

@@ -0,0 +1,31 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `UInt Test` {
@Test
fun `toByteArray()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
0x12345678U.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12),
0x12345678U.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
0x12345678U.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
0x12345678U.toUByteArray(LITTLE_ENDIAN)
)
}
}

View File

@@ -0,0 +1,31 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `ULong Test` {
@Test
fun `toByteArray()`() {
assertContentEquals(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
0x1234567812345678UL.toByteArray()
)
assertContentEquals(
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
0x1234567812345678UL.toByteArray(LITTLE_ENDIAN)
)
}
@Test
fun `toUByteArray()`() {
assertContentEquals(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
0x1234567812345678UL.toUByteArray()
)
assertContentEquals(
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
0x1234567812345678UL.toUByteArray(LITTLE_ENDIAN)
)
}
}