1dee69fbae0efbace2d64dfbd8b6001c025422d1
[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 var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
25
26 var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED || UNSUPPORTED_Y;
27
28 if (PATCH) {
29   patchedExec = function exec(str) {
30     var re = this;
31     var lastIndex, reCopy, match, i;
32     var sticky = UNSUPPORTED_Y && re.sticky;
33     var flags = regexpFlags.call(re);
34     var source = re.source;
35     var charsAdded = 0;
36     var strCopy = str;
37
38     if (sticky) {
39       flags = flags.replace('y', '');
40       if (flags.indexOf('g') === -1) {
41         flags += 'g';
42       }
43
44       strCopy = String(str).slice(re.lastIndex);
45       // Support anchored sticky behavior.
46       if (re.lastIndex > 0 && (!re.multiline || re.multiline && str[re.lastIndex - 1] !== '\n')) {
47         source = '(?: ' + source + ')';
48         strCopy = ' ' + strCopy;
49         charsAdded++;
50       }
51       // ^(? + rx + ) is needed, in combination with some str slicing, to
52       // simulate the 'y' flag.
53       reCopy = new RegExp('^(?:' + source + ')', flags);
54     }
55
56     if (NPCG_INCLUDED) {
57       reCopy = new RegExp('^' + source + '$(?!\\s)', flags);
58     }
59     if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex;
60
61     match = nativeExec.call(sticky ? reCopy : re, strCopy);
62
63     if (sticky) {
64       if (match) {
65         match.input = match.input.slice(charsAdded);
66         match[0] = match[0].slice(charsAdded);
67         match.index = re.lastIndex;
68         re.lastIndex += match[0].length;
69       } else re.lastIndex = 0;
70     } else if (UPDATES_LAST_INDEX_WRONG && match) {
71       re.lastIndex = re.global ? match.index + match[0].length : lastIndex;
72     }
73     if (NPCG_INCLUDED && match && match.length > 1) {
74       // Fix browsers whose `exec` methods don't consistently return `undefined`
75       // for NPCG, like IE8. NOTE: This doesn' work for /(.?)?/
76       nativeReplace.call(match[0], reCopy, function () {
77         for (i = 1; i < arguments.length - 2; i++) {
78           if (arguments[i] === undefined) match[i] = undefined;
79         }
80       });
81     }
82
83     return match;
84   };
85 }
86
87 module.exports = patchedExec;