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: null terminated strings #8

Merged
merged 1 commit into from
Mar 14, 2023
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
25 changes: 25 additions & 0 deletions types/string/fixed_length.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { SizedType } from "../types.ts";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

export class FixedLengthString implements SizedType<string> {
byteLength: number;
eliassjogreen marked this conversation as resolved.
Show resolved Hide resolved

constructor(byteLength: number) {
this.byteLength = byteLength;
}

read(dataView: DataView, byteOffset = 0): string {
return decoder.decode(
new Uint8Array(dataView.buffer, byteOffset, byteOffset + this.byteLength),
);
}

write(value: string, dataView: DataView, byteOffset = 0) {
encoder.encodeInto(
value,
new Uint8Array(dataView.buffer, byteOffset, this.byteLength),
);
}
}
27 changes: 2 additions & 25 deletions types/string/mod.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,2 @@
import { SizedType } from "../types.ts";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

export class FixedUTF8String implements SizedType<string> {
byteLength: number;

constructor(byteLength: number) {
this.byteLength = byteLength;
}

read(dataView: DataView, byteOffset = 0): string {
return decoder.decode(
new Uint8Array(dataView.buffer, byteOffset, byteOffset + this.byteLength),
);
}

write(value: string, dataView: DataView, byteOffset = 0) {
encoder.encodeInto(
value,
new Uint8Array(dataView.buffer, byteOffset, this.byteLength),
);
}
}
export * from "./fixed_length.ts";
export * from "./null_terminated.ts";
37 changes: 37 additions & 0 deletions types/string/null_terminated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Type } from "../types.ts";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

export class NullTerminatedString implements Type<string> {
read(dataView: DataView, byteOffset = 0): string {
let endByteLength;

for (let i = byteOffset; i < dataView.byteLength; i++) {
if (dataView.getUint8(i) === 0) {
endByteLength = i - byteOffset;
break;
}
}

if (endByteLength == null) {
throw new TypeError(
"Did not encounter null terminator when attempting to read null terminated string",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

terminator

);
}

return decoder.decode(
new Uint8Array(dataView.buffer, byteOffset, endByteLength),
);
}

write(value: string, dataView: DataView, byteOffset = 0) {
value += "\0";
encoder.encodeInto(
value,
new Uint8Array(dataView.buffer, byteOffset, value.length),
);
}
}

export const nullTerminatedString = new NullTerminatedString();
47 changes: 47 additions & 0 deletions types/string/null_terminated_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
assertEquals,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { nullTerminatedString } from "./null_terminated.ts";

const encoder = new TextEncoder();

Deno.test("NullTerminatedString", async ({ step }) => {
await step("read", () => {
assertEquals(
nullTerminatedString.read(
new DataView(encoder.encode("Hello world!\0").buffer),
),
"Hello world!",
);

assertEquals(
nullTerminatedString.read(
new DataView(encoder.encode("Hello\0world!\0").buffer),
),
"Hello",
);

assertEquals(
nullTerminatedString.read(
new DataView(encoder.encode("Hello\0world!\0").buffer),
6,
),
"world!",
);

assertThrows(
() =>
nullTerminatedString.read(
new DataView(encoder.encode("Hello world!").buffer),
),
"Did not encounter null terminator when attempting to read null terminated string",
);
});

await step("write", () => {
const dataView = new DataView(new Uint8Array(13).buffer);
nullTerminatedString.write("Hello world!", dataView);
assertEquals(dataView.buffer, encoder.encode("Hello world!\0").buffer);
});
});