1 | 'use strict';
|
---|
2 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
3 | var hasOwn = require('../internals/has-own-property');
|
---|
4 |
|
---|
5 | var $SyntaxError = SyntaxError;
|
---|
6 | var $parseInt = parseInt;
|
---|
7 | var fromCharCode = String.fromCharCode;
|
---|
8 | var at = uncurryThis(''.charAt);
|
---|
9 | var slice = uncurryThis(''.slice);
|
---|
10 | var exec = uncurryThis(/./.exec);
|
---|
11 |
|
---|
12 | var codePoints = {
|
---|
13 | '\\"': '"',
|
---|
14 | '\\\\': '\\',
|
---|
15 | '\\/': '/',
|
---|
16 | '\\b': '\b',
|
---|
17 | '\\f': '\f',
|
---|
18 | '\\n': '\n',
|
---|
19 | '\\r': '\r',
|
---|
20 | '\\t': '\t'
|
---|
21 | };
|
---|
22 |
|
---|
23 | var IS_4_HEX_DIGITS = /^[\da-f]{4}$/i;
|
---|
24 | // eslint-disable-next-line regexp/no-control-character -- safe
|
---|
25 | var IS_C0_CONTROL_CODE = /^[\u0000-\u001F]$/;
|
---|
26 |
|
---|
27 | module.exports = function (source, i) {
|
---|
28 | var unterminated = true;
|
---|
29 | var value = '';
|
---|
30 | while (i < source.length) {
|
---|
31 | var chr = at(source, i);
|
---|
32 | if (chr === '\\') {
|
---|
33 | var twoChars = slice(source, i, i + 2);
|
---|
34 | if (hasOwn(codePoints, twoChars)) {
|
---|
35 | value += codePoints[twoChars];
|
---|
36 | i += 2;
|
---|
37 | } else if (twoChars === '\\u') {
|
---|
38 | i += 2;
|
---|
39 | var fourHexDigits = slice(source, i, i + 4);
|
---|
40 | if (!exec(IS_4_HEX_DIGITS, fourHexDigits)) throw new $SyntaxError('Bad Unicode escape at: ' + i);
|
---|
41 | value += fromCharCode($parseInt(fourHexDigits, 16));
|
---|
42 | i += 4;
|
---|
43 | } else throw new $SyntaxError('Unknown escape sequence: "' + twoChars + '"');
|
---|
44 | } else if (chr === '"') {
|
---|
45 | unterminated = false;
|
---|
46 | i++;
|
---|
47 | break;
|
---|
48 | } else {
|
---|
49 | if (exec(IS_C0_CONTROL_CODE, chr)) throw new $SyntaxError('Bad control character in string literal at: ' + i);
|
---|
50 | value += chr;
|
---|
51 | i++;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | if (unterminated) throw new $SyntaxError('Unterminated string at: ' + i);
|
---|
55 | return { value: value, end: i };
|
---|
56 | };
|
---|