You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB
JavaScript

'use strict';
const renderBytes = require("../../render/bytes");
let sequenceMap = {
"ucs2-level1": Buffer.from([0x25, 0x2F, 0x40]),
"ucs2-level2": Buffer.from([0x25, 0x2F, 0x43]),
"ucs2-level3": Buffer.from([0x25, 0x2F, 0x45])
}
module.exports = function parseEscapeSequences(escapeSequences) {
let pos = 0;
let finished = false;
let encodings = [];
while (finished === false) {
if (escapeSequences.readUInt8(pos) === 0) {
/* We've run out of escape sequences to parse */
break;
}
/* We only check for 3-byte-long escape sequences, for now... */
let slice3 = escapeSequences.slice(pos, pos + 3);
let encoding = Object.keys(sequenceMap).find((encoding) => sequenceMap[encoding].equals(slice3));
if (encoding == null) {
throw new Error(`Encountered unrecognized escape sequence; remaining bytes to parse: ${renderBytes(escapeSequences.slice(pos))}`);
} else {
pos += sequenceMap[encoding].length;
encodings.push(encoding);
}
}
if (encodings.length === 0) {
return ["ascii"];
} else {
return encodings;
}
};