refactor lib structure

This commit is contained in:
2025-12-13 14:49:00 +01:00
parent 3ab2bea4f6
commit f5d559e8fe
28 changed files with 1017 additions and 1301 deletions

View File

@@ -1,159 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
infix fun ByteArray.and(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun ByteArray.or(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun ByteArray.xor(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] xor that[i]
}
}
fun ByteArray.padStart(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
repeat(length - size) {
bytes += padByte
}
bytes += this.toList()
return bytes.toByteArray()
}
fun ByteArray.padEnd(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
bytes += this.toList()
repeat(length - size) {
bytes += padByte
}
return bytes.toByteArray()
}
fun ByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0) { acc, byte ->
(acc shl 8) or (byte.toInt() and 0xFF)
}
}
fun ByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return toList()
.chunked(4) { it.toByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
fun ByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0L) { acc, byte ->
(acc shl 8) or (byte.toLong() and 0xFFL)
}
}
fun ByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return toList()
.chunked(8) { it.toByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
}
}
fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return toList()
.chunked(4) { it.toByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0UL) { acc, byte ->
(acc shl 8) or (byte.toULong() and 0xFFUL)
}
}
fun ByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return toList()
.chunked(8) { it.toByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -16,7 +16,4 @@ fun ByteArray.normalize(
fun UByteArray.normalize(
order: ByteOrder,
): UByteArray = when (order) {
BIG_ENDIAN -> this
LITTLE_ENDIAN -> reversedArray()
}
): UByteArray = asByteArray().normalize(order).asUByteArray()

View File

@@ -0,0 +1,181 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun ByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0) { acc, byte -> (acc shl 8) or (byte.toInt() and 0xFF) }
}
fun UByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int = asByteArray().toInt(order)
fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt = toInt(order).toUInt()
fun UByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt = asByteArray().toUInt(order)
fun ByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return asList()
.chunked(4) { it.toByteArray().toInt(order) }
.toIntArray()
}
fun UByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray = asByteArray().toIntArray(order)
fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray = toIntArray(order).asUIntArray()
fun UByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray = asByteArray().toUIntArray(order)
fun Int.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = toInt().toByteArray(order)
fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toInt().toUByteArray(order)
fun IntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).toList()
}
return bytes.toByteArray()
}
fun UIntArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = asIntArray().toByteArray(order)
fun IntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun UIntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = asIntArray().toUByteArray(order)
fun ByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0L) { acc, byte -> (acc shl 8) or (byte.toLong() and 0xFFL) }
}
fun UByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long = asByteArray().toLong(order)
fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong = toLong(order).toULong()
fun UByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong = asByteArray().toULong(order)
fun ByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return asList()
.chunked(8) { it.toByteArray().toLong(order) }
.toLongArray()
}
fun UByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray = asByteArray().toLongArray(order)
fun ByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray = toLongArray(order).asULongArray()
fun UByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray = asByteArray().toULongArray(order)
fun Long.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun ULong.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = toLong().toByteArray(order)
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toLong().toUByteArray(order)
fun LongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = mutableListOf<Byte>()
for (n in this) {
bytes += n.toByteArray(order).asList()
}
return bytes.toByteArray()
}
fun ULongArray.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray = asLongArray().toByteArray(order)
fun LongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = toByteArray(order).asUByteArray()
fun ULongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray = asLongArray().toUByteArray(order)

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun Int.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
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 IntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun Long.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toByte()
}
return bytes.normalize(order)
}
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
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 LongArray.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,35 @@
package com.infendro.bytearray
fun ByteArray.padStart(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
repeat(length - size) {
bytes += padByte
}
bytes += this.asList()
return bytes.toByteArray()
}
fun UByteArray.padStart(
length: Int,
padByte: UByte,
): UByteArray = asByteArray().padStart(length, padByte.toByte()).asUByteArray()
fun ByteArray.padEnd(
length: Int,
padByte: Byte,
): ByteArray {
val bytes = mutableListOf<Byte>()
bytes += this.asList()
repeat(length - size) {
bytes += padByte
}
return bytes.toByteArray()
}
fun UByteArray.padEnd(
length: Int,
padByte: UByte,
): UByteArray = asByteArray().padEnd(length, padByte.toByte()).asUByteArray()

View File

@@ -0,0 +1,50 @@
package com.infendro.bytearray
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
infix fun ByteArray.and(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun UByteArray.and(
that: UByteArray,
): UByteArray = (this.asByteArray() and that.asByteArray()).asUByteArray()
infix fun ByteArray.or(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun UByteArray.or(
that: UByteArray,
): UByteArray = (this.asByteArray() or that.asByteArray()).asUByteArray()
infix fun ByteArray.xor(
that: ByteArray,
): ByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return ByteArray(size) { i ->
this[i] xor that[i]
}
}
infix fun UByteArray.xor(
that: UByteArray,
): UByteArray = (this.asByteArray() xor that.asByteArray()).asUByteArray()

View File

@@ -1,7 +1,7 @@
package com.infendro.bytearray
fun String.encodeToUByteArray(): UByteArray =
encodeToByteArray().toUByteArray()
encodeToByteArray().asUByteArray()
fun UByteArray.decodeToString(): String =
toByteArray().decodeToString()
asByteArray().decodeToString()

View File

@@ -1,152 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
infix fun UByteArray.and(
that: UByteArray,
): UByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return UByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun UByteArray.or(
that: UByteArray,
): UByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return UByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun UByteArray.xor(
that: UByteArray,
): UByteArray {
if (this.size != that.size)
throw IllegalArgumentException()
return UByteArray(size) { i ->
this[i] xor that[i]
}
}
fun UByteArray.padStart(
length: Int,
padByte: UByte,
): UByteArray {
val bytes = mutableListOf<UByte>()
repeat(length - size) {
bytes += padByte
}
bytes += this
return bytes.toUByteArray()
}
fun UByteArray.padEnd(
length: Int,
padByte: UByte,
): UByteArray {
val bytes = mutableListOf<UByte>()
bytes += this
repeat(length - size) {
bytes += padByte
}
return bytes.toUByteArray()
}
fun UByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0) { acc, byte ->
(acc shl 8) or (byte.toInt() and 0xFF)
}
}
fun UByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return chunked(4) { it.toUByteArray() }
.map { it.toInt(order) }
.toIntArray()
}
fun UByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0L) { acc, byte ->
(acc shl 8) or (byte.toLong() and 0xFFL)
}
}
fun UByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return chunked(8) { it.toUByteArray() }
.map { it.toLong(order) }
.toLongArray()
}
fun UByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4)
throw IllegalArgumentException()
return normalize(order)
.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
}
}
fun UByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0)
throw IllegalArgumentException()
return chunked(4) { it.toUByteArray() }
.map { it.toUInt(order) }
.toUIntArray()
}
fun UByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8)
throw IllegalArgumentException()
return normalize(order)
.fold(0UL) { acc, byte ->
(acc shl 8) or (byte.toULong() and 0xFFUL)
}
}
fun UByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
if (size % 8 != 0)
throw IllegalArgumentException()
return chunked(8) { it.toUByteArray() }
.map { it.toULong(order) }
.toULongArray()
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toByte()
}
return bytes.normalize(order)
}
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
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 UIntArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun ULong.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toByte()
}
return bytes.normalize(order)
}
fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toUByte()
}
return bytes.normalize(order)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
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 ULongArray.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = mutableListOf<UByte>()
for (n in this) {
bytes += n.toUByteArray(order)
}
return bytes.toUByteArray()
}