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 'test'.split(/(?:)/, -1).length != 4 ||
26 'ab'.split(/(?:ab)*/).length != 2 ||
27 '.'.split(/(.?)(.?)/).length != 4 ||
28 '.'.split(/()()/).length > 1 ||
31 // based on es5-shim implementation, need to rework it
32 internalSplit = function (separator, limit) {
33 var string = String(requireObjectCoercible(this));
34 var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
35 if (lim === 0) return [];
36 if (separator === undefined) return [string];
37 // If `separator` is not a regex, use native split
38 if (!isRegExp(separator)) {
39 return nativeSplit.call(string, separator, lim);
42 var flags = (separator.ignoreCase ? 'i' : '') +
43 (separator.multiline ? 'm' : '') +
44 (separator.unicode ? 'u' : '') +
45 (separator.sticky ? 'y' : '');
46 var lastLastIndex = 0;
47 // Make `global` and avoid `lastIndex` issues by working with a copy
48 var separatorCopy = new RegExp(separator.source, flags + 'g');
49 var match, lastIndex, lastLength;
50 while (match = regexpExec.call(separatorCopy, string)) {
51 lastIndex = separatorCopy.lastIndex;
52 if (lastIndex > lastLastIndex) {
53 output.push(string.slice(lastLastIndex, match.index));
54 if (match.length > 1 && match.index < string.length) arrayPush.apply(output, match.slice(1));
55 lastLength = match[0].length;
56 lastLastIndex = lastIndex;
57 if (output.length >= lim) break;
59 if (separatorCopy.lastIndex === match.index) separatorCopy.lastIndex++; // Avoid an infinite loop
61 if (lastLastIndex === string.length) {
62 if (lastLength || !separatorCopy.test('')) output.push('');
63 } else output.push(string.slice(lastLastIndex));
64 return output.length > lim ? output.slice(0, lim) : output;
67 } else if ('0'.split(undefined, 0).length) {
68 internalSplit = function (separator, limit) {
69 return separator === undefined && limit === 0 ? [] : nativeSplit.call(this, separator, limit);
71 } else internalSplit = nativeSplit;
74 // `String.prototype.split` method
75 // https://tc39.github.io/ecma262/#sec-string.prototype.split
76 function split(separator, limit) {
77 var O = requireObjectCoercible(this);
78 var splitter = separator == undefined ? undefined : separator[SPLIT];
79 return splitter !== undefined
80 ? splitter.call(separator, O, limit)
81 : internalSplit.call(String(O), separator, limit);
83 // `RegExp.prototype[@@split]` method
84 // https://tc39.github.io/ecma262/#sec-regexp.prototype-@@split
86 // NOTE: This cannot be properly polyfilled in engines that don't support
88 function (regexp, limit) {
89 var res = maybeCallNative(internalSplit, regexp, this, limit, internalSplit !== nativeSplit);
90 if (res.done) return res.value;
92 var rx = anObject(regexp);
94 var C = speciesConstructor(rx, RegExp);
96 var unicodeMatching = rx.unicode;
97 var flags = (rx.ignoreCase ? 'i' : '') +
98 (rx.multiline ? 'm' : '') +
99 (rx.unicode ? 'u' : '') +
100 (SUPPORTS_Y ? 'y' : 'g');
102 // ^(? + rx + ) is needed, in combination with some S slicing, to
103 // simulate the 'y' flag.
104 var splitter = new C(SUPPORTS_Y ? rx : '^(?:' + rx.source + ')', flags);
105 var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
106 if (lim === 0) return [];
107 if (S.length === 0) return callRegExpExec(splitter, S) === null ? [S] : [];
111 while (q < S.length) {
112 splitter.lastIndex = SUPPORTS_Y ? q : 0;
113 var z = callRegExpExec(splitter, SUPPORTS_Y ? S : S.slice(q));
117 (e = min(toLength(splitter.lastIndex + (SUPPORTS_Y ? 0 : q)), S.length)) === p
119 q = advanceStringIndex(S, q, unicodeMatching);
121 A.push(S.slice(p, q));
122 if (A.length === lim) return A;
123 for (var i = 1; i <= z.length - 1; i++) {
125 if (A.length === lim) return A;