mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-17 08:51:34 +00:00
Remove unnecessary dependencies in asc bundle; Update dependencies
This commit is contained in:
46
bin/util/utf8.js
Normal file
46
bin/util/utf8.js
Normal file
@ -0,0 +1,46 @@
|
||||
// see: https://github.com/dcodeIO/protobuf.js/tree/master/lib/utf8
|
||||
|
||||
exports.length = function utf8_length(string) {
|
||||
var len = 0,
|
||||
c = 0;
|
||||
for (var i = 0; i < string.length; ++i) {
|
||||
c = string.charCodeAt(i);
|
||||
if (c < 128) {
|
||||
len += 1;
|
||||
} else if (c < 2048) {
|
||||
len += 2;
|
||||
} else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
|
||||
++i;
|
||||
len += 4;
|
||||
} else {
|
||||
len += 3;
|
||||
}
|
||||
}
|
||||
return len;
|
||||
};
|
||||
|
||||
exports.write = function utf8_write(string, buffer, offset) {
|
||||
var start = offset,
|
||||
c1, c2;
|
||||
for (var i = 0; i < string.length; ++i) {
|
||||
c1 = string.charCodeAt(i);
|
||||
if (c1 < 128) {
|
||||
buffer[offset++] = c1;
|
||||
} else if (c1 < 2048) {
|
||||
buffer[offset++] = c1 >> 6 | 192;
|
||||
buffer[offset++] = c1 & 63 | 128;
|
||||
} else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
|
||||
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
|
||||
++i;
|
||||
buffer[offset++] = c1 >> 18 | 240;
|
||||
buffer[offset++] = c1 >> 12 & 63 | 128;
|
||||
buffer[offset++] = c1 >> 6 & 63 | 128;
|
||||
buffer[offset++] = c1 & 63 | 128;
|
||||
} else {
|
||||
buffer[offset++] = c1 >> 12 | 224;
|
||||
buffer[offset++] = c1 >> 6 & 63 | 128;
|
||||
buffer[offset++] = c1 & 63 | 128;
|
||||
}
|
||||
}
|
||||
return offset - start;
|
||||
};
|
Reference in New Issue
Block a user