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