1 | 'use strict';
|
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');
|
12 |
|
13 | var arrayPush = [].push;
|
14 | var min = Math.min;
|
15 | var MAX_UINT32 = 0xFFFFFFFF;
|
16 |
|
17 |
|
18 | var SUPPORTS_Y = !fails(function () { return !RegExp(MAX_UINT32, 'y'); });
|
19 |
|
20 |
|
21 | fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCallNative) {
|
22 | var internalSplit;
|
23 | if (
|
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 ||
|
29 | ''.split(/.?/).length
|
30 | ) {
|
31 |
|
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 |
|
38 | if (!isRegExp(separator)) {
|
39 | return nativeSplit.call(string, separator, lim);
|
40 | }
|
41 | var output = [];
|
42 | var flags = (separator.ignoreCase ? 'i' : '') +
|
43 | (separator.multiline ? 'm' : '') +
|
44 | (separator.unicode ? 'u' : '') +
|
45 | (separator.sticky ? 'y' : '');
|
46 | var lastLastIndex = 0;
|
47 |
|
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;
|
58 | }
|
59 | if (separatorCopy.lastIndex === match.index) separatorCopy.lastIndex++;
|
60 | }
|
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;
|
65 | };
|
66 |
|
67 | } else if ('0'.split(undefined, 0).length) {
|
68 | internalSplit = function (separator, limit) {
|
69 | return separator === undefined && limit === 0 ? [] : nativeSplit.call(this, separator, limit);
|
70 | };
|
71 | } else internalSplit = nativeSplit;
|
72 |
|
73 | return [
|
74 |
|
75 |
|
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);
|
82 | },
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function (regexp, limit) {
|
89 | var res = maybeCallNative(internalSplit, regexp, this, limit, internalSplit !== nativeSplit);
|
90 | if (res.done) return res.value;
|
91 |
|
92 | var rx = anObject(regexp);
|
93 | var S = String(this);
|
94 | var C = speciesConstructor(rx, RegExp);
|
95 |
|
96 | var unicodeMatching = rx.unicode;
|
97 | var flags = (rx.ignoreCase ? 'i' : '') +
|
98 | (rx.multiline ? 'm' : '') +
|
99 | (rx.unicode ? 'u' : '') +
|
100 | (SUPPORTS_Y ? 'y' : 'g');
|
101 |
|
102 |
|
103 |
|
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] : [];
|
108 | var p = 0;
|
109 | var q = 0;
|
110 | var A = [];
|
111 | while (q < S.length) {
|
112 | splitter.lastIndex = SUPPORTS_Y ? q : 0;
|
113 | var z = callRegExpExec(splitter, SUPPORTS_Y ? S : S.slice(q));
|
114 | var e;
|
115 | if (
|
116 | z === null ||
|
117 | (e = min(toLength(splitter.lastIndex + (SUPPORTS_Y ? 0 : q)), S.length)) === p
|
118 | ) {
|
119 | q = advanceStringIndex(S, q, unicodeMatching);
|
120 | } else {
|
121 | A.push(S.slice(p, q));
|
122 | if (A.length === lim) return A;
|
123 | for (var i = 1; i <= z.length - 1; i++) {
|
124 | A.push(z[i]);
|
125 | if (A.length === lim) return A;
|
126 | }
|
127 | q = p = e;
|
128 | }
|
129 | }
|
130 | A.push(S.slice(p));
|
131 | return A;
|
132 | }
|
133 | ];
|
134 | }, !SUPPORTS_Y);
|