Also implement buffer toString in asc

This commit is contained in:
dcodeIO 2018-03-31 00:16:12 +02:00
parent 2e5077da2d
commit 72063577f3
4 changed files with 36 additions and 3 deletions

View File

@ -836,7 +836,8 @@ function createMemoryStream(fn) {
return buffer;
};
stream.toString = function() {
return this.toBuffer().toString("utf8");
var buffer = this.toBuffer();
return utf8.read(buffer, 0, buffer.length);
};
return stream;
}

View File

@ -19,6 +19,38 @@ exports.length = function utf8_length(string) {
return len;
};
exports.read = function utf8_read(buffer, start, end) {
var len = end - start;
if (len < 1)
return "";
var parts = null,
chunk = [],
i = 0, t;
while (start < end) {
t = buffer[start++];
if (t < 128) {
chunk[i++] = t;
} else if (t > 191 && t < 224) {
chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
} else if (t > 239 && t < 365) {
t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
chunk[i++] = 0xD800 + (t >> 10);
chunk[i++] = 0xDC00 + (t & 1023);
} else {
chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
}
if (i > 8191) {
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
i = 0;
}
}
if (parts) {
if (i) parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
return parts.join("");
}
return String.fromCharCode.apply(String, chunk.slice(0, i));
};
exports.write = function utf8_write(string, buffer, offset) {
var start = offset,
c1, c2;

2
dist/asc.js vendored

File diff suppressed because one or more lines are too long

2
dist/asc.js.map vendored

File diff suppressed because one or more lines are too long