From 6ea003700f3b6e5b70c58d6960841525cdc2b18b Mon Sep 17 00:00:00 2001 From: sergeych Date: Sat, 8 Mar 2025 02:04:18 +0300 Subject: [PATCH] small improvements --- build.gradle.kts | 5 ++++- .../kotlin/net.sergeych.bintools/ByteChunk.kt | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9e5fe93..2ffccfb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl + plugins { kotlin("multiplatform") version "2.0.21" kotlin("plugin.serialization") version "2.0.21" @@ -6,7 +8,7 @@ plugins { } group = "net.sergeych" -version = "0.1.11" +version = "0.1.12-SNAPSHOT" repositories { mavenCentral() @@ -31,6 +33,7 @@ kotlin { linuxArm64() mingwX64() + @OptIn(ExperimentalWasmDsl::class) wasmJs { browser() binaries.executable() diff --git a/src/commonMain/kotlin/net.sergeych.bintools/ByteChunk.kt b/src/commonMain/kotlin/net.sergeych.bintools/ByteChunk.kt index f3e9555..4b69454 100644 --- a/src/commonMain/kotlin/net.sergeych.bintools/ByteChunk.kt +++ b/src/commonMain/kotlin/net.sergeych.bintools/ByteChunk.kt @@ -1,6 +1,7 @@ package net.sergeych.bintools import kotlinx.serialization.Serializable +import net.sergeych.mp_tools.encodeToBase64Compact import kotlin.math.min import kotlin.random.Random @@ -64,6 +65,11 @@ class ByteChunk(val data: UByteArray): Comparable { */ val dump by lazy { data.toDump() } + /** + * Lazy encode to base64 with url alphabet, without trailing fill '=' characters. + */ + val base64 by lazy { data.asByteArray().encodeToBase64Compact() } + /** * Concatenate two chunks and return new one */ @@ -75,6 +81,13 @@ class ByteChunk(val data: UByteArray): Comparable { } } +/** + * Create the representation of this array as ByteChunk; it does not copy the data. + */ fun ByteArray.asChunk() = ByteChunk(this.asUByteArray()) + +/** + * Create the representation of this array as ByteChunk; it does not copy the data. + */ @Suppress("unused") fun UByteArray.asChunk() = ByteChunk(this) \ No newline at end of file