From 2de077a3ab5392c83b44a52fa36dc6dfd08a16cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Mon, 5 Feb 2024 18:06:18 -0600 Subject: [PATCH] feat: utf8 --- docs/guide/bkon.md | 2 +- src/core/streams/bkon.ts | 10 +++++----- src/core/streams/buffer.ts | 13 +++++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/guide/bkon.md b/docs/guide/bkon.md index ff92e438a..50bed96fd 100644 --- a/docs/guide/bkon.md +++ b/docs/guide/bkon.md @@ -14,7 +14,7 @@ A compact binary representation similar to JSON objects. This documentation uses ```cpp struct String { uint32 length; - ascii[length] value; + utf8[length] value; } ``` diff --git a/src/core/streams/bkon.ts b/src/core/streams/bkon.ts index 14e905a0f..a64927bdf 100644 --- a/src/core/streams/bkon.ts +++ b/src/core/streams/bkon.ts @@ -44,7 +44,7 @@ export class BkonReadStream extends ReadStream { } magic() { - const magic = this.ascii(4); + const magic = this.utf8(4); if (magic !== 'BKON') { throw new Error(`Invalid Bkon magic number: "${magic}"`); @@ -100,7 +100,7 @@ export class BkonReadStream extends ReadStream { string() { const length = this.uint32(); - const string = this.ascii(length); + const string = this.utf8(length); return string; } @@ -332,11 +332,11 @@ export class BkonWriteStream extends WriteStream { } string(value: string) { - this.uint32(value.length); - this.ascii(value); + this.uint32(Buffer.byteLength(value)); + this.utf8(value); } magic() { - this.ascii('BKON'); + this.utf8('BKON'); } } diff --git a/src/core/streams/buffer.ts b/src/core/streams/buffer.ts index c8f0f561f..1edd1730f 100644 --- a/src/core/streams/buffer.ts +++ b/src/core/streams/buffer.ts @@ -35,6 +35,9 @@ export class ReadStream { ascii(size: number) { return this.buffer.toString('ascii', this.increment(size), this.index); } + utf8(size: number) { + return this.buffer.toString('utf8', this.increment(size), this.index); + } int8() { return this.buffer.readInt8(this.increment(1)); @@ -94,6 +97,16 @@ export class WriteStream { } return this; } + utf8(value: string) { + const buffer = Buffer.alloc(Buffer.byteLength(value)); + + buffer.write(value, 'utf8'); + buffer.forEach((value) => { + this.array.push(value); + }); + + return this; + } private uintN(bytes: number, value: number) { for (let index = 0; index < bytes; index++) {