2 var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
3 var isRegExp = require('../internals/is-regexp');
4 var anObject = require('../internals/an-object');
5 var requireObjectCoercible = require('../internals/require-object-coercible');
6 var speciesConstructor = require('../internals/species-constructor');
7 var advanceStringIndex = require('../internals/advance-string-index');
8 var toLength = require('../internals/to-length');
9 var callRegExpExec = require('../internals/regexp-exec-abstract');
10 var regexpExec = require('../internals/regexp-exec');
11 var fails = require('../internals/fails');
13 var arrayPush = [].push;
15 var MAX_UINT32 = 0xFFFFFFFF;
17 // babel-minify transpiles RegExp('x', 'y') -> /x/y and it causes SyntaxError
18 var SUPPORTS_Y = !fails(function () { return !RegExp(MAX_UINT32, 'y'); });
21 fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCallNative) {
24 'abbc'.split(/(b)*/)[1] == 'c' ||
25 // eslint-disable-next-line regexp/no-empty-group -- required for testing
26 'test'.split(/(?:)/, -1).length != 4 ||
27 'ab'.split(/(?:ab)*/).length != 2 ||
28 '.'.split(/(.?)(.?)/).length != 4 ||
29 // eslint-disable-next-line regexp/no-assertion-capturing-group, regexp/no-empty-group -- required for testing
30 '.'.split(/()()/).length > 1 ||
33 // based on es5-shim implementation, need to rework it
34 internalSplit = function (separator, limit) {
35 var string = String(requireObjectCoercible(this));
36 var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
37 if (lim === 0) return [];
38 if (separator === undefined) return [string];
39 // If `separator` is not a regex, use native split
40 if (!isRegExp(separator)) {
41 return nativeSplit.call(string, separator, lim);
44 var flags = (separator.ignoreCase ? 'i' : '') +
45 (separator.multiline ? 'm' : '') +
46 (separator.unicode ? 'u' : '') +
47 (separator.sticky ? 'y' : '');
48 var lastLastIndex = 0;
49 // Make `global` and avoid `lastIndex` issues by working with a copy
50 var separatorCopy = new RegExp(separator.source, flags + 'g');
51 var match, lastIndex, lastLength;
52 while (match = regexpExec.call(separatorCopy, string)) {
53 lastIndex = separatorCopy.lastIndex;
54 if (lastIndex > lastLastIndex) {
55 output.push(string.slice(lastLastIndex, match.index));
56 if (match.length > 1 && match.index < string.length) arrayPush.apply(output, match.slice(1));
57 lastLength = match[0].length;
58 lastLastIndex = lastIndex;
59 if (output.length >= lim) break;
61 if (separatorCopy.lastIndex === match.index) separatorCopy.lastIndex++; // Avoid an infinite loop
63 if (lastLastIndex === string.length) {
64 if (lastLength || !separatorCopy.test('')) output.push('');
65 } else output.push(string.slice(lastLastIndex));
66 return output.length > lim ? output.slice(0, lim) : output;
69 } else if ('0'.split(undefined, 0).length) {
70 internalSplit = function (separator, limit) {
71 return separator === undefined && limit === 0 ? [] : nativeSplit.call(this, separator, limit);
73 } else internalSplit = nativeSplit;
76 // `String.prototype.split` method
77 // https://tc39.es/ecma262/#sec-string.prototype.split
78 function split(separator, limit) {
79 var O = requireObjectCoercible(this);
80 var splitter = separator == undefined ? undefined : separator[SPLIT];
81 return splitter !== undefined
82 ? splitter.call(separator, O, limit)
83 : internalSplit.call(String(O), separator, limit);
85 // `RegExp.prototype[@@split]` method
86 // https://tc39.es/ecma262/#sec-regexp.prototype-@@split
88 // NOTE: This cannot be properly polyfilled in engines that don't support
90 function (regexp, limit) {
91 var res = maybeCallNative(internalSplit, regexp, this, limit, internalSplit !== nativeSplit);
92 if (res.done) return res.value;
94 var rx = anObject(regexp);
96 var C = speciesConstructor(rx, RegExp);
98 var unicodeMatching = rx.unicode;
99 var flags = (rx.ignoreCase ? 'i' : '') +
100 (rx.multiline ? 'm' : '') +
101 (rx.unicode ? 'u' : '') +
102 (SUPPORTS_Y ? 'y' : 'g');
104 // ^(? + rx + ) is needed, in combination with some S slicing, to
105 // simulate the 'y' flag.
106 var splitter = new C(SUPPORTS_Y ? rx : '^(?:' + rx.source + ')', flags);
107 var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
108 if (lim === 0) return [];
109 if (S.length === 0) return callRegExpExec(splitter, S) === null ? [S] : [];
113 while (q < S.length) {
114 splitter.lastIndex = SUPPORTS_Y ? q : 0;
115 var z = callRegExpExec(splitter, SUPPORTS_Y ? S : S.slice(q));
119 (e = min(toLength(splitter.lastIndex + (SUPPORTS_Y ? 0 : q)), S.length)) === p
121 q = advanceStringIndex(S, q, unicodeMatching);
123 A.push(S.slice(p, q));
124 if (A.length === lim) return A;
125 for (var i = 1; i <= z.length - 1; i++) {
127 if (A.length === lim) return A;