2 var apply = require('../internals/function-apply');
3 var call = require('../internals/function-call');
4 var uncurryThis = require('../internals/function-uncurry-this');
5 var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
6 var isRegExp = require('../internals/is-regexp');
7 var anObject = require('../internals/an-object');
8 var requireObjectCoercible = require('../internals/require-object-coercible');
9 var speciesConstructor = require('../internals/species-constructor');
10 var advanceStringIndex = require('../internals/advance-string-index');
11 var toLength = require('../internals/to-length');
12 var toString = require('../internals/to-string');
13 var getMethod = require('../internals/get-method');
14 var arraySlice = require('../internals/array-slice-simple');
15 var callRegExpExec = require('../internals/regexp-exec-abstract');
16 var regexpExec = require('../internals/regexp-exec');
17 var stickyHelpers = require('../internals/regexp-sticky-helpers');
18 var fails = require('../internals/fails');
20 var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;
21 var MAX_UINT32 = 0xFFFFFFFF;
24 var exec = uncurryThis(/./.exec);
25 var push = uncurryThis($push);
26 var stringSlice = uncurryThis(''.slice);
28 // Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
29 // Weex JS has frozen built-in prototypes, so use try / catch wrapper
30 var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
31 // eslint-disable-next-line regexp/no-empty-group -- required for testing
33 var originalExec = re.exec;
34 re.exec = function () { return originalExec.apply(this, arguments); };
35 var result = 'ab'.split(re);
36 return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
40 fixRegExpWellKnownSymbolLogic('split', function (SPLIT, nativeSplit, maybeCallNative) {
43 'abbc'.split(/(b)*/)[1] == 'c' ||
44 // eslint-disable-next-line regexp/no-empty-group -- required for testing
45 'test'.split(/(?:)/, -1).length != 4 ||
46 'ab'.split(/(?:ab)*/).length != 2 ||
47 '.'.split(/(.?)(.?)/).length != 4 ||
48 // eslint-disable-next-line regexp/no-empty-capturing-group, regexp/no-empty-group -- required for testing
49 '.'.split(/()()/).length > 1 ||
52 // based on es5-shim implementation, need to rework it
53 internalSplit = function (separator, limit) {
54 var string = toString(requireObjectCoercible(this));
55 var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
56 if (lim === 0) return [];
57 if (separator === undefined) return [string];
58 // If `separator` is not a regex, use native split
59 if (!isRegExp(separator)) {
60 return call(nativeSplit, string, separator, lim);
63 var flags = (separator.ignoreCase ? 'i' : '') +
64 (separator.multiline ? 'm' : '') +
65 (separator.unicode ? 'u' : '') +
66 (separator.sticky ? 'y' : '');
67 var lastLastIndex = 0;
68 // Make `global` and avoid `lastIndex` issues by working with a copy
69 var separatorCopy = new RegExp(separator.source, flags + 'g');
70 var match, lastIndex, lastLength;
71 while (match = call(regexpExec, separatorCopy, string)) {
72 lastIndex = separatorCopy.lastIndex;
73 if (lastIndex > lastLastIndex) {
74 push(output, stringSlice(string, lastLastIndex, match.index));
75 if (match.length > 1 && match.index < string.length) apply($push, output, arraySlice(match, 1));
76 lastLength = match[0].length;
77 lastLastIndex = lastIndex;
78 if (output.length >= lim) break;
80 if (separatorCopy.lastIndex === match.index) separatorCopy.lastIndex++; // Avoid an infinite loop
82 if (lastLastIndex === string.length) {
83 if (lastLength || !exec(separatorCopy, '')) push(output, '');
84 } else push(output, stringSlice(string, lastLastIndex));
85 return output.length > lim ? arraySlice(output, 0, lim) : output;
88 } else if ('0'.split(undefined, 0).length) {
89 internalSplit = function (separator, limit) {
90 return separator === undefined && limit === 0 ? [] : call(nativeSplit, this, separator, limit);
92 } else internalSplit = nativeSplit;
95 // `String.prototype.split` method
96 // https://tc39.es/ecma262/#sec-string.prototype.split
97 function split(separator, limit) {
98 var O = requireObjectCoercible(this);
99 var splitter = separator == undefined ? undefined : getMethod(separator, SPLIT);
101 ? call(splitter, separator, O, limit)
102 : call(internalSplit, toString(O), separator, limit);
104 // `RegExp.prototype[@@split]` method
105 // https://tc39.es/ecma262/#sec-regexp.prototype-@@split
107 // NOTE: This cannot be properly polyfilled in engines that don't support
109 function (string, limit) {
110 var rx = anObject(this);
111 var S = toString(string);
112 var res = maybeCallNative(internalSplit, rx, S, limit, internalSplit !== nativeSplit);
114 if (res.done) return res.value;
116 var C = speciesConstructor(rx, RegExp);
118 var unicodeMatching = rx.unicode;
119 var flags = (rx.ignoreCase ? 'i' : '') +
120 (rx.multiline ? 'm' : '') +
121 (rx.unicode ? 'u' : '') +
122 (UNSUPPORTED_Y ? 'g' : 'y');
124 // ^(? + rx + ) is needed, in combination with some S slicing, to
125 // simulate the 'y' flag.
126 var splitter = new C(UNSUPPORTED_Y ? '^(?:' + rx.source + ')' : rx, flags);
127 var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
128 if (lim === 0) return [];
129 if (S.length === 0) return callRegExpExec(splitter, S) === null ? [S] : [];
133 while (q < S.length) {
134 splitter.lastIndex = UNSUPPORTED_Y ? 0 : q;
135 var z = callRegExpExec(splitter, UNSUPPORTED_Y ? stringSlice(S, q) : S);
139 (e = min(toLength(splitter.lastIndex + (UNSUPPORTED_Y ? q : 0)), S.length)) === p
141 q = advanceStringIndex(S, q, unicodeMatching);
143 push(A, stringSlice(S, p, q));
144 if (A.length === lim) return A;
145 for (var i = 1; i <= z.length - 1; i++) {
147 if (A.length === lim) return A;
152 push(A, stringSlice(S, p));
156 }, !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC, UNSUPPORTED_Y);