Compare commits
No commits in common. "master" and "0.1.7" have entirely different histories.
@ -17,10 +17,6 @@ in native targets.
|
|||||||
|
|
||||||
The last 1.8-based version is 0.0.8. Some fixes are not yet backported to it pls leave an issue of needed.
|
The last 1.8-based version is 0.0.8. Some fixes are not yet backported to it pls leave an issue of needed.
|
||||||
|
|
||||||
# Documentation
|
|
||||||
|
|
||||||
Aside of the samples in this readme please see [library documentation](https://code.sergeych.net/docs/mp_bintools/).
|
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
Add our maven:
|
Add our maven:
|
||||||
|
@ -8,7 +8,7 @@ plugins {
|
|||||||
val serialization_version = "1.6.5-SNAPSHOT"
|
val serialization_version = "1.6.5-SNAPSHOT"
|
||||||
|
|
||||||
group = "net.sergeych"
|
group = "net.sergeych"
|
||||||
version = "0.1.8-SNAPSHOT"
|
version = "0.1.7"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@ -18,7 +18,12 @@ repositories {
|
|||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvmToolchain(8)
|
jvmToolchain(8)
|
||||||
jvm()
|
jvm {
|
||||||
|
withJava()
|
||||||
|
testRuns["test"].executionTask.configure {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
}
|
||||||
js {
|
js {
|
||||||
browser()
|
browser()
|
||||||
nodejs()
|
nodejs()
|
||||||
|
@ -1,80 +0,0 @@
|
|||||||
package net.sergeych.bintools
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlin.math.min
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bytes sequence with comparison, concatenation, and string representation,
|
|
||||||
* could be used as hash keys for pure binary values, etc.
|
|
||||||
*/
|
|
||||||
@Suppress("unused")
|
|
||||||
@Serializable
|
|
||||||
class ByteChunk(val data: UByteArray): Comparable<ByteChunk> {
|
|
||||||
|
|
||||||
val size: Int get() = data.size
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Per-byte comparison also of different length. From two chunks
|
|
||||||
* of different size but equal beginning, the shorter is considered
|
|
||||||
* the smaller.
|
|
||||||
*/
|
|
||||||
override fun compareTo(other: ByteChunk): Int {
|
|
||||||
val limit = min(size, other.size)
|
|
||||||
for( i in 0 ..< limit) {
|
|
||||||
val own = data[i]
|
|
||||||
val their = other.data[i]
|
|
||||||
if( own < their) return -1
|
|
||||||
else if( own > their) return 1
|
|
||||||
}
|
|
||||||
if( size < other.size ) return -1
|
|
||||||
if( size > other.size ) return 1
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Equal chunks means content equality.
|
|
||||||
*/
|
|
||||||
override fun equals(other: Any?): Boolean {
|
|
||||||
if (this === other) return true
|
|
||||||
if (other !is ByteChunk) return false
|
|
||||||
|
|
||||||
return data contentEquals other.data
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Content-based hash code
|
|
||||||
*/
|
|
||||||
override fun hashCode(): Int {
|
|
||||||
return data.contentHashCode()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* hex representation of data
|
|
||||||
*/
|
|
||||||
override fun toString(): String = hex
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hex encoded data
|
|
||||||
*/
|
|
||||||
val hex by lazy { data.encodeToHex() }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* human-readable dump
|
|
||||||
*/
|
|
||||||
val dump by lazy { data.toDump() }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Concatenate two chunks and return new one
|
|
||||||
*/
|
|
||||||
operator fun plus(other: ByteChunk): ByteChunk = ByteChunk(data + other.data)
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun fromHex(hex: String): ByteChunk = ByteChunk(hex.decodeHex().asUByteArray())
|
|
||||||
fun random(size: Int=16): ByteChunk = Random.nextBytes(size).asChunk()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.asChunk() = ByteChunk(this.asUByteArray())
|
|
||||||
@Suppress("unused")
|
|
||||||
fun UByteArray.asChunk() = ByteChunk(this)
|
|
@ -113,8 +113,6 @@ fun Collection<Byte>.encodeToHex(separator: String = " "): String = joinToString
|
|||||||
|
|
||||||
fun ByteArray.toDump(wide: Boolean = false): String = toDumpLines(wide).joinToString("\n")
|
fun ByteArray.toDump(wide: Boolean = false): String = toDumpLines(wide).joinToString("\n")
|
||||||
|
|
||||||
fun UByteArray.toDump(wide: Boolean = false): String = asByteArray().toDumpLines(wide).joinToString("\n")
|
|
||||||
|
|
||||||
fun ByteArray.toDumpLines(wide: Boolean = false): List<String> {
|
fun ByteArray.toDumpLines(wide: Boolean = false): List<String> {
|
||||||
|
|
||||||
val lineSize = if (wide) 32 else 16
|
val lineSize = if (wide) 32 else 16
|
||||||
|
@ -30,7 +30,6 @@ data class Foobar2(val bar: Int, val foo: Int, val other: Int = -1)
|
|||||||
@Framed
|
@Framed
|
||||||
data class FoobarF1(val bar: Int, val foo: Int = 117)
|
data class FoobarF1(val bar: Int, val foo: Int = 117)
|
||||||
|
|
||||||
@Suppress("unused")
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@Framed
|
@Framed
|
||||||
@SerialName("bipack.FoobarF1")
|
@SerialName("bipack.FoobarF1")
|
||||||
@ -441,21 +440,4 @@ class BipackEncoderTest {
|
|||||||
println(y)
|
println(y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class T1(@Fixed val i: Byte)
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testFixedByte() {
|
|
||||||
fun t1(i: Int) {
|
|
||||||
val packed = BipackEncoder.encode(T1(i.toByte()))
|
|
||||||
println(packed.toDump())
|
|
||||||
assertEquals(1, packed.size)
|
|
||||||
assertEquals(i, BipackDecoder.decode<T1>(packed).i.toInt())
|
|
||||||
}
|
|
||||||
t1(127)
|
|
||||||
t1(-127)
|
|
||||||
t1(1)
|
|
||||||
t1(-1)
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user