@rightcapital/phpdoc-parser
Version:
TypeScript version of PHPDoc parser with support for intersection types and generics
42 lines (41 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringUnescaper = void 0;
class StringUnescaper {
static unescapeString(input) {
const quote = input[0];
if (quote === "'") {
return input.slice(1, input.length - 1).replaceAll(/\\([\\\.])/g, '$1');
}
return this.parseEscapeSequences(input.slice(1, input.length - 1), '"');
}
static parseEscapeSequences(input, quote) {
input = input.replaceAll(new RegExp(`\\${quote}`, 'g'), quote);
return input.replaceAll(/\\([\\nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}|u\{([0-9a-fA-F]+)\})/g, (substring, ...matches) => {
const firstCaptureGroup = matches[0];
if (this.REPLACEMENTS[firstCaptureGroup]) {
return this.REPLACEMENTS[firstCaptureGroup];
}
if (firstCaptureGroup[0] === 'x' || firstCaptureGroup[0] === 'X') {
return String.fromCharCode(parseInt(firstCaptureGroup.slice(1), 16));
}
if (firstCaptureGroup[0] === 'u') {
return String.fromCharCode(parseInt(matches[1], 16));
}
return String.fromCharCode(parseInt(firstCaptureGroup, 8));
});
}
getType() {
return 'Scalar_String';
}
}
exports.StringUnescaper = StringUnescaper;
StringUnescaper.REPLACEMENTS = {
'\\': '\\',
n: '\n',
r: '\r',
t: '\t',
f: '\f',
v: '\v',
e: '\x1B',
};