feat: utf8

This commit is contained in:
TrèsAbhi committed 2024-02-05 18:06:18 -06:00
1 parent 63084d1855
commit 2de077a3ab
3 files changed
+19 -6

No files matched your search

+1 -1
View File
@@ -14,7 +14,7 @@ A compact binary representation similar to JSON objects. This documentation uses
```cpp ```cpp
struct String { struct String {
uint32 length; uint32 length;
ascii[length] value; utf8[length] value;
} }
``` ```
+5 -5
View File
@@ -44,7 +44,7 @@ export class BkonReadStream extends ReadStream {
} }
magic() { magic() {
const magic = this.ascii(4); const magic = this.utf8(4);
if (magic !== 'BKON') { if (magic !== 'BKON') {
throw new Error(`Invalid Bkon magic number: "${magic}"`); throw new Error(`Invalid Bkon magic number: "${magic}"`);
@@ -100,7 +100,7 @@ export class BkonReadStream extends ReadStream {
string() { string() {
const length = this.uint32(); const length = this.uint32();
const string = this.ascii(length); const string = this.utf8(length);
return string; return string;
} }
@@ -332,11 +332,11 @@ export class BkonWriteStream extends WriteStream {
} }
string(value: string) { string(value: string) {
this.uint32(value.length); this.uint32(Buffer.byteLength(value));
this.ascii(value); this.utf8(value);
} }
magic() { magic() {
this.ascii('BKON'); this.utf8('BKON');
} }
} }
+13
View File
@@ -35,6 +35,9 @@ export class ReadStream {
ascii(size: number) { ascii(size: number) {
return this.buffer.toString('ascii', this.increment(size), this.index); return this.buffer.toString('ascii', this.increment(size), this.index);
} }
utf8(size: number) {
return this.buffer.toString('utf8', this.increment(size), this.index);
}
int8() { int8() {
return this.buffer.readInt8(this.increment(1)); return this.buffer.readInt8(this.increment(1));
@@ -94,6 +97,16 @@ export class WriteStream {
} }
return this; 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) { private uintN(bytes: number, value: number) {
for (let index = 0; index < bytes; index++) { for (let index = 0; index < bytes; index++) {