Compare commits

..

7 Commits

Author SHA1 Message Date
3127b6c066 Update README.md 2026-04-16 16:10:52 +00:00
e8baac9ba3 Bump version to 1.4.2
All checks were successful
/ test (pull_request) Successful in 27s
2026-03-11 23:17:32 +01:00
d3a67c622c Fix default parameter 2026-03-11 23:12:41 +01:00
c80040466b Bump version to 1.4.1
All checks were successful
/ test (pull_request) Successful in 2m17s
2026-03-11 22:16:30 +01:00
20c2ba4c24 Improve shorthand bytearray operator functions 2026-03-11 22:15:57 +01:00
47666a1e1a Merge pull request '1.4.0 release' (#10) from develop into main
All checks were successful
/ publish (push) Successful in 3m19s
Reviewed-on: Infendro/bytes-kt#10
2026-02-13 23:08:05 +00:00
0129039df9 improve test coverage
All checks were successful
/ test (pull_request) Successful in 2m27s
2026-02-14 00:05:06 +01:00
18 changed files with 371 additions and 296 deletions

View File

@@ -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 ## Installation
@@ -12,6 +12,6 @@ repositories {
} }
commonMain.dependencies { commonMain.dependencies {
implementation("com.infendro:bytes:1.4.0") implementation("com.infendro:bytes:1.4.2")
} }
``` ```

View File

@@ -5,7 +5,7 @@ import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro" group = "com.infendro"
version = "1.4.0" version = "1.4.2"
plugins { plugins {
alias(libs.plugins.infendro) alias(libs.plugins.infendro)

View File

@@ -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 { fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray {
val length = endIndex - startIndex val length = endIndex - startIndex
val result = IntArray(length / 4) val result = IntArray(length / 4)
@@ -57,6 +93,50 @@ fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteO
return result 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 { fun ByteArray.toLongArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): LongArray {
val length = endIndex - startIndex val length = endIndex - startIndex
val result = LongArray(length / 8) val result = LongArray(length / 8)

View File

@@ -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)
}
}
}
}

View File

@@ -5,14 +5,13 @@ import kotlin.experimental.and
import kotlin.experimental.or import kotlin.experimental.or
import kotlin.experimental.xor import kotlin.experimental.xor
private inline fun ByteArray.combineInto( fun ByteArray.andInto(
that: ByteArray, that: ByteArray,
destination: ByteArray, destination: ByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, thatStartIndex: Int = 0,
combine: (Byte, Byte) -> Byte,
) { ) {
val length = endIndex - startIndex val length = endIndex - startIndex
require(length >= 0) require(length >= 0)
@@ -21,17 +20,16 @@ private inline fun ByteArray.combineInto(
require(destinationOffset in 0..(destination.size - length)) require(destinationOffset in 0..(destination.size - length))
for (i in 0..<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, that: Byte,
destination: ByteArray, destination: ByteArray,
destinationOffset: Int = 0, destinationOffset: Int = 0,
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, endIndex: Int = size,
combine: (Byte, Byte) -> Byte,
) { ) {
val length = endIndex - startIndex val length = endIndex - startIndex
require(length >= 0) require(length >= 0)
@@ -39,55 +37,26 @@ private inline fun ByteArray.combineInto(
require(destinationOffset in 0..(destination.size - length)) require(destinationOffset in 0..(destination.size - length))
for (i in 0..<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 { fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
require(this.size == that.size) andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
val result = ByteArray(size) fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
combineInto(that, result, combine = combine) andInto(that, this, startIndex, startIndex, endIndex)
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 }
}
infix fun ByteArray.and(that: ByteArray): ByteArray { 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 { 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( fun ByteArray.orInto(
@@ -98,11 +67,15 @@ fun ByteArray.orInto(
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, 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) { for (i in 0..<length) {
orInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset) destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
}
} }
fun ByteArray.orInto( fun ByteArray.orInto(
@@ -112,15 +85,32 @@ fun ByteArray.orInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, 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 { 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 { 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( fun ByteArray.xorInto(
@@ -131,11 +121,15 @@ fun ByteArray.xorInto(
endIndex: Int = size, endIndex: Int = size,
thatStartIndex: Int = 0, 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) { for (i in 0..<length) {
xorInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset) destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
}
} }
fun ByteArray.xorInto( fun ByteArray.xorInto(
@@ -145,15 +139,32 @@ fun ByteArray.xorInto(
startIndex: Int = 0, startIndex: Int = 0,
endIndex: Int = size, 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 { 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 { 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) { 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) invInto(this, startIndex, startIndex, endIndex)
}
operator fun ByteArray.not(): ByteArray { operator fun ByteArray.not(): ByteArray {
val result = ByteArray(size) val result = ByteArray(size)

View File

@@ -2,6 +2,26 @@ package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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 { fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
val result = ByteArray(4) val result = ByteArray(4)

View File

@@ -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()
}
}
}

View File

@@ -2,6 +2,43 @@ package com.infendro.bytes.intarray
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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 { fun IntArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
val length = endIndex - startIndex val length = endIndex - startIndex

View File

@@ -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()
}
}
}
}

View File

@@ -2,6 +2,34 @@ package com.infendro.bytes.long
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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 { fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
val result = ByteArray(8) val result = ByteArray(8)

View File

@@ -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()
}
}
}

View File

@@ -2,6 +2,51 @@ package com.infendro.bytes.longarray
import com.infendro.bytes.ByteOrder import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN 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 { fun LongArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
val length = endIndex - startIndex val length = endIndex - startIndex

View File

@@ -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()
}
}
}
}

View 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)
}
}

View File

@@ -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 @Test
fun toIntArray() { fun toIntArray() {
val expected = intArrayOf(0x01234567, 0x67452301) 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 @Test
fun toLongArray() { fun toLongArray() {
val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL) val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL)

View File

@@ -11,6 +11,13 @@ class ByteArrayOperatorTest {
assertContentEquals(expected, actual) assertContentEquals(expected, actual)
} }
@Test
fun andByte() {
val expected = byteArrayOf(0x0C, 0x00)
val actual = byteArrayOf(0x5C, 0x70) and 0x0F
assertContentEquals(expected, actual)
}
@Test @Test
fun or() { fun or() {
val expected = byteArrayOf(0x7E, 0x7F) val expected = byteArrayOf(0x7E, 0x7F)
@@ -18,6 +25,13 @@ class ByteArrayOperatorTest {
assertContentEquals(expected, actual) assertContentEquals(expected, actual)
} }
@Test
fun orByte() {
val expected = byteArrayOf(0x5F, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) or 0x0F
assertContentEquals(expected, actual)
}
@Test @Test
fun xor() { fun xor() {
val expected = byteArrayOf(0x66, 0x7F) val expected = byteArrayOf(0x66, 0x7F)
@@ -25,6 +39,13 @@ class ByteArrayOperatorTest {
assertContentEquals(expected, actual) assertContentEquals(expected, actual)
} }
@Test
fun xorByte() {
val expected = byteArrayOf(0x53, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) xor 0x0F
assertContentEquals(expected, actual)
}
@Test @Test
fun not() { fun not() {
val expected = byteArrayOf(-0x01, -0x20) val expected = byteArrayOf(-0x01, -0x20)

View 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)
}
}

View 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)
}
}