"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseString = parseString;
var CONTROL_CODES = [0, 7, 8, 9, 10, 11, 12, 13, 26, 27, 127 ];
function decodeControlCharacter(char) {
switch (char) {
case "t":
return 0x09;
case "n":
return 0x0a;
case "r":
return 0x0d;
case '"':
return 0x22;
case "′":
return 0x27;
case "\\":
return 0x5c;
}
return -1;
}
var ESCAPE_CHAR = 92;
var QUOTE_CHAR = 34;
function parseString(value) {
var byteArray = [];
var index = 0;
while (index < value.length) {
var charCode = value.charCodeAt(index);
if (CONTROL_CODES.indexOf(charCode) !== -1) {
throw new Error("ASCII control characters are not permitted within string literals");
}
if (charCode === QUOTE_CHAR) {
throw new Error("quotes are not permitted within string literals");
}
if (charCode === ESCAPE_CHAR) {
var firstChar = value.substr(index + 1, 1);
var decodedControlChar = decodeControlCharacter(firstChar);
if (decodedControlChar !== -1) {
byteArray.push(decodedControlChar);
index += 2;
} else {
var hexValue = value.substr(index + 1, 2);
if (!/^[0-9A-F]{2}$/i.test(hexValue)) {
throw new Error("invalid character encoding");
}
byteArray.push(parseInt(hexValue, 16));
index += 3;
}
} else {
byteArray.push(charCode);
index++;
}
}
return byteArray;
}