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

View File

@@ -1,5 +1,8 @@
[versions] [versions]
kotlin = "2.1.20" kotlin = "2.2.21"
[plugins] [plugins]
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } 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, length: Int,
padByte: Byte, padByte: Byte,
): ByteArray { ): ByteArray {
return buildList { val bytes = mutableListOf<Byte>()
repeat(length - this@padStart.size) { repeat(length - size) {
add(padByte) bytes += padByte
} }
addAll(this@padStart) bytes += this.toList()
}.toByteArray() return bytes.toByteArray()
} }
fun ByteArray.padEnd( fun ByteArray.padEnd(
length: Int, length: Int,
padByte: Byte, padByte: Byte,
): ByteArray { ): ByteArray {
return buildList { val bytes = mutableListOf<Byte>()
addAll(this@padEnd) bytes += this.toList()
repeat(length - this@padEnd.size) { repeat(length - size) {
add(padByte) bytes += padByte
} }
}.toByteArray() return bytes.toByteArray()
} }
fun ByteArray.toInt( fun ByteArray.toInt(
@@ -76,8 +76,8 @@ fun ByteArray.toIntArray(
if (size % 4 != 0) throw Exception() if (size % 4 != 0) throw Exception()
return toList() return toList()
.chunked(4) .chunked(4) { it.toByteArray() }
.map { it.toByteArray().toInt(order) } .map { it.toInt(order) }
.toIntArray() .toIntArray()
} }
@@ -98,8 +98,8 @@ fun ByteArray.toLongArray(
if (size % 8 != 0) throw Exception() if (size % 8 != 0) throw Exception()
return toList() return toList()
.chunked(8) .chunked(8) { it.toByteArray() }
.map { it.toByteArray().toLong(order) } .map { it.toLong(order) }
.toLongArray() .toLongArray()
} }
@@ -120,8 +120,8 @@ fun ByteArray.toUIntArray(
if (size % 4 != 0) throw Exception() if (size % 4 != 0) throw Exception()
return toList() return toList()
.chunked(4) .chunked(4) { it.toByteArray() }
.map { it.toByteArray().toUInt(order) } .map { it.toUInt(order) }
.toUIntArray() .toUIntArray()
} }
@@ -142,7 +142,7 @@ fun ByteArray.toULongArray(
if (size % 8 != 0) throw Exception() if (size % 8 != 0) throw Exception()
return toList() return toList()
.chunked(8) .chunked(8) { it.toByteArray() }
.map { it.toByteArray().toULong(order) } .map { it.toULong(order) }
.toULongArray() .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) 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( fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
): UByteArray { ): UByteArray {
@@ -21,3 +31,13 @@ fun Int.toUByteArray(
} }
return order.normalize(bytes) 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) 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( fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
): UByteArray { ): UByteArray {
@@ -21,3 +31,13 @@ fun Long.toUByteArray(
} }
return order.normalize(bytes) 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, length: Int,
padByte: UByte, padByte: UByte,
): UByteArray { ): UByteArray {
return buildList { val bytes = mutableListOf<UByte>()
repeat(length - this@padStart.size) { repeat(length - size) {
add(padByte) bytes += padByte
} }
addAll(this@padStart) bytes += this
}.toUByteArray() return bytes.toUByteArray()
} }
fun UByteArray.padEnd( fun UByteArray.padEnd(
length: Int, length: Int,
padByte: UByte, padByte: UByte,
): UByteArray { ): UByteArray {
return buildList { val bytes = mutableListOf<UByte>()
addAll(this@padEnd) bytes += this
repeat(length - this@padEnd.size) { repeat(length - size) {
add(padByte) bytes += padByte
} }
}.toUByteArray() return bytes.toUByteArray()
} }
fun UByteArray.toInt( fun UByteArray.toInt(
@@ -72,9 +72,8 @@ fun UByteArray.toIntArray(
): IntArray { ): IntArray {
if (size % 4 != 0) throw Exception() if (size % 4 != 0) throw Exception()
return toList() return chunked(4) { it.toUByteArray() }
.chunked(4) .map { it.toInt(order) }
.map { it.toUByteArray().toInt(order) }
.toIntArray() .toIntArray()
} }
@@ -94,9 +93,8 @@ fun UByteArray.toLongArray(
): LongArray { ): LongArray {
if (size % 8 != 0) throw Exception() if (size % 8 != 0) throw Exception()
return toList() return chunked(8) { it.toUByteArray() }
.chunked(8) .map { it.toLong(order) }
.map { it.toUByteArray().toLong(order) }
.toLongArray() .toLongArray()
} }
@@ -116,9 +114,8 @@ fun UByteArray.toUIntArray(
): UIntArray { ): UIntArray {
if (size % 4 != 0) throw Exception() if (size % 4 != 0) throw Exception()
return toList() return chunked(4) { it.toUByteArray() }
.chunked(4) .map { it.toUInt(order) }
.map { it.toUByteArray().toUInt(order) }
.toUIntArray() .toUIntArray()
} }
@@ -138,8 +135,7 @@ fun UByteArray.toULongArray(
): ULongArray { ): ULongArray {
if (size % 8 != 0) throw Exception() if (size % 8 != 0) throw Exception()
return toList() return chunked(8) { it.toUByteArray() }
.chunked(8) .map { it.toULong(order) }
.map { it.toUByteArray().toULong(order) }
.toULongArray() .toULongArray()
} }

View File

@@ -12,6 +12,16 @@ fun UInt.toByteArray(
return order.normalize(bytes) 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( fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
): UByteArray { ): UByteArray {
@@ -21,3 +31,13 @@ fun UInt.toUByteArray(
} }
return order.normalize(bytes) 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) 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( fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN, order: ByteOrder = BIG_ENDIAN,
): UByteArray { ): UByteArray {
@@ -21,3 +31,13 @@ fun ULong.toUByteArray(
} }
return order.normalize(bytes) 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)
)
}
}