Added char type and Segment.isString
This commit is contained in:
32
src/index.ts
32
src/index.ts
@@ -35,6 +35,17 @@ export class Reader {
|
||||
return value !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an 8-bit character from the buffer at the current offset, and advance the offset by 1 byte.
|
||||
* @returns A string containing the character read from the buffer.
|
||||
*/
|
||||
public char(): string {
|
||||
this.checkBounds(1);
|
||||
const value = this.view.getUint8(this.offset);
|
||||
this.offset += 1;
|
||||
return String.fromCharCode(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an 8-bit signed integer from the buffer at the current offset, and advance the offset by 1 byte.
|
||||
*
|
||||
@@ -434,6 +445,27 @@ export class Writer {
|
||||
this.offset += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an 8-bit character to the buffer at the current offset, and advance the offset by 1 byte.
|
||||
*
|
||||
* @param value The character or its ASCII code to write.
|
||||
*/
|
||||
public char(value: string | number): void {
|
||||
if (typeof value === "string") {
|
||||
if (value.length !== 1) {
|
||||
throw new Error("Input must be a single character");
|
||||
}
|
||||
this.checkBounds(1);
|
||||
this.view.setUint8(this.offset, value.charCodeAt(0));
|
||||
} else if (typeof value === "number") {
|
||||
this.checkBounds(1);
|
||||
this.view.setUint8(this.offset, value);
|
||||
} else {
|
||||
throw new Error("Input must be a string or number");
|
||||
}
|
||||
this.offset += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an 8-bit signed integer to the buffer at the current offset, and advance the offset by
|
||||
* 1 byte.
|
||||
|
||||
Reference in New Issue
Block a user