Compare commits

...

5 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
3 changed files with 75 additions and 65 deletions

View File

@@ -1,6 +1,6 @@
# bytes-kt
# bytes.kt
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
`bytes.kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
## Installation
@@ -12,6 +12,6 @@ repositories {
}
commonMain.dependencies {
implementation("com.infendro:bytes:1.4.0")
implementation("com.infendro:bytes:1.4.2")
}
```

View File

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

View File

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