refactor and simplify functions
This commit is contained in:
@@ -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(
|
||||||
|
|||||||
@@ -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())
|
|
||||||
@@ -15,11 +15,11 @@ fun Int.toByteArray(
|
|||||||
fun IntArray.toByteArray(
|
fun IntArray.toByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<Byte>()
|
||||||
for (n in this@toByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toByteArray(order))
|
bytes += n.toByteArray(order).toList()
|
||||||
}
|
}
|
||||||
}.toByteArray()
|
return bytes.toByteArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Int.toUByteArray(
|
fun Int.toUByteArray(
|
||||||
@@ -35,9 +35,9 @@ fun Int.toUByteArray(
|
|||||||
fun IntArray.toUByteArray(
|
fun IntArray.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UByteArray {
|
): UByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<UByte>()
|
||||||
for (n in this@toUByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toUByteArray(order))
|
bytes += n.toUByteArray(order)
|
||||||
}
|
}
|
||||||
}.toUByteArray()
|
return bytes.toUByteArray()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ fun Long.toByteArray(
|
|||||||
fun LongArray.toByteArray(
|
fun LongArray.toByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<Byte>()
|
||||||
for (n in this@toByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toByteArray(order))
|
bytes += n.toByteArray(order).toList()
|
||||||
}
|
}
|
||||||
}.toByteArray()
|
return bytes.toByteArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Long.toUByteArray(
|
fun Long.toUByteArray(
|
||||||
@@ -35,9 +35,9 @@ fun Long.toUByteArray(
|
|||||||
fun LongArray.toUByteArray(
|
fun LongArray.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UByteArray {
|
): UByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<UByte>()
|
||||||
for (n in this@toUByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toUByteArray(order))
|
bytes += n.toUByteArray(order)
|
||||||
}
|
}
|
||||||
}.toUByteArray()
|
return bytes.toUByteArray()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,8 +72,7 @@ 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) { it.toUByteArray() }
|
|
||||||
.map { it.toInt(order) }
|
.map { it.toInt(order) }
|
||||||
.toIntArray()
|
.toIntArray()
|
||||||
}
|
}
|
||||||
@@ -94,8 +93,7 @@ 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) { it.toUByteArray() }
|
|
||||||
.map { it.toLong(order) }
|
.map { it.toLong(order) }
|
||||||
.toLongArray()
|
.toLongArray()
|
||||||
}
|
}
|
||||||
@@ -116,8 +114,7 @@ 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) { it.toUByteArray() }
|
|
||||||
.map { it.toUInt(order) }
|
.map { it.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) { it.toUByteArray() }
|
|
||||||
.map { it.toULong(order) }
|
.map { it.toULong(order) }
|
||||||
.toULongArray()
|
.toULongArray()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ fun UInt.toByteArray(
|
|||||||
fun UIntArray.toByteArray(
|
fun UIntArray.toByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<Byte>()
|
||||||
for (n in this@toByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toByteArray(order))
|
bytes += n.toByteArray(order).toList()
|
||||||
}
|
}
|
||||||
}.toByteArray()
|
return bytes.toByteArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun UInt.toUByteArray(
|
fun UInt.toUByteArray(
|
||||||
@@ -35,9 +35,9 @@ fun UInt.toUByteArray(
|
|||||||
fun UIntArray.toUByteArray(
|
fun UIntArray.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UByteArray {
|
): UByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<UByte>()
|
||||||
for (n in this@toUByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toUByteArray(order))
|
bytes += n.toUByteArray(order)
|
||||||
}
|
}
|
||||||
}.toUByteArray()
|
return bytes.toUByteArray()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ fun ULong.toByteArray(
|
|||||||
fun ULongArray.toByteArray(
|
fun ULongArray.toByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<Byte>()
|
||||||
for (n in this@toByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toByteArray(order))
|
bytes += n.toByteArray(order).toList()
|
||||||
}
|
}
|
||||||
}.toByteArray()
|
return bytes.toByteArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ULong.toUByteArray(
|
fun ULong.toUByteArray(
|
||||||
@@ -35,9 +35,9 @@ fun ULong.toUByteArray(
|
|||||||
fun ULongArray.toUByteArray(
|
fun ULongArray.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UByteArray {
|
): UByteArray {
|
||||||
return buildList {
|
val bytes = mutableListOf<UByte>()
|
||||||
for (n in this@toUByteArray) {
|
for (n in this) {
|
||||||
addAll(n.toUByteArray(order))
|
bytes += n.toUByteArray(order)
|
||||||
}
|
}
|
||||||
}.toUByteArray()
|
return bytes.toUByteArray()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user