@deepkit/bson
Version:
Deepkit BSON parser
67 lines • 2.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeUTF8 = decodeUTF8;
exports.decodeUTF8Short = decodeUTF8Short;
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
const model_js_1 = require("./model.js");
const decoder = new TextDecoder('utf-8');
function decodeUTF8(buffer, off = 0, end) {
if (end - off > 8) {
return decoder.decode(buffer.subarray(off, end));
}
else {
return decodeUTF8Short(buffer, off, end);
}
}
decodeUTF8.__type = ['buffer', 'off', () => 0, 'end', 'decodeUTF8', 'PW2!\'2">#\'2$"/%'];
function decodeUTF8Short(buffer, off = 0, end) {
let s = '';
while (off < end) {
let c = buffer[off++];
if (c > 127) {
if (c > 191 && c < 224) {
if (off >= end)
throw new model_js_1.BSONError('UTF-8 decode: incomplete 2-byte sequence');
c = (c & 31) << 6 | buffer[off++] & 63;
}
else if (c > 223 && c < 240) {
if (off + 1 >= end)
throw new model_js_1.BSONError('UTF-8 decode: incomplete 3-byte sequence');
c = (c & 15) << 12 | (buffer[off++] & 63) << 6 | buffer[off++] & 63;
}
else if (c > 239 && c < 248) {
if (off + 2 >= end)
throw new model_js_1.BSONError('UTF-8 decode: incomplete 4-byte sequence');
c = (c & 7) << 18 | (buffer[off++] & 63) << 12 | (buffer[off++] & 63) << 6 | buffer[off++] & 63;
}
else
throw new model_js_1.BSONError('UTF-8 decode: unknown multibyte start 0x' + c.toString(16) + ' at index ' + (off - 1));
if (c <= 0xffff) {
s += String.fromCharCode(c);
}
else if (c <= 0x10ffff) {
c -= 0x10000;
s += String.fromCharCode(c >> 10 | 0xd800, c & 0x3FF | 0xdc00);
}
else
throw new model_js_1.BSONError('UTF-8 decode: code point 0x' + c.toString(16) + ' exceeds UTF-16 reach');
}
else {
if (c === 0) {
return s;
}
s += String.fromCharCode(c);
}
}
return s;
}
decodeUTF8Short.__type = ['buffer', 'off', () => 0, 'end', 'decodeUTF8Short', 'PW2!\'2">#\'2$"/%'];
//# sourceMappingURL=strings.js.map