From a598ac0f3460510f875acfa786e4c8ca6d54d84c Mon Sep 17 00:00:00 2001 From: MierenManz <63878374+MierenManz@users.noreply.github.com> Date: Tue, 5 Mar 2024 09:56:52 +0100 Subject: [PATCH] feat: Additional string codecs (#29) --- src/string/fixed_length.ts | 6 +-- src/string/prefixed_length.ts | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/string/prefixed_length.ts diff --git a/src/string/fixed_length.ts b/src/string/fixed_length.ts index ed2038a..da1e290 100644 --- a/src/string/fixed_length.ts +++ b/src/string/fixed_length.ts @@ -1,7 +1,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { TEXT_DECODER, TEXT_ENCODER } from "./_common.ts"; -export class FixedLength extends SizedType { +export class FixedLengthString extends SizedType { constructor(length: number) { super(length, 1); } @@ -38,5 +38,5 @@ export class FixedLength extends SizedType { } } -export const asciiChar = new FixedLength(1); -export const utf8Char = new FixedLength(4); +export const asciiChar = new FixedLengthString(1); +export const utf8Char = new FixedLengthString(4); diff --git a/src/string/prefixed_length.ts b/src/string/prefixed_length.ts new file mode 100644 index 0000000..03448b4 --- /dev/null +++ b/src/string/prefixed_length.ts @@ -0,0 +1,73 @@ +import { u8, UnsizedType } from "../mod.ts"; +import { Options } from "../types/_common.ts"; +import { TEXT_DECODER, TEXT_ENCODER } from "./_common.ts"; + +export class PrefixedString extends UnsizedType { + #prefixCodec: UnsizedType; + + constructor(prefixCodec: UnsizedType = u8) { + super(1); + this.#prefixCodec = prefixCodec; + } + + writePacked( + value: string, + dt: DataView, + options: Options = { byteOffset: 0 }, + ): void { + this.#prefixCodec.writePacked(value.length, dt, options); + + const view = new Uint8Array( + dt.buffer, + dt.byteOffset + options.byteOffset, + value.length, + ); + + TEXT_ENCODER.encodeInto(value, view); + super.incrementOffset(options, value.length); + } + + write( + value: string, + dt: DataView, + options: Options = { byteOffset: 0 }, + ): void { + this.#prefixCodec.write(value.length, dt, options); + super.alignOffset(options); + + const view = new Uint8Array( + dt.buffer, + dt.byteLength + options.byteOffset, + value.length, + ); + + TEXT_ENCODER.encodeInto(value, view); + super.incrementOffset(options, value.length); + } + + readPacked(dt: DataView, options: Options = { byteOffset: 0 }): string { + const length = this.#prefixCodec.readPacked(dt, options); + const view = new Uint8Array( + dt.buffer, + dt.byteOffset + options.byteOffset, + length, + ); + + super.incrementOffset(options, length); + return TEXT_DECODER.decode(view); + } + + read(dt: DataView, options: Options = { byteOffset: 0 }): string { + const length = this.#prefixCodec.read(dt, options); + super.alignOffset(options); + + const view = new Uint8Array( + dt.buffer, + dt.byteOffset + options.byteOffset, + length, + ); + + super.incrementOffset(options, length); + return TEXT_DECODER.decode(view); + } +}