Clean up reader.string

This commit is contained in:
mmorrison 2019-02-04 19:58:28 -06:00
parent 6189d2fa62
commit fc5975bf0c
12 changed files with 58 additions and 61 deletions

View file

@ -21,7 +21,9 @@ class Reader {
* @param {Buffer} buffer
**/
constructor(query,buffer) {
this.query = query;
this.defaultEncoding = query.options.encoding || query.encoding;
this.defaultDelimiter = query.delimiter;
this.defaultByteOrder = query.byteorder;
this.buffer = buffer;
this.i = 0;
}
@ -34,56 +36,64 @@ class Reader {
this.i += i;
}
string(...args) {
let options = {};
if(args.length === 0) {
options = {};
} else if(args.length === 1) {
if(typeof args[0] === 'string') options = { delimiter: args[0] };
else if(typeof args[0] === 'number') options = { length: args[0] };
else options = args[0];
pascalString(bytesForSize, adjustment=0) {
const length = this.uint(bytesForSize) + adjustment;
return this.string(length);
}
string(arg) {
let encoding = this.defaultEncoding;
let length = null;
let delimiter = this.defaultDelimiter;
if(typeof arg === 'string') delimiter = arg;
else if(typeof arg === 'number') length = arg;
else if(typeof arg === 'object') {
if ('encoding' in arg) encoding = arg.encoding;
if ('length' in arg) length = arg.length;
if ('delimiter' in arg) delimiter = arg.delimiter;
}
options.encoding = options.encoding || this.query.options.encoding || this.query.encoding;
if(options.encoding === 'latin1') options.encoding = 'win1252';
if(encoding === 'latin1') encoding = 'win1252';
const start = this.i+0;
const start = this.i;
let end = start;
if(!('length' in options)) {
if(length === null) {
// terminated by the delimiter
let delim = options.delimiter || this.query.delimiter;
if(typeof delim === 'string') delim = delim.charCodeAt(0);
while(true) {
if(end >= this.buffer.length) {
let delim = delimiter;
if (typeof delim === 'string') delim = delim.charCodeAt(0);
while (true) {
if (end >= this.buffer.length) {
end = this.buffer.length;
break;
}
if(this.buffer.readUInt8(end) === delim) break;
if (this.buffer.readUInt8(end) === delim) break;
end++;
}
this.i = end+1;
this.i = end + 1;
} else if (length <= 0) {
return '';
} else {
end = start+options.length;
end = start+length;
if(end >= this.buffer.length) {
end = this.buffer.length;
}
this.i = end;
}
let out = this.buffer.slice(start, end);
const enc = options.encoding;
const slice = this.buffer.slice(start, end);
const enc = encoding;
if(enc === 'utf8' || enc === 'ucs2' || enc === 'binary') {
out = out.toString(enc);
return slice.toString(enc);
} else {
out = Iconv.decode(out,enc);
return Iconv.decode(slice,enc);
}
return out;
}
int(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
if(this.query.byteorder === 'be') {
if(this.defaultByteOrder === 'be') {
if(bytes === 1) r = this.buffer.readInt8(this.i);
else if(bytes === 2) r = this.buffer.readInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readInt32BE(this.i);
@ -101,7 +111,7 @@ class Reader {
uint(bytes) {
let r = 0;
if(this.remaining() >= bytes) {
if(this.query.byteorder === 'be') {
if(this.defaultByteOrder === 'be') {
if(bytes === 1) r = this.buffer.readUInt8(this.i);
else if(bytes === 2) r = this.buffer.readUInt16BE(this.i);
else if(bytes === 4) r = this.buffer.readUInt32BE(this.i);
@ -120,7 +130,7 @@ class Reader {
float() {
let r = 0;
if(this.remaining() >= 4) {
if(this.query.byteorder === 'be') r = this.buffer.readFloatBE(this.i);
if(this.defaultByteOrder === 'be') r = this.buffer.readFloatBE(this.i);
else r = this.buffer.readFloatLE(this.i);
}
this.i += 4;