Compare commits

...

23 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
3801c5759f bump version to 1.4.0
All checks were successful
/ test (pull_request) Successful in 41s
2026-02-05 14:29:19 +01:00
03ec68cc40 remove unsigned support 2026-02-05 14:19:51 +01:00
421beeed1e Merge pull request '1.3.4 release' (#9) from develop into main
All checks were successful
/ publish (push) Successful in 2m48s
Reviewed-on: Infendro/bytes-kt#9
2026-02-03 22:06:17 +00:00
19b2fb36fd bump version to 1.3.4
All checks were successful
/ test (pull_request) Successful in 1m44s
2026-02-03 23:02:24 +01:00
f18adc08b9 implement shorthand bytearray operations 2026-02-03 23:01:19 +01:00
85f10be1d2 Merge pull request '1.3.3 release' (#8) from develop into main
All checks were successful
/ publish (push) Successful in 1m22s
Reviewed-on: Infendro/bytes-kt#8
2026-01-22 10:59:48 +00:00
345be1cc6f bump version to 1.3.3
All checks were successful
/ test (pull_request) Successful in 28s
2026-01-22 11:58:36 +01:00
d7d4e0c635 implement bytearray operators with byte 2026-01-22 11:35:09 +01:00
3e769fe4d0 small refactor to ci caching 2026-01-17 12:38:20 +01:00
17ff49ce0c Merge pull request '1.3.2 release' (#7) from develop into main
All checks were successful
/ publish (push) Successful in 3m26s
Reviewed-on: Infendro/bytes-kt#7
2026-01-15 15:23:32 +00:00
d7e65ccc32 fix ByteArray to numeric conversion
All checks were successful
/ test (pull_request) Successful in 2m49s
2026-01-15 16:18:44 +01:00
dddf2bab93 add return types 2026-01-15 14:56:46 +01:00
d55ec62be9 simplify test workflow to JVM only 2026-01-15 14:45:31 +01:00
9c7a9e4926 Merge pull request '1.3.1 release' (#6) from develop into main
All checks were successful
/ publish (push) Successful in 2m14s
Reviewed-on: Infendro/bytes-kt#6
2026-01-15 13:18:42 +00:00
ff0e17b5ad switch to signed-first approach
All checks were successful
/ test (pull_request) Successful in 2m47s
It turns out signed math is more performant
2026-01-15 14:10:14 +01:00
d06e523914 fix REAMDE 2026-01-14 16:18:57 +01:00
39 changed files with 794 additions and 1104 deletions

View File

@@ -19,7 +19,7 @@ jobs:
uses: actions/cache@v4
with:
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: gradle-
restore-keys: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
@@ -28,7 +28,7 @@ jobs:
uses: actions/cache@v4
with:
key: konan-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: konan-
restore-keys: konan
path: |
~/.konan/
@@ -36,7 +36,7 @@ jobs:
uses: actions/cache@v4
with:
key: node-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: node-
restore-keys: node
path: |
~/.gradle/nodejs
~/.gradle/yarn

View File

@@ -19,27 +19,10 @@ jobs:
uses: actions/cache@v4
with:
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: gradle-
restore-keys: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
- name: Cache Konan
uses: actions/cache@v4
with:
key: konan-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: konan-
path: |
~/.konan/
- name: Cache Node
uses: actions/cache@v4
with:
key: node-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: node-
path: |
~/.gradle/nodejs
~/.gradle/yarn
- name: Test
run: ./gradlew allTests
run: ./gradlew jvmTest

View File

@@ -1,15 +1,6 @@
# bytes-kt
# bytes.kt
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `UByteArray` and `ByteArray`.
## Targets
| Target | Status |
|------------------|---------------|
| JVM | Supported |
| JavaScript | Supported |
| Native (Linux) | Supported |
| Native (Windows) | Not Supported |
`bytes.kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
## Installation
@@ -20,7 +11,7 @@ repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
}
dependencies {
implementation("com.infendro:bytearray:1.3.0")
commonMain.dependencies {
implementation("com.infendro:bytes:1.4.2")
}
```

View File

@@ -1,8 +1,11 @@
@file:OptIn(ExperimentalWasmDsl::class)
import com.infendro.plugin.infendro
import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro"
version = "1.3.0"
version = "1.4.2"
plugins {
alias(libs.plugins.infendro)
@@ -13,9 +16,13 @@ plugins {
kotlin {
jvm()
linuxX64()
js {
nodejs()
}
linuxArm64()
mingwX64()
macosX64()
macosArm64()
js { nodejs() }
wasmJs { nodejs() }
wasmWasi { nodejs() }
repositories {
mavenCentral()
@@ -26,10 +33,6 @@ kotlin {
implementation(libs.test)
}
}
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}
}
publishing.repositories {

View File

@@ -0,0 +1,7 @@
package com.infendro.bytes.byte
import kotlin.experimental.inv
operator fun Byte.not(): Byte {
return inv()
}

View File

@@ -2,31 +2,144 @@ package com.infendro.bytes.bytearray
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ubytearray.toInt
import com.infendro.bytes.ubytearray.toLong
import com.infendro.bytes.ubytearray.toUInt
import com.infendro.bytes.ubytearray.toULong
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun ByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
asUByteArray().toUInt(startIndex, order)
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int {
require(startIndex in 0..(size - 4))
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
asUByteArray().toInt(startIndex, order)
return when (order) {
BIG_ENDIAN -> {
((this[startIndex].toInt() and 0xFF) shl 24) or
((this[startIndex + 1].toInt() and 0xFF) shl 16) or
((this[startIndex + 2].toInt() and 0xFF) shl 8) or
(this[startIndex + 3].toInt() and 0xFF)
}
LITTLE_ENDIAN -> {
(this[startIndex].toInt() and 0xFF) or
((this[startIndex + 1].toInt() and 0xFF) shl 8) or
((this[startIndex + 2].toInt() and 0xFF) shl 16) or
((this[startIndex + 3].toInt() and 0xFF) shl 24)
}
}
}
fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN) =
UIntArray(size / 4).also { copyInto(it, order = order) }
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
require(startIndex in 0..(size - 8))
fun ByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN) =
IntArray(size / 4).also { copyInto(it, order = order) }
return when (order) {
BIG_ENDIAN -> {
((this[startIndex].toLong() and 0xFFL) shl 56) or
((this[startIndex + 1].toLong() and 0xFFL) shl 48) or
((this[startIndex + 2].toLong() and 0xFFL) shl 40) or
((this[startIndex + 3].toLong() and 0xFFL) shl 32) or
((this[startIndex + 4].toLong() and 0xFFL) shl 24) or
((this[startIndex + 5].toLong() and 0xFFL) shl 16) or
((this[startIndex + 6].toLong() and 0xFFL) shl 8) or
(this[startIndex + 7].toLong() and 0xFFL)
}
LITTLE_ENDIAN -> {
(this[startIndex].toLong() and 0xFFL) or
((this[startIndex + 1].toLong() and 0xFFL) shl 8) or
((this[startIndex + 2].toLong() and 0xFFL) shl 16) or
((this[startIndex + 3].toLong() and 0xFFL) shl 24) or
((this[startIndex + 4].toLong() and 0xFFL) shl 32) or
((this[startIndex + 5].toLong() and 0xFFL) shl 40) or
((this[startIndex + 6].toLong() and 0xFFL) shl 48) or
((this[startIndex + 7].toLong() and 0xFFL) shl 56)
}
}
}
fun ByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
asUByteArray().toULong(startIndex, order)
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)))
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
asUByteArray().toLong(startIndex, order)
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.toULongArray(order: ByteOrder = BIG_ENDIAN) =
ULongArray(size / 8).also { copyInto(it, order = order) }
fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray {
val length = endIndex - startIndex
val result = IntArray(length / 4)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}
fun ByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
LongArray(size / 8).also { copyInto(it, order = order) }
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)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}

View File

@@ -1,37 +0,0 @@
package com.infendro.bytes.bytearray
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ubytearray.copyInto
fun ByteArray.copyInto(
destination: UIntArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
fun ByteArray.copyInto(
destination: IntArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
fun ByteArray.copyInto(
destination: ULongArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
fun ByteArray.copyInto(
destination: LongArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)

View File

@@ -0,0 +1,188 @@
package com.infendro.bytes.bytearray
import com.infendro.bytes.byte.not
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
fun ByteArray.andInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) {
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))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
}
}
fun ByteArray.andInto(
that: Byte,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) {
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] and that
}
}
fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
andInto(that, this, startIndex, startIndex, endIndex)
infix fun ByteArray.and(that: ByteArray): ByteArray {
val result = ByteArray(size)
andInto(that, result)
return result
}
infix fun ByteArray.and(that: Byte): ByteArray {
val result = ByteArray(size)
andInto(that, result)
return result
}
fun ByteArray.orInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) {
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))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
}
}
fun ByteArray.orInto(
that: Byte,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) {
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 {
val result = ByteArray(size)
orInto(that, result)
return result
}
infix fun ByteArray.or(that: Byte): ByteArray {
val result = ByteArray(size)
orInto(that, result)
return result
}
fun ByteArray.xorInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) {
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))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
}
}
fun ByteArray.xorInto(
that: Byte,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) {
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 {
val result = ByteArray(size)
xorInto(that, result)
return result
}
infix fun ByteArray.xor(that: Byte): ByteArray {
val result = ByteArray(size)
xorInto(that, result)
return result
}
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
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]
}
}
fun ByteArray.inv(startIndex: Int, endIndex: Int) =
invInto(this, startIndex, startIndex, endIndex)
operator fun ByteArray.not(): ByteArray {
val result = ByteArray(size)
invInto(result)
return result
}

View File

@@ -1,70 +0,0 @@
package com.infendro.bytes.bytearray
import com.infendro.bytes.ubytearray.*
fun ByteArray.invInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) = asUByteArray().invInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex)
fun ByteArray.inv() =
(asUByteArray().inv()).asByteArray()
fun ByteArray.andInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) = asUByteArray().andInto(
that.asUByteArray(),
destination.asUByteArray(),
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
)
infix fun ByteArray.and(that: ByteArray) =
(this.asUByteArray() and that.asUByteArray()).asByteArray()
fun ByteArray.orInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) = asUByteArray().orInto(
that.asUByteArray(),
destination.asUByteArray(),
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
)
infix fun ByteArray.or(that: ByteArray) =
(this.asUByteArray() or that.asUByteArray()).asByteArray()
fun ByteArray.xorInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) = asUByteArray().xorInto(
that.asUByteArray(),
destination.asUByteArray(),
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
)
infix fun ByteArray.xor(that: ByteArray) =
(this.asUByteArray() xor that.asUByteArray()).asByteArray()

View File

@@ -2,15 +2,29 @@ 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.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(4).also { copyInto(it, order = order) }
fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
require(destinationOffset in 0..(destination.size - 4))
fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(4).also { copyInto(it, order = order) }
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 IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 4).also { copyInto(it, order = order) }
fun IntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 4).also { copyInto(it, order = order) }
fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
val result = ByteArray(4)
copyInto(result, order = order)
return result
}

View File

@@ -1,33 +0,0 @@
package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.uint.copyInto
fun Int.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toUInt().copyInto(destination, destinationOffset, order)
fun Int.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toUInt().copyInto(destination, destinationOffset, order)
fun IntArray.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
fun IntArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)

View File

@@ -0,0 +1,5 @@
package com.infendro.bytes.int
operator fun Int.not(): Int {
return inv()
}

View File

@@ -0,0 +1,48 @@
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
val result = ByteArray(length * 4)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}

View File

@@ -2,15 +2,37 @@ 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.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(8).also { copyInto(it, order = order) }
fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
require(destinationOffset in 0..(destination.size - 8))
fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(8).also { copyInto(it, order = order) }
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 LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 8).also { copyInto(it, order = order) }
fun LongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 8).also { copyInto(it, order = order) }
fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
val result = ByteArray(8)
copyInto(result, order = order)
return result
}

View File

@@ -1,33 +0,0 @@
package com.infendro.bytes.long
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ulong.copyInto
fun Long.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toULong().copyInto(destination, destinationOffset, order)
fun Long.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toULong().copyInto(destination, destinationOffset, order)
fun LongArray.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asULongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
fun LongArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asULongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)

View File

@@ -0,0 +1,5 @@
package com.infendro.bytes.long
operator fun Long.not(): Long {
return inv()
}

View File

@@ -0,0 +1,56 @@
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
val result = ByteArray(length * 8)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}

View File

@@ -1,57 +0,0 @@
package com.infendro.bytes.ubytearray
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun UByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt {
require(startIndex in 0..(size - 4))
return when (order) {
BIG_ENDIAN -> {
(this[startIndex].toUInt() shl 24) or (this[startIndex + 1].toUInt() shl 16) or
(this[startIndex + 2].toUInt() shl 8) or this[startIndex + 3].toUInt()
}
LITTLE_ENDIAN -> {
this[startIndex].toUInt() or (this[startIndex + 1].toUInt() shl 8) or
(this[startIndex + 2].toUInt() shl 16) or (this[startIndex + 3].toUInt() shl 24)
}
}
}
fun UByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
toUInt(startIndex, order).toInt()
fun UByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN) =
UIntArray(size / 4).also { copyInto(it, order = order) }
fun UByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN) =
IntArray(size / 4).also { copyInto(it, order = order) }
fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong {
require(startIndex in 0..(size - 8))
return when (order) {
BIG_ENDIAN -> {
(this[startIndex].toULong() shl 56) or (this[startIndex + 1].toULong() shl 48) or
(this[startIndex + 2].toULong() shl 40) or (this[startIndex + 3].toULong() shl 32) or
(this[startIndex + 4].toULong() shl 24) or (this[startIndex + 5].toULong() shl 16) or
(this[startIndex + 6].toULong() shl 8) or this[startIndex + 7].toULong()
}
LITTLE_ENDIAN -> {
this[startIndex].toULong() or (this[startIndex + 1].toULong() shl 8) or
(this[startIndex + 2].toULong() shl 16) or (this[startIndex + 3].toULong() shl 24) or
(this[startIndex + 4].toULong() shl 32) or (this[startIndex + 5].toULong() shl 40) or
(this[startIndex + 6].toULong() shl 48) or (this[startIndex + 7].toULong() shl 56)
}
}
}
fun UByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN) =
toULong(startIndex, order).toLong()
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN) =
ULongArray(size / 8).also { copyInto(it, order = order) }
fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN) =
LongArray(size / 8).also { copyInto(it, order = order) }

View File

@@ -1,89 +0,0 @@
package com.infendro.bytes.ubytearray
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun UByteArray.copyInto(
destination: UIntArray,
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].toUInt() shl 24) or (this[offset + 1].toUInt() shl 16) or
(this[offset + 2].toUInt() shl 8) or this[offset + 3].toUInt()
}
}
LITTLE_ENDIAN -> {
repeat(length / 4) { i ->
val offset = startIndex + (i * 4)
destination[destinationOffset + i] =
this[offset].toUInt() or (this[offset + 1].toUInt() shl 8) or
(this[offset + 2].toUInt() shl 16) or (this[offset + 3].toUInt() shl 24)
}
}
}
}
fun UByteArray.copyInto(
destination: IntArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUIntArray(), destinationOffset, startIndex, endIndex, order)
fun UByteArray.copyInto(
destination: ULongArray,
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].toULong() shl 56) or (this[offset + 1].toULong() shl 48) or
(this[offset + 2].toULong() shl 40) or (this[offset + 3].toULong() shl 32) or
(this[offset + 4].toULong() shl 24) or (this[offset + 5].toULong() shl 16) or
(this[offset + 6].toULong() shl 8) or this[offset + 7].toULong()
}
}
LITTLE_ENDIAN -> {
repeat(length / 8) { i ->
val offset = startIndex + (i * 8)
destination[destinationOffset + i] =
this[offset].toULong() or (this[offset + 1].toULong() shl 8) or
(this[offset + 2].toULong() shl 16) or (this[offset + 3].toULong() shl 24) or
(this[offset + 4].toULong() shl 32) or (this[offset + 5].toULong() shl 40) or
(this[offset + 6].toULong() shl 48) or (this[offset + 7].toULong() shl 56)
}
}
}
}
fun UByteArray.copyInto(
destination: LongArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asULongArray(), destinationOffset, startIndex, endIndex, order)

View File

@@ -1,105 +0,0 @@
package com.infendro.bytes.ubytearray
fun UByteArray.invInto(
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - length))
repeat(length) { i ->
destination[destinationOffset + i] = this[startIndex + i].inv()
}
}
fun UByteArray.inv() = UByteArray(size).also { invInto(it) }
private inline fun UByteArray.combineInto(
that: UByteArray,
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
combine: (UByte, UByte) -> UByte,
) {
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))
repeat(length) { i ->
destination[destinationOffset + i] = combine(this[startIndex + i], that[thatStartIndex + i])
}
}
private inline fun UByteArray.combine(
that: UByteArray,
combine: (UByte, UByte) -> UByte,
): UByteArray {
require(this.size == that.size)
return UByteArray(size).also { combineInto(that, it, combine = combine) }
}
fun UByteArray.andInto(
that: UByteArray,
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) = combineInto(
that,
destination,
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
) { a, b -> a and b }
infix fun UByteArray.and(that: UByteArray) =
combine(that) { a, b -> a and b }
fun UByteArray.orInto(
that: UByteArray,
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) = combineInto(
that,
destination,
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
) { a, b -> a or b }
infix fun UByteArray.or(that: UByteArray) =
combine(that) { a, b -> a or b }
fun UByteArray.xorInto(
that: UByteArray,
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) = combineInto(
that,
destination,
destinationOffset,
startIndex,
endIndex,
thatStartIndex,
) { a, b -> a xor b }
infix fun UByteArray.xor(that: UByteArray) =
combine(that) { a, b -> a xor b }

View File

@@ -1,7 +0,0 @@
package com.infendro.bytes.ubytearray
fun String.encodeToUByteArray(): UByteArray =
encodeToByteArray().asUByteArray()
fun UByteArray.decodeToString(): String =
asByteArray().decodeToString()

View File

@@ -1,16 +0,0 @@
package com.infendro.bytes.uint
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(4).also { copyInto(it, order = order) }
fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(4).also { copyInto(it, order = order) }
fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 4).also { copyInto(it, order = order) }
fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 4).also { copyInto(it, order = order) }

View File

@@ -1,78 +0,0 @@
package com.infendro.bytes.uint
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun UInt.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) {
require(destinationOffset in 0..(destination.size - 4))
when (order) {
BIG_ENDIAN -> {
destination[destinationOffset] = (this shr 24).toUByte()
destination[destinationOffset + 1] = (this shr 16).toUByte()
destination[destinationOffset + 2] = (this shr 8).toUByte()
destination[destinationOffset + 3] = this.toUByte()
}
LITTLE_ENDIAN -> {
destination[destinationOffset] = this.toUByte()
destination[destinationOffset + 1] = (this shr 8).toUByte()
destination[destinationOffset + 2] = (this shr 16).toUByte()
destination[destinationOffset + 3] = (this shr 24).toUByte()
}
}
}
fun UInt.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, order)
fun UIntArray.copyInto(
destination: UByteArray,
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).toUByte()
destination[offset + 1] = (value shr 16).toUByte()
destination[offset + 2] = (value shr 8).toUByte()
destination[offset + 3] = value.toUByte()
}
}
LITTLE_ENDIAN -> {
repeat(length) { i ->
val offset = destinationOffset + (i * 4)
val value = this[startIndex + i]
destination[offset] = value.toUByte()
destination[offset + 1] = (value shr 8).toUByte()
destination[offset + 2] = (value shr 16).toUByte()
destination[offset + 3] = (value shr 24).toUByte()
}
}
}
}
fun UIntArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex, order)

View File

@@ -1,16 +0,0 @@
package com.infendro.bytes.ulong
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(8).also { copyInto(it, order = order) }
fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(8).also { copyInto(it, order = order) }
fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
UByteArray(size * 8).also { copyInto(it, order = order) }
fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
ByteArray(size * 8).also { copyInto(it, order = order) }

View File

@@ -1,94 +0,0 @@
package com.infendro.bytes.ulong
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun ULong.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) {
require(destinationOffset in 0..(destination.size - 8))
when (order) {
BIG_ENDIAN -> {
destination[destinationOffset] = (this shr 56).toUByte()
destination[destinationOffset + 1] = (this shr 48).toUByte()
destination[destinationOffset + 2] = (this shr 40).toUByte()
destination[destinationOffset + 3] = (this shr 32).toUByte()
destination[destinationOffset + 4] = (this shr 24).toUByte()
destination[destinationOffset + 5] = (this shr 16).toUByte()
destination[destinationOffset + 6] = (this shr 8).toUByte()
destination[destinationOffset + 7] = this.toUByte()
}
LITTLE_ENDIAN -> {
destination[destinationOffset] = this.toUByte()
destination[destinationOffset + 1] = (this shr 8).toUByte()
destination[destinationOffset + 2] = (this shr 16).toUByte()
destination[destinationOffset + 3] = (this shr 24).toUByte()
destination[destinationOffset + 4] = (this shr 32).toUByte()
destination[destinationOffset + 5] = (this shr 40).toUByte()
destination[destinationOffset + 6] = (this shr 48).toUByte()
destination[destinationOffset + 7] = (this shr 56).toUByte()
}
}
}
fun ULong.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, order)
fun ULongArray.copyInto(
destination: UByteArray,
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).toUByte()
destination[offset + 1] = (value shr 48).toUByte()
destination[offset + 2] = (value shr 40).toUByte()
destination[offset + 3] = (value shr 32).toUByte()
destination[offset + 4] = (value shr 24).toUByte()
destination[offset + 5] = (value shr 16).toUByte()
destination[offset + 6] = (value shr 8).toUByte()
destination[offset + 7] = value.toUByte()
}
}
LITTLE_ENDIAN -> {
repeat(length) { i ->
val offset = destinationOffset + (i * 8)
val value = this[startIndex + i]
destination[offset] = value.toUByte()
destination[offset + 1] = (value shr 8).toUByte()
destination[offset + 2] = (value shr 16).toUByte()
destination[offset + 3] = (value shr 24).toUByte()
destination[offset + 4] = (value shr 32).toUByte()
destination[offset + 5] = (value shr 40).toUByte()
destination[offset + 6] = (value shr 48).toUByte()
destination[offset + 7] = (value shr 56).toUByte()
}
}
}
}
fun ULongArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex, order)

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

@@ -0,0 +1,98 @@
package com.infendro.bytes.bytearray
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class ByteArrayConversionTest {
@Test
fun toInt() {
val expected = 0x01234567
run {
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67).toInt()
assertEquals(expected, actual)
}
run {
val actual = byteArrayOf(0x67, 0x45, 0x23, 0x01).toInt(order = LITTLE_ENDIAN)
assertEquals(expected, actual)
}
}
@Test
fun toLong() {
val expected = 0x0123456789abcdefL
run {
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11).toLong()
assertEquals(expected, actual)
}
run {
val actual = byteArrayOf(-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01).toLong(order = LITTLE_ENDIAN)
assertEquals(expected, actual)
}
}
@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)
run {
val actual = byteArrayOf(
0x01, 0x23, 0x45, 0x67,
0x67, 0x45, 0x23, 0x01,
).toIntArray()
assertContentEquals(expected, actual)
}
run {
val actual = byteArrayOf(
0x67, 0x45, 0x23, 0x01,
0x01, 0x23, 0x45, 0x67,
).toIntArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
@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)
run {
val actual = byteArrayOf(
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
).toLongArray()
assertContentEquals(expected, actual)
}
run {
val actual = byteArrayOf(
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
).toLongArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

View File

@@ -0,0 +1,55 @@
package com.infendro.bytes.bytearray
import kotlin.test.Test
import kotlin.test.assertContentEquals
class ByteArrayOperatorTest {
@Test
fun and() {
val expected = byteArrayOf(0x18, 0x00)
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
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)
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
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)
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
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)
val actual = !byteArrayOf(0x00, 0x1F)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,20 @@
package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class IntConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67)
run {
val actual = 0x01234567.toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = 0x67452301.toByteArray(LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

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,23 @@
package com.infendro.bytes.intarray
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class IntArrayConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(
0x01, 0x23, 0x45, 0x67,
0x67, 0x45, 0x23, 0x01,
)
run {
val actual = intArrayOf(0x01234567, 0x67452301).toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = intArrayOf(0x67452301, 0x01234567).toByteArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

View File

@@ -0,0 +1,20 @@
package com.infendro.bytes.long
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class LongConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11)
run {
val actual = 0x0123456789abcdefL.toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = (-0x1032547698badcffL).toByteArray(LITTLE_ENDIAN)
assertContentEquals(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)
}
}

View File

@@ -0,0 +1,23 @@
package com.infendro.bytes.longarray
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class LongArrayConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
)
run {
val actual = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL).toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = longArrayOf(-0x1032547698badcffL, 0x0123456789abcdefL).toByteArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

View File

@@ -1,120 +0,0 @@
package com.infendro.bytes.ubytearray
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `UByteArray Conversion Test` {
@Test
fun toUInt() {
val expected = 0x12345678U
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(order = LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun toInt() {
val expected = 0x12345678
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(order = LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun toUIntArray() {
val expected = uintArrayOf(0x12345678U, 0x12345678U)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U,
).toUIntArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U,
).toUIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun toIntArray() {
val expected = intArrayOf(0x12345678, 0x12345678)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U,
).toIntArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U,
).toIntArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun toULong() {
val expected = 0x1234567812345678UL
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(order = LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun toLong() {
val expected = 0x1234567812345678L
val actual = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong(),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(order = LITTLE_ENDIAN),
)
assertEquals(expected, actual[0])
assertEquals(expected, actual[1])
}
@Test
fun toULongArray() {
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
).toULongArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
).toULongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
@Test
fun toLongArray() {
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
val actual = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
).toLongArray(),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
).toLongArray(LITTLE_ENDIAN),
)
assertContentEquals(expected, actual[0])
assertContentEquals(expected, actual[1])
}
}

View File

@@ -1,65 +0,0 @@
package com.infendro.bytes.ubytearray
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertFails
class `UByteArray Operators Test` {
@Test
fun inv() {
val expected = ubyteArrayOf(0xFFU, 0xE0U)
val actual = ubyteArrayOf(0x00U, 0x1FU).inv()
assertContentEquals(expected, actual)
}
@Test
fun `and - size`() {
assertFails {
ubyteArrayOf() and ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) and ubyteArrayOf()
}
}
@Test
fun and() {
val expected = ubyteArrayOf(0x18U, 0x00U)
val actual = ubyteArrayOf(0x5CU, 0x70U) and ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
@Test
fun `or - size`() {
assertFails {
ubyteArrayOf() or ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) or ubyteArrayOf()
}
}
@Test
fun or() {
val expected = ubyteArrayOf(0x7EU, 0x7FU)
val actual = ubyteArrayOf(0x5CU, 0x70U) or ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
@Test
fun `xor - size`() {
assertFails {
ubyteArrayOf() xor ubyteArrayOf(0x00U)
}
assertFails {
ubyteArrayOf(0x00U) xor ubyteArrayOf()
}
}
@Test
fun xor() {
val expected = ubyteArrayOf(0x66U, 0x7FU)
val actual = ubyteArrayOf(0x5CU, 0x70U) xor ubyteArrayOf(0x3AU, 0x0FU)
assertContentEquals(expected, actual)
}
}

View File

@@ -1,21 +0,0 @@
package com.infendro.bytes.ubytearray
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `UByteArray String Test` {
@Test
fun encodeToUByteArray() {
val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U)
val actual = "PASSWORD".encodeToUByteArray()
assertContentEquals(expected, actual)
}
@Test
fun decodeToString() {
val expected = "PASSWORD"
val actual = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U).decodeToString()
assertEquals(expected, actual)
}
}

View File

@@ -1,91 +0,0 @@
package com.infendro.bytes.uint
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `UInt Conversion Test` {
@Test
fun toUByteArray() {
val expected = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
)
val actual = arrayOf(
0x12345678U.toUByteArray(),
0x12345678U.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun toByteArray() {
val expected = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78),
byteArrayOf(0x78, 0x56, 0x34, 0x12),
)
val actual = arrayOf(
0x12345678U.toByteArray(),
0x12345678U.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `Array toUByteArray`() {
val expected = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U,
),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U,
),
)
val actual = arrayOf(
uintArrayOf(
0x12345678U,
0x12345678U,
).toUByteArray(),
uintArrayOf(
0x12345678U,
0x12345678U,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `Array toByteArray`() {
val expected = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78,
),
byteArrayOf(
0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12,
),
)
val actual = arrayOf(
uintArrayOf(
0x12345678U,
0x12345678U,
).toByteArray(),
uintArrayOf(
0x12345678U,
0x12345678U,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}

View File

@@ -1,91 +0,0 @@
package com.infendro.bytes.ulong
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class `ULong Conversion Test` {
@Test
fun toUByteArray() {
val expected = arrayOf(
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U),
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U),
)
val actual = arrayOf(
0x1234567812345678UL.toUByteArray(),
0x1234567812345678UL.toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun toByteArray() {
val expected = arrayOf(
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
)
val actual = arrayOf(
0x1234567812345678UL.toByteArray(),
0x1234567812345678UL.toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `Array toUByteArray`() {
val expected = arrayOf(
ubyteArrayOf(
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
),
ubyteArrayOf(
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
),
)
val actual = arrayOf(
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toUByteArray(),
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toUByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
@Test
fun `Array toByteArray`() {
val expected = arrayOf(
byteArrayOf(
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
),
byteArrayOf(
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
),
)
val actual = arrayOf(
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toByteArray(),
ulongArrayOf(
0x1234567812345678UL,
0x1234567812345678UL,
).toByteArray(LITTLE_ENDIAN),
)
for (i in 0..1) {
assertContentEquals(expected[i], actual[i])
}
}
}