mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-05-02 10:22:15 +00:00
Also implement buffer toString in asc
This commit is contained in:
parent
2e5077da2d
commit
72063577f3
@ -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;
|
||||
}
|
||||
|
@ -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
2
dist/asc.js
vendored
File diff suppressed because one or more lines are too long
2
dist/asc.js.map
vendored
2
dist/asc.js.map
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user