.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / core-js / internals / regexp-exec.js
1 'use strict';
2 var regexpFlags = require('./regexp-flags');
3 var stickyHelpers = require('./regexp-sticky-helpers');
4
5 var nativeExec = RegExp.prototype.exec;
6 // This always refers to the native implementation, because the
7 // String#replace polyfill uses ./fix-regexp-well-known-symbol-logic.js,
8 // which loads this file before patching the method.
9 var nativeReplace = String.prototype.replace;
10
11 var patchedExec = nativeExec;
12
13 var UPDATES_LAST_INDEX_WRONG = (function () {
14   var re1 = /a/;
15   var re2 = /b*/g;
16   nativeExec.call(re1, 'a');
17   nativeExec.call(re2, 'a');
18   return re1.lastIndex !== 0 || re2.lastIndex !== 0;
19 })();
20
21 var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y || stickyHelpers.BROKEN_CARET;
22
23 // nonparticipating capturing group, copied from es5-shim's String#split patch.
24 // eslint-disable-next-line regexp/no-assertion-capturing-group, regexp/no-empty-group -- required for testing
25 var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
26
27 var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED || UNSUPPORTED_Y;
28
29 if (PATCH) {
30   patchedExec = function exec(str) {
31     var re = this;
32     var lastIndex, reCopy, match, i;
33     var sticky = UNSUPPORTED_Y && re.sticky;
34     var flags = regexpFlags.call(re);
35     var source = re.source;
36     var charsAdded = 0;
37     var strCopy = str;
38
39     if (sticky) {
40       flags = flags.replace('y', '');
41       if (flags.indexOf('g') === -1) {
42         flags += 'g';
43       }
44
45       strCopy = String(str).slice(re.lastIndex);
46       // Support anchored sticky behavior.
47       if (re.lastIndex > 0 && (!re.multiline || re.multiline && str[re.lastIndex - 1] !== '\n')) {
48         source = '(?: ' + source + ')';
49         strCopy = ' ' + strCopy;
50         charsAdded++;
51       }
52       // ^(? + rx + ) is needed, in combination with some str slicing, to
53       // simulate the 'y' flag.
54       reCopy = new RegExp('^(?:' + source + ')', flags);
55     }
56
57     if (NPCG_INCLUDED) {
58       reCopy = new RegExp('^' + source + '$(?!\\s)', flags);
59     }
60     if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex;
61
62     match = nativeExec.call(sticky ? reCopy : re, strCopy);
63
64     if (sticky) {
65       if (match) {
66         match.input = match.input.slice(charsAdded);
67         match[0] = match[0].slice(charsAdded);
68         match.index = re.lastIndex;
69         re.lastIndex += match[0].length;
70       } else re.lastIndex = 0;
71     } else if (UPDATES_LAST_INDEX_WRONG && match) {
72       re.lastIndex = re.global ? match.index + match[0].length : lastIndex;
73     }
74     if (NPCG_INCLUDED && match && match.length > 1) {
75       // Fix browsers whose `exec` methods don't consistently return `undefined`
76       // for NPCG, like IE8. NOTE: This doesn' work for /(.?)?/
77       nativeReplace.call(match[0], reCopy, function () {
78         for (i = 1; i < arguments.length - 2; i++) {
79           if (arguments[i] === undefined) match[i] = undefined;
80         }
81       });
82     }
83
84     return match;
85   };
86 }
87
88 module.exports = patchedExec;