From 328cb0c655937b9687775c7c53772dc967007bb5 Mon Sep 17 00:00:00 2001 From: luaneko Date: Thu, 9 Jan 2025 19:48:28 +1100 Subject: [PATCH] Add offset argument to encode_utf8 --- bytes.ts | 10 +++++++--- deno.json | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bytes.ts b/bytes.ts index 1f28af8..6856245 100644 --- a/bytes.ts +++ b/bytes.ts @@ -18,9 +18,13 @@ export function to_utf8(s: BinaryLike): Uint8Array { return typeof s === "string" ? encoder.encode(s) : s; } -export function encode_utf8(s: BinaryLike, buf: Uint8Array): number { - if (typeof s === "string") return encoder.encodeInto(s, buf).written; - else return buf.set(s), s.length; +export function encode_utf8(s: BinaryLike, buf: Uint8Array, offset = 0) { + if (typeof s === "string") { + if (offset !== 0) buf = buf.subarray(offset); + return encoder.encodeInto(s, buf).written; + } else { + return buf.set(s, offset), s.length; + } } export function from_hex(s: BinaryLike): Uint8Array { diff --git a/deno.json b/deno.json index 82dd816..ff1f8e6 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,5 @@ { "name": "@luaneko/lstd", - "version": "0.1.5", + "version": "0.1.6", "exports": "./mod.ts" }