Dump buffer operations in a nicer and more compact way.

This commit is contained in:
László Monda
2016-04-03 02:47:06 +02:00
parent ec65753246
commit c92831907b
2 changed files with 13 additions and 4 deletions

View File

@@ -22,7 +22,7 @@ abstract class Serializable<T> {
fromBinary(buffer: UhkBuffer): T {
let indentation = new Array(Serializable.depth + 1).join(' ');
let isArray = this instanceof UhkArray;
process.stdout.write(`${indentation}${this.constructor.name}.fromBinary: `);
process.stdout.write(`${indentation}${this.constructor.name}.fromBinary: [`);
if (isArray) {
process.stdout.write('\n');
}
@@ -32,7 +32,7 @@ abstract class Serializable<T> {
buffer.enableDump = false;
Serializable.depth--;
if (!isArray) {
process.stdout.write(`=> ${value}\n`);
process.stdout.write(`] => ${value}\n`);
}
return value;
}

View File

@@ -3,6 +3,7 @@ class UhkBuffer {
private static maxCompactLength = 0xFFFF;
private static longCompactLengthPrefix = 0xFF;
private static stringEncoding = 'utf8';
private static isFirstElementToDump = false;
offset: number;
enableDump = false;
@@ -33,14 +34,14 @@ class UhkBuffer {
let value = this.buffer.readUInt8(this.offset);
this.bytesToBacktrack = 1;
this.offset += this.bytesToBacktrack;
this.dump(`uint8(${value}) `);
this.dump(`u8(${value})`);
return value;
}
writeUInt8(value: number): void {
this.buffer.writeUInt8(value, this.offset);
this.offset += 1;
this.dump(`uint8(${value}) `);
this.dump(`u8(${value})`);
}
readInt16(): number {
@@ -140,7 +141,15 @@ class UhkBuffer {
dump(value) {
if (this.enableDump) {
if (!UhkBuffer.isFirstElementToDump) {
process.stdout.write(' ');
}
process.stdout.write(value);
if (UhkBuffer.isFirstElementToDump) {
UhkBuffer.isFirstElementToDump = false;
}
} else {
UhkBuffer.isFirstElementToDump = true;
}
}
}