Compare commits
7 Commits
3801c5759f
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
3127b6c066
|
|||
|
e8baac9ba3
|
|||
|
d3a67c622c
|
|||
|
c80040466b
|
|||
|
20c2ba4c24
|
|||
|
47666a1e1a
|
|||
|
0129039df9
|
@@ -1,6 +1,6 @@
|
||||
# bytes-kt
|
||||
# bytes.kt
|
||||
|
||||
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
|
||||
`bytes.kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -12,6 +12,6 @@ repositories {
|
||||
}
|
||||
|
||||
commonMain.dependencies {
|
||||
implementation("com.infendro:bytes:1.4.0")
|
||||
implementation("com.infendro:bytes:1.4.2")
|
||||
}
|
||||
```
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.infendro.plugin.token
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
|
||||
group = "com.infendro"
|
||||
version = "1.4.0"
|
||||
version = "1.4.2"
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.infendro)
|
||||
|
||||
@@ -50,6 +50,42 @@ fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
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].toInt() and 0xFF) shl 24) or
|
||||
((this[offset + 1].toInt() and 0xFF) shl 16) or
|
||||
((this[offset + 2].toInt() and 0xFF) shl 8) or
|
||||
(this[offset + 3].toInt() and 0xFF)
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toInt() and 0xFF) or
|
||||
((this[offset + 1].toInt() and 0xFF) shl 8) or
|
||||
((this[offset + 2].toInt() and 0xFF) shl 16) or
|
||||
((this[offset + 3].toInt() and 0xFF) shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray {
|
||||
val length = endIndex - startIndex
|
||||
val result = IntArray(length / 4)
|
||||
@@ -57,6 +93,50 @@ fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteO
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
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() and 0xFFL) shl 56) or
|
||||
((this[offset + 1].toLong() and 0xFFL) shl 48) or
|
||||
((this[offset + 2].toLong() and 0xFFL) shl 40) or
|
||||
((this[offset + 3].toLong() and 0xFFL) shl 32) or
|
||||
((this[offset + 4].toLong() and 0xFFL) shl 24) or
|
||||
((this[offset + 5].toLong() and 0xFFL) shl 16) or
|
||||
((this[offset + 6].toLong() and 0xFFL) shl 8) or
|
||||
(this[offset + 7].toLong() and 0xFFL)
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toLong() and 0xFFL) or
|
||||
((this[offset + 1].toLong() and 0xFFL) shl 8) or
|
||||
((this[offset + 2].toLong() and 0xFFL) shl 16) or
|
||||
((this[offset + 3].toLong() and 0xFFL) shl 24) or
|
||||
((this[offset + 4].toLong() and 0xFFL) shl 32) or
|
||||
((this[offset + 5].toLong() and 0xFFL) shl 40) or
|
||||
((this[offset + 6].toLong() and 0xFFL) shl 48) or
|
||||
((this[offset + 7].toLong() and 0xFFL) shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toLongArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): LongArray {
|
||||
val length = endIndex - startIndex
|
||||
val result = LongArray(length / 8)
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.infendro.bytes.bytearray
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: IntArray,
|
||||
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].toInt() and 0xFF) shl 24) or
|
||||
((this[offset + 1].toInt() and 0xFF) shl 16) or
|
||||
((this[offset + 2].toInt() and 0xFF) shl 8) or
|
||||
(this[offset + 3].toInt() and 0xFF)
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 4) { i ->
|
||||
val offset = startIndex + (i * 4)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toInt() and 0xFF) or
|
||||
((this[offset + 1].toInt() and 0xFF) shl 8) or
|
||||
((this[offset + 2].toInt() and 0xFF) shl 16) or
|
||||
((this[offset + 3].toInt() and 0xFF) shl 24)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.copyInto(
|
||||
destination: LongArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
) {
|
||||
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() and 0xFFL) shl 56) or
|
||||
((this[offset + 1].toLong() and 0xFFL) shl 48) or
|
||||
((this[offset + 2].toLong() and 0xFFL) shl 40) or
|
||||
((this[offset + 3].toLong() and 0xFFL) shl 32) or
|
||||
((this[offset + 4].toLong() and 0xFFL) shl 24) or
|
||||
((this[offset + 5].toLong() and 0xFFL) shl 16) or
|
||||
((this[offset + 6].toLong() and 0xFFL) shl 8) or
|
||||
(this[offset + 7].toLong() and 0xFFL)
|
||||
}
|
||||
}
|
||||
LITTLE_ENDIAN -> {
|
||||
repeat(length / 8) { i ->
|
||||
val offset = startIndex + (i * 8)
|
||||
destination[destinationOffset + i] =
|
||||
(this[offset].toLong() and 0xFFL) or
|
||||
((this[offset + 1].toLong() and 0xFFL) shl 8) or
|
||||
((this[offset + 2].toLong() and 0xFFL) shl 16) or
|
||||
((this[offset + 3].toLong() and 0xFFL) shl 24) or
|
||||
((this[offset + 4].toLong() and 0xFFL) shl 32) or
|
||||
((this[offset + 5].toLong() and 0xFFL) shl 40) or
|
||||
((this[offset + 6].toLong() and 0xFFL) shl 48) or
|
||||
((this[offset + 7].toLong() and 0xFFL) shl 56)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,13 @@ import kotlin.experimental.and
|
||||
import kotlin.experimental.or
|
||||
import kotlin.experimental.xor
|
||||
|
||||
private inline fun ByteArray.combineInto(
|
||||
fun ByteArray.andInto(
|
||||
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)
|
||||
@@ -21,17 +20,16 @@ private inline fun ByteArray.combineInto(
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = combine(this[startIndex + i], that[thatStartIndex + i])
|
||||
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun ByteArray.combineInto(
|
||||
fun ByteArray.andInto(
|
||||
that: Byte,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
combine: (Byte, Byte) -> Byte,
|
||||
) {
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
@@ -39,55 +37,26 @@ private inline fun ByteArray.combineInto(
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = combine(this[startIndex + i], that)
|
||||
destination[destinationOffset + i] = this[startIndex + i] and that
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun ByteArray.combine(that: ByteArray, combine: (Byte, Byte) -> Byte): ByteArray {
|
||||
require(this.size == that.size)
|
||||
fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||
andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
val result = ByteArray(size)
|
||||
combineInto(that, result, combine = combine)
|
||||
return result
|
||||
}
|
||||
|
||||
private inline fun ByteArray.combine(that: Byte, combine: (Byte, Byte) -> Byte): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
combineInto(that, result, combine = combine)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.andInto(
|
||||
that: ByteArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a and b }
|
||||
}
|
||||
|
||||
fun ByteArray.andInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
||||
andInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset)
|
||||
}
|
||||
|
||||
fun ByteArray.andInto(
|
||||
that: Byte,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int = 0,
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a and b }
|
||||
}
|
||||
fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
andInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.and(that: ByteArray): ByteArray {
|
||||
return combine(that) { a, b -> a and b }
|
||||
val result = ByteArray(size)
|
||||
andInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
infix fun ByteArray.and(that: Byte): ByteArray {
|
||||
return combine(that) { a, b -> a and b }
|
||||
val result = ByteArray(size)
|
||||
andInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.orInto(
|
||||
@@ -98,11 +67,15 @@ fun ByteArray.orInto(
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b }
|
||||
}
|
||||
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))
|
||||
|
||||
fun ByteArray.orInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
||||
orInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset)
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.orInto(
|
||||
@@ -112,15 +85,32 @@ fun ByteArray.orInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a or b }
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] or that
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.orWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||
orInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
fun ByteArray.orWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
orInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.or(that: ByteArray): ByteArray {
|
||||
return combine(that) { a, b -> a or b }
|
||||
val result = ByteArray(size)
|
||||
orInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
infix fun ByteArray.or(that: Byte): ByteArray {
|
||||
return combine(that) { a, b -> a or b }
|
||||
val result = ByteArray(size)
|
||||
orInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
@@ -131,11 +121,15 @@ fun ByteArray.xorInto(
|
||||
endIndex: Int = size,
|
||||
thatStartIndex: Int = 0,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b }
|
||||
}
|
||||
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))
|
||||
|
||||
fun ByteArray.xorInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
||||
xorInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset)
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.xorInto(
|
||||
@@ -145,15 +139,32 @@ fun ByteArray.xorInto(
|
||||
startIndex: Int = 0,
|
||||
endIndex: Int = size,
|
||||
) {
|
||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a xor b }
|
||||
val length = endIndex - startIndex
|
||||
require(length >= 0)
|
||||
require(startIndex in 0..(size - length))
|
||||
require(destinationOffset in 0..(destination.size - length))
|
||||
|
||||
for (i in 0..<length) {
|
||||
destination[destinationOffset + i] = this[startIndex + i] xor that
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.xorWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||
xorInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||
|
||||
fun ByteArray.xorWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||
xorInto(that, this, startIndex, startIndex, endIndex)
|
||||
|
||||
infix fun ByteArray.xor(that: ByteArray): ByteArray {
|
||||
return combine(that) { a, b -> a xor b }
|
||||
val result = ByteArray(size)
|
||||
xorInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
infix fun ByteArray.xor(that: Byte): ByteArray {
|
||||
return combine(that) { a, b -> a xor b }
|
||||
val result = ByteArray(size)
|
||||
xorInto(that, result)
|
||||
return result
|
||||
}
|
||||
|
||||
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
||||
@@ -167,9 +178,8 @@ fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startI
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.inv(startIndex: Int = 0, endIndex: Int = size) {
|
||||
fun ByteArray.inv(startIndex: Int, endIndex: Int) =
|
||||
invInto(this, startIndex, startIndex, endIndex)
|
||||
}
|
||||
|
||||
operator fun ByteArray.not(): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
|
||||
@@ -2,6 +2,26 @@ package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||
require(destinationOffset in 0..(destination.size - 4))
|
||||
|
||||
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.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val result = ByteArray(4)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||
require(destinationOffset in 0..(destination.size - 4))
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,43 @@ 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.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val length = endIndex - startIndex
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,34 @@ package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||
require(destinationOffset in 0..(destination.size - 8))
|
||||
|
||||
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.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val result = ByteArray(8)
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import com.infendro.bytes.ByteOrder
|
||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||
|
||||
fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||
require(destinationOffset in 0..(destination.size - 8))
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,51 @@ 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.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||
val length = endIndex - startIndex
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.infendro.bytes.byte
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ByteOperatorTest {
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = (-0x01).toByte()
|
||||
val actual = !0x00.toByte()
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,19 @@ class ByteArrayConversionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyIntoIntArray() {
|
||||
val expected = intArrayOf(0, 0x01234567, 0x01234567)
|
||||
val actual = IntArray(3).also { array ->
|
||||
val bytes = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67,
|
||||
0x01, 0x23, 0x45, 0x67,
|
||||
)
|
||||
bytes.copyInto(array, 1)
|
||||
}
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toIntArray() {
|
||||
val expected = intArrayOf(0x01234567, 0x67452301)
|
||||
@@ -51,6 +64,19 @@ class ByteArrayConversionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun copyIntoLongArray() {
|
||||
val expected = longArrayOf(0, 0x0123456789abcdefL, 0x0123456789abcdefL)
|
||||
val actual = LongArray(3).also { array ->
|
||||
val bytes = byteArrayOf(
|
||||
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||
)
|
||||
bytes.copyInto(array, 1)
|
||||
}
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toLongArray() {
|
||||
val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL)
|
||||
|
||||
@@ -11,6 +11,13 @@ class ByteArrayOperatorTest {
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun andByte() {
|
||||
val expected = byteArrayOf(0x0C, 0x00)
|
||||
val actual = byteArrayOf(0x5C, 0x70) and 0x0F
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun or() {
|
||||
val expected = byteArrayOf(0x7E, 0x7F)
|
||||
@@ -18,6 +25,13 @@ class ByteArrayOperatorTest {
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun orByte() {
|
||||
val expected = byteArrayOf(0x5F, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) or 0x0F
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xor() {
|
||||
val expected = byteArrayOf(0x66, 0x7F)
|
||||
@@ -25,6 +39,13 @@ class ByteArrayOperatorTest {
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun xorByte() {
|
||||
val expected = byteArrayOf(0x53, 0x7F)
|
||||
val actual = byteArrayOf(0x5C, 0x70) xor 0x0F
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = byteArrayOf(-0x01, -0x20)
|
||||
|
||||
13
src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.infendro.bytes.int
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class IntOperatorTest {
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = -0x00000001
|
||||
val actual = !0x00000000
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
13
src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.infendro.bytes.long
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class LongOperatorTest {
|
||||
@Test
|
||||
fun not() {
|
||||
val expected = -0x0000000000000001L
|
||||
val actual = !0x0000000000000000L
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user