Add functions for reading/writing strings without lengths
This commit is contained in:
parent
4cacdbf113
commit
961df8cd72
8 changed files with 94 additions and 26 deletions
|
@ -50,4 +50,25 @@ export class ReaderBase {
|
||||||
public readBool() {
|
public readBool() {
|
||||||
return Boolean(this.readUByte());
|
return Boolean(this.readUByte());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public readShortString() {
|
||||||
|
const length = this.readUByte();
|
||||||
|
let text = "";
|
||||||
|
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
text += String.fromCharCode(this.readUByte());
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readBytesAsString(bytesToRead:number) {
|
||||||
|
let text = "";
|
||||||
|
|
||||||
|
for (let i = 0; i < bytesToRead; i++) {
|
||||||
|
text += String.fromCharCode(this.readUByte());
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -76,4 +76,19 @@ export class WriterBase {
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public writeStringAsBytes(text:string) {
|
||||||
|
let buffer;
|
||||||
|
if (this.resizable) {
|
||||||
|
buffer = getBufferClass().alloc(text.length);
|
||||||
|
} else {
|
||||||
|
buffer = this.buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < text.length; i++) {
|
||||||
|
buffer.writeUInt8(text.charCodeAt(i), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -9,6 +9,8 @@ export interface IReader {
|
||||||
readByte(): number,
|
readByte(): number,
|
||||||
readUByte(): number,
|
readUByte(): number,
|
||||||
readBool(): boolean,
|
readBool(): boolean,
|
||||||
|
readShortString(): string,
|
||||||
|
readBytesAsString(bytesToRead:number): string,
|
||||||
readShort(): number,
|
readShort(): number,
|
||||||
readUShort(): number,
|
readUShort(): number,
|
||||||
readInt(): number,
|
readInt(): number,
|
||||||
|
@ -17,7 +19,7 @@ export interface IReader {
|
||||||
readULong(): bigint,
|
readULong(): bigint,
|
||||||
readFloat(): number,
|
readFloat(): number,
|
||||||
readDouble(): number,
|
readDouble(): number,
|
||||||
readShortString(): string,
|
|
||||||
readString(): string,
|
readString(): string,
|
||||||
|
readShortsAsString(shortsToRead:number): string,
|
||||||
readString16(): string,
|
readString16(): string,
|
||||||
}
|
}
|
|
@ -53,20 +53,9 @@ export class ReaderBE extends ReaderBase implements IReader {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readShortString() {
|
|
||||||
const length = this.readUByte();
|
|
||||||
let text:string = "";
|
|
||||||
|
|
||||||
for (let i = 0; i < length; i++) {
|
|
||||||
text += String.fromCharCode(this.readUByte());
|
|
||||||
}
|
|
||||||
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public readString() {
|
public readString() {
|
||||||
const length = this.readUShort();
|
const length = this.readUShort();
|
||||||
let text:string = "";
|
let text = "";
|
||||||
|
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
text += String.fromCharCode(this.readUByte());
|
text += String.fromCharCode(this.readUByte());
|
||||||
|
@ -77,7 +66,7 @@ export class ReaderBE extends ReaderBase implements IReader {
|
||||||
|
|
||||||
public readString16() {
|
public readString16() {
|
||||||
const length = this.readUShort();
|
const length = this.readUShort();
|
||||||
let text:string = "";
|
let text = "";
|
||||||
|
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
text += String.fromCharCode(this.readUShort());
|
text += String.fromCharCode(this.readUShort());
|
||||||
|
@ -85,4 +74,14 @@ export class ReaderBE extends ReaderBase implements IReader {
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public readShortsAsString(shortsToRead:number) {
|
||||||
|
let text = "";
|
||||||
|
|
||||||
|
for (let i = 0; i < shortsToRead; i++) {
|
||||||
|
text += String.fromCharCode(this.readUShort());
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -53,17 +53,6 @@ export class ReaderLE extends ReaderBase implements IReader {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public readShortString() {
|
|
||||||
const length = this.readUByte();
|
|
||||||
let text:string = "";
|
|
||||||
|
|
||||||
for (let i = 0; i < length; i++) {
|
|
||||||
text += String.fromCharCode(this.readUByte());
|
|
||||||
}
|
|
||||||
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public readString() {
|
public readString() {
|
||||||
const length = this.readUShort();
|
const length = this.readUShort();
|
||||||
let text:string = "";
|
let text:string = "";
|
||||||
|
@ -85,4 +74,14 @@ export class ReaderLE extends ReaderBase implements IReader {
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public readShortsAsString(shortsToRead:number) {
|
||||||
|
let text = "";
|
||||||
|
|
||||||
|
for (let i = 0; i < shortsToRead; i++) {
|
||||||
|
text += String.fromCharCode(this.readUShort());
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -11,15 +11,17 @@ export interface IWriter {
|
||||||
writeByte(value:number): IWriter,
|
writeByte(value:number): IWriter,
|
||||||
writeUByte(value:number): IWriter,
|
writeUByte(value:number): IWriter,
|
||||||
writeBool(value:boolean|number): IWriter,
|
writeBool(value:boolean|number): IWriter,
|
||||||
|
writeStringAsBytes(text:string): IWriter,
|
||||||
writeShort(value:number): IWriter,
|
writeShort(value:number): IWriter,
|
||||||
writeUShort(value:number): IWriter,
|
writeUShort(value:number): IWriter,
|
||||||
writeInt(value:number): IWriter,
|
writeInt(value:number): IWriter,
|
||||||
writeUInt(value:number): IWriter,
|
writeUInt(value:number): IWriter,
|
||||||
writeLong(value:number|bigint): IWriter,
|
writeLong(value:number|bigint): IWriter,
|
||||||
writeULong(value:number): IWriter,
|
writeULong(value:number|bigint): IWriter,
|
||||||
writeFloat(value:number): IWriter,
|
writeFloat(value:number): IWriter,
|
||||||
writeDouble(value:number): IWriter,
|
writeDouble(value:number): IWriter,
|
||||||
writeShortString(text:string): IWriter,
|
writeShortString(text:string): IWriter,
|
||||||
writeString(text:string): IWriter,
|
writeString(text:string): IWriter,
|
||||||
writeString16(text:string): IWriter,
|
writeString16(text:string): IWriter,
|
||||||
|
writeStringAsShorts(text:string): IWriter
|
||||||
}
|
}
|
|
@ -147,4 +147,19 @@ export class WriterBE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public writeStringAsShorts(text:string) {
|
||||||
|
let buffer:Buffer;
|
||||||
|
if (this.resizable) {
|
||||||
|
buffer = getBufferClass().alloc(text.length * 2);
|
||||||
|
} else {
|
||||||
|
buffer = this.buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < text.length; i++) {
|
||||||
|
buffer.writeUint16BE(text.charCodeAt(i), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -146,4 +146,19 @@ export class WriterLE extends WriterBase implements IWriter {
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public writeStringAsShorts(text:string) {
|
||||||
|
let buffer:Buffer;
|
||||||
|
if (this.resizable) {
|
||||||
|
buffer = getBufferClass().alloc(text.length * 2);
|
||||||
|
} else {
|
||||||
|
buffer = this.buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < text.length; i++) {
|
||||||
|
buffer.writeUint16LE(text.charCodeAt(i), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue