Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Additional string codecs #29

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/string/fixed_length.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
export class FixedLengthString extends SizedType<string> {
constructor(length: number) {
super(length, 1);
}
Expand Down Expand Up @@ -38,5 +38,5 @@ export class FixedLength extends SizedType<string> {
}
}

export const asciiChar = new FixedLength(1);
export const utf8Char = new FixedLength(4);
export const asciiChar = new FixedLengthString(1);
export const utf8Char = new FixedLengthString(4);
73 changes: 73 additions & 0 deletions src/string/prefixed_length.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
#prefixCodec: UnsizedType<number>;

constructor(prefixCodec: UnsizedType<number> = 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);
}
}
Loading