From 368ce2ce8ca5762ec2a62dcb34132fabe13ca782 Mon Sep 17 00:00:00 2001 From: sergeych Date: Wed, 8 Apr 2026 09:27:52 +0300 Subject: [PATCH] added buffer.base64std --- docs/Buffer.md | 23 ++++++++++--------- .../kotlin/net/sergeych/lyng/obj/ObjBuffer.kt | 9 ++++++++ lynglib/src/commonTest/kotlin/ScriptTest.kt | 1 + 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/docs/Buffer.md b/docs/Buffer.md index a15723f..e1a6203 100644 --- a/docs/Buffer.md +++ b/docs/Buffer.md @@ -120,17 +120,18 @@ which is used in `toString`) and hex encoding: ## Members -| name | meaning | type | -|----------------------------|-----------------------------------------|---------------| -| `size` | size | Int | -| `decodeUtf8` | decode to String using UTF8 rules | Any | -| `+` | buffer concatenation | Any | -| `toMutable()` | create a mutable copy | MutableBuffer | -| `hex` | encode to hex strign | String | -| `Buffer.decodeHex(hexStr) | decode hex string | Buffer | -| `base64` | encode to base64 (url flavor) (2) | String | -| `Buffer.decodeBase64(str)` | decode base64 to new Buffer (2) | Buffer | -| `toBitInput()` | create bit input from a byte buffer (3) | | +| name | meaning | type | +|----------------------------|------------------------------------------------|---------------| +| `size` | size | Int | +| `decodeUtf8` | decode to String using UTF8 rules | Any | +| `+` | buffer concatenation | Any | +| `toMutable()` | create a mutable copy | MutableBuffer | +| `hex` | encode to hex strign | String | +| `Buffer.decodeHex(hexStr) | decode hex string | Buffer | +| `base64` | encode to base64 (url flavor) (2) | String | +| `base64std` | encode to base64 (default vocabulary, filling) | String | +| `Buffer.decodeBase64(str)` | decode base64 to new Buffer (2) | Buffer | +| `toBitInput()` | create bit input from a byte buffer (3) | | (1) : optimized implementation that override `Iterable` one diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjBuffer.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjBuffer.kt index 563d232..37bfa9f 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjBuffer.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjBuffer.kt @@ -30,6 +30,7 @@ import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonType import net.sergeych.mp_tools.decodeBase64Url +import net.sergeych.mp_tools.encodeToBase64 import net.sergeych.mp_tools.encodeToBase64Url import kotlin.math.min @@ -39,6 +40,7 @@ open class ObjBuffer(val byteArray: UByteArray) : Obj() { val hex by lazy { byteArray.encodeToHex("")} val base64 by lazy { byteArray.toByteArray().encodeToBase64Url()} + val base64std by lazy { byteArray.toByteArray().encodeToBase64()} fun checkIndex(scope: Scope, index: Obj): Int { if (index !is ObjInt) @@ -196,6 +198,13 @@ open class ObjBuffer(val byteArray: UByteArray) : Obj() { moduleName = "lyng.stdlib", getter = { thisAs().base64.toObj() } ) + addPropertyDoc( + name = "base64std", + doc = "Base64 standard string representation of the buffer.", + type = type("lyng.String"), + moduleName = "lyng.stdlib", + getter = { thisAs().base64std.toObj() } + ) addFn("decodeUtf8") { ObjString( thisAs().byteArray.toByteArray().decodeToString() diff --git a/lynglib/src/commonTest/kotlin/ScriptTest.kt b/lynglib/src/commonTest/kotlin/ScriptTest.kt index 714240e..2b5b4d8 100644 --- a/lynglib/src/commonTest/kotlin/ScriptTest.kt +++ b/lynglib/src/commonTest/kotlin/ScriptTest.kt @@ -3233,6 +3233,7 @@ class ScriptTest { assertEquals( "hello", b.decodeUtf8() ) println(b.base64) + println(b.base64std) println(b.hex) assertEquals( b, Buffer.decodeBase64(b.base64) )