added buffer.base64std

This commit is contained in:
Sergey Chernov 2026-04-08 09:27:52 +03:00
parent d0d79d2f07
commit 368ce2ce8c
3 changed files with 22 additions and 11 deletions

View File

@ -120,17 +120,18 @@ which is used in `toString`) and hex encoding:
## Members ## Members
| name | meaning | type | | name | meaning | type |
|----------------------------|-----------------------------------------|---------------| |----------------------------|------------------------------------------------|---------------|
| `size` | size | Int | | `size` | size | Int |
| `decodeUtf8` | decode to String using UTF8 rules | Any | | `decodeUtf8` | decode to String using UTF8 rules | Any |
| `+` | buffer concatenation | Any | | `+` | buffer concatenation | Any |
| `toMutable()` | create a mutable copy | MutableBuffer | | `toMutable()` | create a mutable copy | MutableBuffer |
| `hex` | encode to hex strign | String | | `hex` | encode to hex strign | String |
| `Buffer.decodeHex(hexStr) | decode hex string | Buffer | | `Buffer.decodeHex(hexStr) | decode hex string | Buffer |
| `base64` | encode to base64 (url flavor) (2) | String | | `base64` | encode to base64 (url flavor) (2) | String |
| `Buffer.decodeBase64(str)` | decode base64 to new Buffer (2) | Buffer | | `base64std` | encode to base64 (default vocabulary, filling) | String |
| `toBitInput()` | create bit input from a byte buffer (3) | | | `Buffer.decodeBase64(str)` | decode base64 to new Buffer (2) | Buffer |
| `toBitInput()` | create bit input from a byte buffer (3) | |
(1) (1)
: optimized implementation that override `Iterable` one : optimized implementation that override `Iterable` one

View File

@ -30,6 +30,7 @@ import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
import net.sergeych.lynon.LynonType import net.sergeych.lynon.LynonType
import net.sergeych.mp_tools.decodeBase64Url import net.sergeych.mp_tools.decodeBase64Url
import net.sergeych.mp_tools.encodeToBase64
import net.sergeych.mp_tools.encodeToBase64Url import net.sergeych.mp_tools.encodeToBase64Url
import kotlin.math.min import kotlin.math.min
@ -39,6 +40,7 @@ open class ObjBuffer(val byteArray: UByteArray) : Obj() {
val hex by lazy { byteArray.encodeToHex("")} val hex by lazy { byteArray.encodeToHex("")}
val base64 by lazy { byteArray.toByteArray().encodeToBase64Url()} val base64 by lazy { byteArray.toByteArray().encodeToBase64Url()}
val base64std by lazy { byteArray.toByteArray().encodeToBase64()}
fun checkIndex(scope: Scope, index: Obj): Int { fun checkIndex(scope: Scope, index: Obj): Int {
if (index !is ObjInt) if (index !is ObjInt)
@ -196,6 +198,13 @@ open class ObjBuffer(val byteArray: UByteArray) : Obj() {
moduleName = "lyng.stdlib", moduleName = "lyng.stdlib",
getter = { thisAs<ObjBuffer>().base64.toObj() } getter = { thisAs<ObjBuffer>().base64.toObj() }
) )
addPropertyDoc(
name = "base64std",
doc = "Base64 standard string representation of the buffer.",
type = type("lyng.String"),
moduleName = "lyng.stdlib",
getter = { thisAs<ObjBuffer>().base64std.toObj() }
)
addFn("decodeUtf8") { addFn("decodeUtf8") {
ObjString( ObjString(
thisAs<ObjBuffer>().byteArray.toByteArray().decodeToString() thisAs<ObjBuffer>().byteArray.toByteArray().decodeToString()

View File

@ -3233,6 +3233,7 @@ class ScriptTest {
assertEquals( "hello", b.decodeUtf8() ) assertEquals( "hello", b.decodeUtf8() )
println(b.base64) println(b.base64)
println(b.base64std)
println(b.hex) println(b.hex)
assertEquals( b, Buffer.decodeBase64(b.base64) ) assertEquals( b, Buffer.decodeBase64(b.base64) )