Compare commits
22 Commits
ff0e17b5ad
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
3127b6c066
|
|||
|
e8baac9ba3
|
|||
|
d3a67c622c
|
|||
|
c80040466b
|
|||
|
20c2ba4c24
|
|||
|
47666a1e1a
|
|||
|
0129039df9
|
|||
|
3801c5759f
|
|||
|
03ec68cc40
|
|||
|
421beeed1e
|
|||
|
19b2fb36fd
|
|||
|
f18adc08b9
|
|||
|
85f10be1d2
|
|||
|
345be1cc6f
|
|||
|
d7d4e0c635
|
|||
|
3e769fe4d0
|
|||
|
17ff49ce0c
|
|||
|
d7e65ccc32
|
|||
|
dddf2bab93
|
|||
|
d55ec62be9
|
|||
|
9c7a9e4926
|
|||
|
afe35355a8
|
@@ -19,7 +19,7 @@ jobs:
|
|||||||
uses: actions/cache@v4
|
uses: actions/cache@v4
|
||||||
with:
|
with:
|
||||||
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
restore-keys: gradle-
|
restore-keys: gradle
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/caches/
|
~/.gradle/caches/
|
||||||
~/.gradle/wrapper/
|
~/.gradle/wrapper/
|
||||||
@@ -28,7 +28,7 @@ jobs:
|
|||||||
uses: actions/cache@v4
|
uses: actions/cache@v4
|
||||||
with:
|
with:
|
||||||
key: konan-${{ hashFiles('**/libs.versions.toml') }}
|
key: konan-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
restore-keys: konan-
|
restore-keys: konan
|
||||||
path: |
|
path: |
|
||||||
~/.konan/
|
~/.konan/
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
uses: actions/cache@v4
|
uses: actions/cache@v4
|
||||||
with:
|
with:
|
||||||
key: node-${{ hashFiles('**/libs.versions.toml') }}
|
key: node-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
restore-keys: node-
|
restore-keys: node
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/nodejs
|
~/.gradle/nodejs
|
||||||
~/.gradle/yarn
|
~/.gradle/yarn
|
||||||
|
|||||||
@@ -19,27 +19,10 @@ jobs:
|
|||||||
uses: actions/cache@v4
|
uses: actions/cache@v4
|
||||||
with:
|
with:
|
||||||
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
restore-keys: gradle-
|
restore-keys: gradle
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/caches/
|
~/.gradle/caches/
|
||||||
~/.gradle/wrapper/
|
~/.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
|
- name: Test
|
||||||
run: ./gradlew allTests
|
run: ./gradlew jvmTest
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# bytes-kt
|
# bytes.kt
|
||||||
|
|
||||||
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with `ByteArray` and `UByteArray`.
|
`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.3.1")
|
implementation("com.infendro:bytes:1.4.2")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -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.3.1"
|
version = "1.4.2"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
alias(libs.plugins.infendro)
|
alias(libs.plugins.infendro)
|
||||||
@@ -33,10 +33,6 @@ kotlin {
|
|||||||
implementation(libs.test)
|
implementation(libs.test)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compilerOptions {
|
|
||||||
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
publishing.repositories {
|
publishing.repositories {
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.infendro.bytes.byte
|
||||||
|
|
||||||
|
import kotlin.experimental.inv
|
||||||
|
|
||||||
|
operator fun Byte.not(): Byte {
|
||||||
|
return inv()
|
||||||
|
}
|
||||||
@@ -9,49 +9,137 @@ fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int {
|
|||||||
|
|
||||||
return when (order) {
|
return when (order) {
|
||||||
BIG_ENDIAN -> {
|
BIG_ENDIAN -> {
|
||||||
(this[startIndex].toInt() shl 24) or (this[startIndex + 1].toInt() shl 16) or
|
((this[startIndex].toInt() and 0xFF) shl 24) or
|
||||||
(this[startIndex + 2].toInt() shl 8) or this[startIndex + 3].toInt()
|
((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 -> {
|
LITTLE_ENDIAN -> {
|
||||||
this[startIndex].toInt() or (this[startIndex + 1].toInt() shl 8) or
|
(this[startIndex].toInt() and 0xFF) or
|
||||||
(this[startIndex + 2].toInt() shl 16) or (this[startIndex + 3].toInt() shl 24)
|
((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.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt =
|
|
||||||
toInt(startIndex, order).toUInt()
|
|
||||||
|
|
||||||
fun ByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN): IntArray =
|
|
||||||
IntArray(size / 4).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun ByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray =
|
|
||||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
|
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
|
||||||
require(startIndex in 0..(size - 8))
|
require(startIndex in 0..(size - 8))
|
||||||
|
|
||||||
return when (order) {
|
return when (order) {
|
||||||
BIG_ENDIAN -> {
|
BIG_ENDIAN -> {
|
||||||
(this[startIndex].toLong() shl 56) or (this[startIndex + 1].toLong() shl 48) or
|
((this[startIndex].toLong() and 0xFFL) shl 56) or
|
||||||
(this[startIndex + 2].toLong() shl 40) or (this[startIndex + 3].toLong() shl 32) or
|
((this[startIndex + 1].toLong() and 0xFFL) shl 48) or
|
||||||
(this[startIndex + 4].toLong() shl 24) or (this[startIndex + 5].toLong() shl 16) or
|
((this[startIndex + 2].toLong() and 0xFFL) shl 40) or
|
||||||
(this[startIndex + 6].toLong() shl 8) or this[startIndex + 7].toLong()
|
((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 -> {
|
LITTLE_ENDIAN -> {
|
||||||
this[startIndex].toLong() or (this[startIndex + 1].toLong() shl 8) or
|
(this[startIndex].toLong() and 0xFFL) or
|
||||||
(this[startIndex + 2].toLong() shl 16) or (this[startIndex + 3].toLong() shl 24) or
|
((this[startIndex + 1].toLong() and 0xFFL) shl 8) or
|
||||||
(this[startIndex + 4].toLong() shl 32) or (this[startIndex + 5].toLong() shl 40) or
|
((this[startIndex + 2].toLong() and 0xFFL) shl 16) or
|
||||||
(this[startIndex + 6].toLong() shl 48) or (this[startIndex + 7].toLong() shl 56)
|
((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): ULong =
|
fun ByteArray.copyInto(
|
||||||
toLong(startIndex, order).toULong()
|
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.toLongArray(order: ByteOrder = BIG_ENDIAN): LongArray =
|
when (order) {
|
||||||
LongArray(size / 8).also { copyInto(it, order = 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 =
|
fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray {
|
||||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
val length = endIndex - startIndex
|
||||||
|
val result = IntArray(length / 4)
|
||||||
|
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||||
|
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)
|
||||||
|
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,89 +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() shl 24) or (this[offset + 1].toInt() shl 16) or
|
|
||||||
(this[offset + 2].toInt() shl 8) or this[offset + 3].toInt()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LITTLE_ENDIAN -> {
|
|
||||||
repeat(length / 4) { i ->
|
|
||||||
val offset = startIndex + (i * 4)
|
|
||||||
destination[destinationOffset + i] =
|
|
||||||
this[offset].toInt() or (this[offset + 1].toInt() shl 8) or
|
|
||||||
(this[offset + 2].toInt() shl 16) or (this[offset + 3].toInt() shl 24)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.copyInto(
|
|
||||||
destination: UIntArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = copyInto(destination.asIntArray(), destinationOffset, startIndex, endIndex, 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() shl 56) or (this[offset + 1].toLong() shl 48) or
|
|
||||||
(this[offset + 2].toLong() shl 40) or (this[offset + 3].toLong() shl 32) or
|
|
||||||
(this[offset + 4].toLong() shl 24) or (this[offset + 5].toLong() shl 16) or
|
|
||||||
(this[offset + 6].toLong() shl 8) or this[offset + 7].toLong()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LITTLE_ENDIAN -> {
|
|
||||||
repeat(length / 8) { i ->
|
|
||||||
val offset = startIndex + (i * 8)
|
|
||||||
destination[destinationOffset + i] =
|
|
||||||
this[offset].toLong() or (this[offset + 1].toLong() shl 8) or
|
|
||||||
(this[offset + 2].toLong() shl 16) or (this[offset + 3].toLong() shl 24) or
|
|
||||||
(this[offset + 4].toLong() shl 32) or (this[offset + 5].toLong() shl 40) or
|
|
||||||
(this[offset + 6].toLong() shl 48) or (this[offset + 7].toLong() shl 56)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.copyInto(
|
|
||||||
destination: ULongArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = copyInto(destination.asLongArray(), destinationOffset, startIndex, endIndex, order)
|
|
||||||
188
src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt
Normal file
188
src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt
Normal 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
|
||||||
|
}
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
package com.infendro.bytes.bytearray
|
|
||||||
|
|
||||||
import kotlin.experimental.and
|
|
||||||
import kotlin.experimental.inv
|
|
||||||
import kotlin.experimental.or
|
|
||||||
import kotlin.experimental.xor
|
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
repeat(length) { i ->
|
|
||||||
destination[destinationOffset + i] = this[startIndex + i].inv()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.inv() =
|
|
||||||
ByteArray(size).also { invInto(it) }
|
|
||||||
|
|
||||||
private inline fun ByteArray.combineInto(
|
|
||||||
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)
|
|
||||||
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 ByteArray.combine(
|
|
||||||
that: ByteArray,
|
|
||||||
combine: (Byte, Byte) -> Byte,
|
|
||||||
): ByteArray {
|
|
||||||
require(this.size == that.size)
|
|
||||||
|
|
||||||
return ByteArray(size).also { combineInto(that, it, combine = combine) }
|
|
||||||
}
|
|
||||||
|
|
||||||
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 }
|
|
||||||
|
|
||||||
infix fun ByteArray.and(that: ByteArray) =
|
|
||||||
combine(that) { a, b -> a and b }
|
|
||||||
|
|
||||||
fun ByteArray.orInto(
|
|
||||||
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 or b }
|
|
||||||
|
|
||||||
infix fun ByteArray.or(that: ByteArray) =
|
|
||||||
combine(that) { a, b -> a or b }
|
|
||||||
|
|
||||||
fun ByteArray.xorInto(
|
|
||||||
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 xor b }
|
|
||||||
|
|
||||||
infix fun ByteArray.xor(that: ByteArray) =
|
|
||||||
combine(that) { a, b -> a xor b }
|
|
||||||
@@ -2,9 +2,29 @@ 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.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||||
ByteArray(4).also { copyInto(it, order = order) }
|
require(destinationOffset in 0..(destination.size - 4))
|
||||||
|
|
||||||
fun Int.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
when (order) {
|
||||||
UByteArray(4).also { copyInto(it, order = 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)
|
||||||
|
copyInto(result, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,34 +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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Int.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = copyInto(destination.asByteArray(), destinationOffset, order)
|
|
||||||
5
src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
5
src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.bytes.int
|
||||||
|
|
||||||
|
operator fun Int.not(): Int {
|
||||||
|
return inv()
|
||||||
|
}
|
||||||
@@ -2,9 +2,47 @@ 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.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
fun IntArray.copyInto(
|
||||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
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)))
|
||||||
|
|
||||||
fun IntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
when (order) {
|
||||||
UByteArray(size * 4).also { copyInto(it, order = 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,49 +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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun IntArray.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = copyInto(destination.asByteArray(), destinationOffset, startIndex, endIndex, order)
|
|
||||||
@@ -2,9 +2,37 @@ 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.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||||
ByteArray(8).also { copyInto(it, order = order) }
|
require(destinationOffset in 0..(destination.size - 8))
|
||||||
|
|
||||||
fun Long.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
when (order) {
|
||||||
UByteArray(8).also { copyInto(it, order = 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)
|
||||||
|
copyInto(result, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,42 +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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Long.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = copyInto(destination.asByteArray(), destinationOffset, order)
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.bytes.long
|
||||||
|
|
||||||
|
operator fun Long.not(): Long {
|
||||||
|
return inv()
|
||||||
|
}
|
||||||
@@ -2,9 +2,55 @@ 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.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
fun LongArray.copyInto(
|
||||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
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)))
|
||||||
|
|
||||||
fun LongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
when (order) {
|
||||||
UByteArray(size * 8).also { copyInto(it, order = 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,57 +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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun LongArray.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = copyInto(destination.asByteArray(), destinationOffset, startIndex, endIndex, order)
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package com.infendro.bytes.ubytearray
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
import com.infendro.bytes.bytearray.toInt
|
|
||||||
import com.infendro.bytes.bytearray.toLong
|
|
||||||
import com.infendro.bytes.bytearray.toUInt
|
|
||||||
import com.infendro.bytes.bytearray.toULong
|
|
||||||
|
|
||||||
fun UByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int =
|
|
||||||
asByteArray().toInt(startIndex, order)
|
|
||||||
|
|
||||||
fun UByteArray.toUInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): UInt =
|
|
||||||
asByteArray().toUInt(startIndex, order)
|
|
||||||
|
|
||||||
fun UByteArray.toIntArray(order: ByteOrder = BIG_ENDIAN): IntArray =
|
|
||||||
IntArray(size / 4).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun UByteArray.toUIntArray(order: ByteOrder = BIG_ENDIAN): UIntArray =
|
|
||||||
UIntArray(size / 4).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun UByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long =
|
|
||||||
asByteArray().toLong(startIndex, order)
|
|
||||||
|
|
||||||
fun UByteArray.toULong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): ULong =
|
|
||||||
asByteArray().toULong(startIndex, order)
|
|
||||||
|
|
||||||
fun UByteArray.toLongArray(order: ByteOrder = BIG_ENDIAN): LongArray =
|
|
||||||
LongArray(size / 8).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun UByteArray.toULongArray(order: ByteOrder = BIG_ENDIAN): ULongArray =
|
|
||||||
ULongArray(size / 8).also { copyInto(it, order = order) }
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package com.infendro.bytes.ubytearray
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
import com.infendro.bytes.bytearray.copyInto
|
|
||||||
|
|
||||||
fun UByteArray.copyInto(
|
|
||||||
destination: IntArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
|
|
||||||
fun UByteArray.copyInto(
|
|
||||||
destination: UIntArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
|
|
||||||
fun UByteArray.copyInto(
|
|
||||||
destination: ULongArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
|
|
||||||
fun UByteArray.copyInto(
|
|
||||||
destination: LongArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asByteArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
package com.infendro.bytes.ubytearray
|
|
||||||
|
|
||||||
import com.infendro.bytes.bytearray.andInto
|
|
||||||
import com.infendro.bytes.bytearray.invInto
|
|
||||||
import com.infendro.bytes.bytearray.orInto
|
|
||||||
import com.infendro.bytes.bytearray.xorInto
|
|
||||||
|
|
||||||
fun UByteArray.invInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
) = asByteArray().invInto(destination.asByteArray(), destinationOffset, startIndex, endIndex)
|
|
||||||
|
|
||||||
fun UByteArray.inv() =
|
|
||||||
UByteArray(size).also { invInto(it) }
|
|
||||||
|
|
||||||
fun UByteArray.andInto(
|
|
||||||
that: UByteArray,
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
thatStartIndex: Int = 0,
|
|
||||||
) = asByteArray().andInto(
|
|
||||||
that.asByteArray(),
|
|
||||||
destination.asByteArray(),
|
|
||||||
destinationOffset,
|
|
||||||
startIndex,
|
|
||||||
endIndex,
|
|
||||||
thatStartIndex,
|
|
||||||
)
|
|
||||||
|
|
||||||
infix fun UByteArray.and(that: UByteArray) =
|
|
||||||
UByteArray(size).also { andInto(that, it) }
|
|
||||||
|
|
||||||
fun UByteArray.orInto(
|
|
||||||
that: UByteArray,
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
thatStartIndex: Int = 0,
|
|
||||||
) = asByteArray().orInto(
|
|
||||||
that.asByteArray(),
|
|
||||||
destination.asByteArray(),
|
|
||||||
destinationOffset,
|
|
||||||
startIndex,
|
|
||||||
endIndex,
|
|
||||||
thatStartIndex,
|
|
||||||
)
|
|
||||||
|
|
||||||
infix fun UByteArray.or(that: UByteArray) =
|
|
||||||
UByteArray(size).also { orInto(that, it) }
|
|
||||||
|
|
||||||
fun UByteArray.xorInto(
|
|
||||||
that: UByteArray,
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
thatStartIndex: Int = 0,
|
|
||||||
) = asByteArray().xorInto(
|
|
||||||
that.asByteArray(),
|
|
||||||
destination.asByteArray(),
|
|
||||||
destinationOffset,
|
|
||||||
startIndex,
|
|
||||||
endIndex,
|
|
||||||
thatStartIndex,
|
|
||||||
)
|
|
||||||
|
|
||||||
infix fun UByteArray.xor(that: UByteArray) =
|
|
||||||
UByteArray(size).also { xorInto(that, it) }
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.infendro.bytes.ubytearray
|
|
||||||
|
|
||||||
fun String.encodeToUByteArray(): UByteArray =
|
|
||||||
encodeToByteArray().asUByteArray()
|
|
||||||
|
|
||||||
fun UByteArray.decodeToString(): String =
|
|
||||||
asByteArray().decodeToString()
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.infendro.bytes.uint
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun UInt.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
ByteArray(4).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun UInt.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
UByteArray(4).also { copyInto(it, order = order) }
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.infendro.bytes.uint
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
import com.infendro.bytes.int.copyInto
|
|
||||||
|
|
||||||
fun UInt.copyInto(
|
|
||||||
destination: ByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = toInt().copyInto(destination, destinationOffset, order)
|
|
||||||
|
|
||||||
fun UInt.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = toInt().copyInto(destination, destinationOffset, order)
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.infendro.bytes.uintarray
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun UIntArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
ByteArray(size * 4).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun UIntArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
UByteArray(size * 4).also { copyInto(it, order = order) }
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package com.infendro.bytes.uintarray
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
import com.infendro.bytes.intarray.copyInto
|
|
||||||
|
|
||||||
fun UIntArray.copyInto(
|
|
||||||
destination: ByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
|
|
||||||
fun UIntArray.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.infendro.bytes.ulong
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun ULong.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
ByteArray(8).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun ULong.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
UByteArray(8).also { copyInto(it, order = order) }
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package com.infendro.bytes.ulong
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
import com.infendro.bytes.long.copyInto
|
|
||||||
|
|
||||||
fun ULong.copyInto(
|
|
||||||
destination: ByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = toLong().copyInto(destination, destinationOffset, order)
|
|
||||||
|
|
||||||
fun ULong.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = toLong().copyInto(destination, destinationOffset, order)
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.infendro.bytes.ulongarray
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun ULongArray.toByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
ByteArray(size * 8).also { copyInto(it, order = order) }
|
|
||||||
|
|
||||||
fun ULongArray.toUByteArray(order: ByteOrder = BIG_ENDIAN) =
|
|
||||||
UByteArray(size * 8).also { copyInto(it, order = order) }
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package com.infendro.bytes.ulongarray
|
|
||||||
|
|
||||||
import com.infendro.bytes.ByteOrder
|
|
||||||
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
|
||||||
import com.infendro.bytes.longarray.copyInto
|
|
||||||
|
|
||||||
fun ULongArray.copyInto(
|
|
||||||
destination: ByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asLongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
|
|
||||||
fun ULongArray.copyInto(
|
|
||||||
destination: UByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
) = asLongArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,113 +8,91 @@ import kotlin.test.assertEquals
|
|||||||
class ByteArrayConversionTest {
|
class ByteArrayConversionTest {
|
||||||
@Test
|
@Test
|
||||||
fun toInt() {
|
fun toInt() {
|
||||||
val expected = 0x12345678
|
val expected = 0x01234567
|
||||||
val actual = arrayOf(
|
run {
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt(),
|
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67).toInt()
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(order = LITTLE_ENDIAN),
|
assertEquals(expected, actual)
|
||||||
)
|
|
||||||
assertEquals(expected, actual[0])
|
|
||||||
assertEquals(expected, actual[1])
|
|
||||||
}
|
}
|
||||||
|
run {
|
||||||
@Test
|
val actual = byteArrayOf(0x67, 0x45, 0x23, 0x01).toInt(order = LITTLE_ENDIAN)
|
||||||
fun toUInt() {
|
assertEquals(expected, actual)
|
||||||
val expected = 0x12345678U
|
|
||||||
val actual = arrayOf(
|
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt(),
|
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(order = LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
assertEquals(expected, actual[0])
|
|
||||||
assertEquals(expected, actual[1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun toIntArray() {
|
|
||||||
val expected = intArrayOf(0x12345678, 0x78563412)
|
|
||||||
val actual = arrayOf(
|
|
||||||
byteArrayOf(
|
|
||||||
0x12, 0x34, 0x56, 0x78,
|
|
||||||
0x78, 0x56, 0x34, 0x12,
|
|
||||||
).toIntArray(),
|
|
||||||
byteArrayOf(
|
|
||||||
0x78, 0x56, 0x34, 0x12,
|
|
||||||
0x12, 0x34, 0x56, 0x78,
|
|
||||||
).toIntArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
assertContentEquals(expected, actual[0])
|
|
||||||
assertContentEquals(expected, actual[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun toUIntArray() {
|
|
||||||
val expected = uintArrayOf(0x12345678U, 0x78563412U)
|
|
||||||
val actual = arrayOf(
|
|
||||||
byteArrayOf(
|
|
||||||
0x12, 0x34, 0x56, 0x78,
|
|
||||||
0x78, 0x56, 0x34, 0x12,
|
|
||||||
).toUIntArray(),
|
|
||||||
byteArrayOf(
|
|
||||||
0x78, 0x56, 0x34, 0x12,
|
|
||||||
0x12, 0x34, 0x56, 0x78,
|
|
||||||
).toUIntArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
assertContentEquals(expected, actual[0])
|
|
||||||
assertContentEquals(expected, actual[1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun toLong() {
|
fun toLong() {
|
||||||
val expected = 0x1234567878563412L
|
val expected = 0x0123456789abcdefL
|
||||||
val actual = arrayOf(
|
run {
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toLong(),
|
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11).toLong()
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toLong(order = LITTLE_ENDIAN),
|
assertEquals(expected, actual)
|
||||||
)
|
}
|
||||||
assertEquals(expected, actual[0])
|
run {
|
||||||
assertEquals(expected, actual[1])
|
val actual = byteArrayOf(-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01).toLong(order = LITTLE_ENDIAN)
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun toULong() {
|
fun copyIntoIntArray() {
|
||||||
val expected = 0x1234567878563412UL
|
val expected = intArrayOf(0, 0x01234567, 0x01234567)
|
||||||
val actual = arrayOf(
|
val actual = IntArray(3).also { array ->
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toULong(),
|
val bytes = byteArrayOf(
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12).toULong(order = LITTLE_ENDIAN),
|
0x01, 0x23, 0x45, 0x67,
|
||||||
|
0x01, 0x23, 0x45, 0x67,
|
||||||
)
|
)
|
||||||
assertEquals(expected, actual[0])
|
bytes.copyInto(array, 1)
|
||||||
assertEquals(expected, actual[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
|
@Test
|
||||||
fun toLongArray() {
|
fun toLongArray() {
|
||||||
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
|
val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL)
|
||||||
val actual = arrayOf(
|
run {
|
||||||
byteArrayOf(
|
val actual = byteArrayOf(
|
||||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||||
).toLongArray(),
|
).toLongArray()
|
||||||
byteArrayOf(
|
assertContentEquals(expected, actual)
|
||||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
}
|
||||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
run {
|
||||||
).toLongArray(LITTLE_ENDIAN),
|
val actual = byteArrayOf(
|
||||||
)
|
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||||
assertContentEquals(expected, actual[0])
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
assertContentEquals(expected, actual[1])
|
).toLongArray(order = LITTLE_ENDIAN)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun toULongArray() {
|
|
||||||
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
|
|
||||||
val actual = arrayOf(
|
|
||||||
byteArrayOf(
|
|
||||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
|
||||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
|
||||||
).toULongArray(),
|
|
||||||
byteArrayOf(
|
|
||||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
|
||||||
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
|
||||||
).toULongArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
assertContentEquals(expected, actual[0])
|
|
||||||
assertContentEquals(expected, actual[1])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,26 +2,8 @@ package com.infendro.bytes.bytearray
|
|||||||
|
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertContentEquals
|
import kotlin.test.assertContentEquals
|
||||||
import kotlin.test.assertFails
|
|
||||||
|
|
||||||
class ByteArrayOperatorsTest {
|
|
||||||
@Test
|
|
||||||
fun inv() {
|
|
||||||
val expected = ubyteArrayOf(0xFFU, 0xE0U).asByteArray()
|
|
||||||
val actual = byteArrayOf(0x00, 0x1F).inv()
|
|
||||||
assertContentEquals(expected, actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun andSize() {
|
|
||||||
assertFails {
|
|
||||||
byteArrayOf() and byteArrayOf(0x00)
|
|
||||||
}
|
|
||||||
assertFails {
|
|
||||||
byteArrayOf(0x00) and byteArrayOf()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
class ByteArrayOperatorTest {
|
||||||
@Test
|
@Test
|
||||||
fun and() {
|
fun and() {
|
||||||
val expected = byteArrayOf(0x18, 0x00)
|
val expected = byteArrayOf(0x18, 0x00)
|
||||||
@@ -30,13 +12,10 @@ class ByteArrayOperatorsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun orSize() {
|
fun andByte() {
|
||||||
assertFails {
|
val expected = byteArrayOf(0x0C, 0x00)
|
||||||
byteArrayOf() or byteArrayOf(0x00)
|
val actual = byteArrayOf(0x5C, 0x70) and 0x0F
|
||||||
}
|
assertContentEquals(expected, actual)
|
||||||
assertFails {
|
|
||||||
byteArrayOf(0x00) or byteArrayOf()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -47,13 +26,10 @@ class ByteArrayOperatorsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun xorSize() {
|
fun orByte() {
|
||||||
assertFails {
|
val expected = byteArrayOf(0x5F, 0x7F)
|
||||||
byteArrayOf() xor byteArrayOf(0x00)
|
val actual = byteArrayOf(0x5C, 0x70) or 0x0F
|
||||||
}
|
assertContentEquals(expected, actual)
|
||||||
assertFails {
|
|
||||||
byteArrayOf(0x00) xor byteArrayOf()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -62,4 +38,18 @@ class ByteArrayOperatorsTest {
|
|||||||
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
|
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
|
||||||
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
|
||||||
|
fun not() {
|
||||||
|
val expected = byteArrayOf(-0x01, -0x20)
|
||||||
|
val actual = !byteArrayOf(0x00, 0x1F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,31 +7,14 @@ import kotlin.test.assertContentEquals
|
|||||||
class IntConversionTest {
|
class IntConversionTest {
|
||||||
@Test
|
@Test
|
||||||
fun toByteArray() {
|
fun toByteArray() {
|
||||||
val expected = arrayOf(
|
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67)
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78),
|
run {
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12),
|
val actual = 0x01234567.toByteArray()
|
||||||
)
|
assertContentEquals(expected, actual)
|
||||||
val actual = arrayOf(
|
|
||||||
0x12345678.toByteArray(),
|
|
||||||
0x12345678.toByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
run {
|
||||||
|
val actual = 0x67452301.toByteArray(LITTLE_ENDIAN)
|
||||||
@Test
|
assertContentEquals(expected, actual)
|
||||||
fun toUByteArray() {
|
|
||||||
val expected = arrayOf(
|
|
||||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U),
|
|
||||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U),
|
|
||||||
)
|
|
||||||
val actual = arrayOf(
|
|
||||||
0x12345678.toUByteArray(),
|
|
||||||
0x12345678.toUByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,55 +7,17 @@ import kotlin.test.assertContentEquals
|
|||||||
class IntArrayConversionTest {
|
class IntArrayConversionTest {
|
||||||
@Test
|
@Test
|
||||||
fun toByteArray() {
|
fun toByteArray() {
|
||||||
val expected = arrayOf(
|
val expected = byteArrayOf(
|
||||||
byteArrayOf(
|
0x01, 0x23, 0x45, 0x67,
|
||||||
0x12, 0x34, 0x56, 0x78,
|
0x67, 0x45, 0x23, 0x01,
|
||||||
0x12, 0x34, 0x56, 0x78,
|
|
||||||
),
|
|
||||||
byteArrayOf(
|
|
||||||
0x78, 0x56, 0x34, 0x12,
|
|
||||||
0x78, 0x56, 0x34, 0x12,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
val actual = arrayOf(
|
run {
|
||||||
intArrayOf(
|
val actual = intArrayOf(0x01234567, 0x67452301).toByteArray()
|
||||||
0x12345678,
|
assertContentEquals(expected, actual)
|
||||||
0x12345678,
|
|
||||||
).toByteArray(),
|
|
||||||
intArrayOf(
|
|
||||||
0x12345678,
|
|
||||||
0x12345678,
|
|
||||||
).toByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
run {
|
||||||
|
val actual = intArrayOf(0x67452301, 0x01234567).toByteArray(order = LITTLE_ENDIAN)
|
||||||
@Test
|
assertContentEquals(expected, actual)
|
||||||
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(
|
|
||||||
intArrayOf(
|
|
||||||
0x12345678,
|
|
||||||
0x12345678,
|
|
||||||
).toUByteArray(),
|
|
||||||
intArrayOf(
|
|
||||||
0x12345678,
|
|
||||||
0x12345678,
|
|
||||||
).toUByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,31 +7,14 @@ import kotlin.test.assertContentEquals
|
|||||||
class LongConversionTest {
|
class LongConversionTest {
|
||||||
@Test
|
@Test
|
||||||
fun toByteArray() {
|
fun toByteArray() {
|
||||||
val expected = arrayOf(
|
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11)
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78),
|
run {
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12),
|
val actual = 0x0123456789abcdefL.toByteArray()
|
||||||
)
|
assertContentEquals(expected, actual)
|
||||||
val actual = arrayOf(
|
|
||||||
0x1234567812345678L.toByteArray(),
|
|
||||||
0x1234567812345678L.toByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
run {
|
||||||
|
val actual = (-0x1032547698badcffL).toByteArray(LITTLE_ENDIAN)
|
||||||
@Test
|
assertContentEquals(expected, actual)
|
||||||
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(
|
|
||||||
0x1234567812345678L.toUByteArray(),
|
|
||||||
0x1234567812345678L.toUByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,55 +7,17 @@ import kotlin.test.assertContentEquals
|
|||||||
class LongArrayConversionTest {
|
class LongArrayConversionTest {
|
||||||
@Test
|
@Test
|
||||||
fun toByteArray() {
|
fun toByteArray() {
|
||||||
val expected = arrayOf(
|
val expected = byteArrayOf(
|
||||||
byteArrayOf(
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||||
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(
|
run {
|
||||||
longArrayOf(
|
val actual = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL).toByteArray()
|
||||||
0x1234567812345678L,
|
assertContentEquals(expected, actual)
|
||||||
0x1234567812345678L,
|
|
||||||
).toByteArray(),
|
|
||||||
longArrayOf(
|
|
||||||
0x1234567812345678L,
|
|
||||||
0x1234567812345678L,
|
|
||||||
).toByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
run {
|
||||||
|
val actual = longArrayOf(-0x1032547698badcffL, 0x0123456789abcdefL).toByteArray(order = LITTLE_ENDIAN)
|
||||||
@Test
|
assertContentEquals(expected, actual)
|
||||||
fun 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(
|
|
||||||
longArrayOf(
|
|
||||||
0x1234567812345678L,
|
|
||||||
0x1234567812345678L,
|
|
||||||
).toUByteArray(),
|
|
||||||
longArrayOf(
|
|
||||||
0x1234567812345678L,
|
|
||||||
0x1234567812345678L,
|
|
||||||
).toUByteArray(LITTLE_ENDIAN),
|
|
||||||
)
|
|
||||||
for (i in 0..1) {
|
|
||||||
assertContentEquals(expected[i], actual[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package com.infendro.bytes.ubytearray
|
|
||||||
|
|
||||||
import kotlin.test.Test
|
|
||||||
import kotlin.test.assertContentEquals
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
class UByteArrayStringTest {
|
|
||||||
@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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user