[79a0317] | 1 | 'use strict';
|
---|
| 2 | var call = require('../internals/function-call');
|
---|
| 3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
| 4 | var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
|
---|
| 5 | var anObject = require('../internals/an-object');
|
---|
| 6 | var isNullOrUndefined = require('../internals/is-null-or-undefined');
|
---|
| 7 | var requireObjectCoercible = require('../internals/require-object-coercible');
|
---|
| 8 | var speciesConstructor = require('../internals/species-constructor');
|
---|
| 9 | var advanceStringIndex = require('../internals/advance-string-index');
|
---|
| 10 | var toLength = require('../internals/to-length');
|
---|
| 11 | var toString = require('../internals/to-string');
|
---|
| 12 | var getMethod = require('../internals/get-method');
|
---|
| 13 | var regExpExec = require('../internals/regexp-exec-abstract');
|
---|
| 14 | var stickyHelpers = require('../internals/regexp-sticky-helpers');
|
---|
| 15 | var fails = require('../internals/fails');
|
---|
| 16 |
|
---|
| 17 | var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;
|
---|
| 18 | var MAX_UINT32 = 0xFFFFFFFF;
|
---|
| 19 | var min = Math.min;
|
---|
| 20 | var push = uncurryThis([].push);
|
---|
| 21 | var stringSlice = uncurryThis(''.slice);
|
---|
| 22 |
|
---|
| 23 | // Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
|
---|
| 24 | // Weex JS has frozen built-in prototypes, so use try / catch wrapper
|
---|
| 25 | var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
|
---|
| 26 | // eslint-disable-next-line regexp/no-empty-group -- required for testing
|
---|
| 27 | var re = /(?:)/;
|
---|
| 28 | var originalExec = re.exec;
|
---|
| 29 | re.exec = function () { return originalExec.apply(this, arguments); };
|
---|
| 30 | var result = 'ab'.split(re);
|
---|
| 31 | return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
|
---|
| 32 | });
|
---|
| 33 |
|
---|
| 34 | var BUGGY = 'abbc'.split(/(b)*/)[1] === 'c' ||
|
---|
| 35 | // eslint-disable-next-line regexp/no-empty-group -- required for testing
|
---|
| 36 | 'test'.split(/(?:)/, -1).length !== 4 ||
|
---|
| 37 | 'ab'.split(/(?:ab)*/).length !== 2 ||
|
---|
| 38 | '.'.split(/(.?)(.?)/).length !== 4 ||
|
---|
| 39 | // eslint-disable-next-line regexp/no-empty-capturing-group, regexp/no-empty-group -- required for testing
|
---|
| 40 | '.'.split(/()()/).length > 1 ||
|
---|
| 41 | ''.split(/.?/).length;
|
---|
| 42 |
|
---|
| 43 | // @@split logic
|
---|
| 44 | fixRegExpWellKnownSymbolLogic('split', function (SPLIT, nativeSplit, maybeCallNative) {
|
---|
| 45 | var internalSplit = '0'.split(undefined, 0).length ? function (separator, limit) {
|
---|
| 46 | return separator === undefined && limit === 0 ? [] : call(nativeSplit, this, separator, limit);
|
---|
| 47 | } : nativeSplit;
|
---|
| 48 |
|
---|
| 49 | return [
|
---|
| 50 | // `String.prototype.split` method
|
---|
| 51 | // https://tc39.es/ecma262/#sec-string.prototype.split
|
---|
| 52 | function split(separator, limit) {
|
---|
| 53 | var O = requireObjectCoercible(this);
|
---|
| 54 | var splitter = isNullOrUndefined(separator) ? undefined : getMethod(separator, SPLIT);
|
---|
| 55 | return splitter
|
---|
| 56 | ? call(splitter, separator, O, limit)
|
---|
| 57 | : call(internalSplit, toString(O), separator, limit);
|
---|
| 58 | },
|
---|
| 59 | // `RegExp.prototype[@@split]` method
|
---|
| 60 | // https://tc39.es/ecma262/#sec-regexp.prototype-@@split
|
---|
| 61 | //
|
---|
| 62 | // NOTE: This cannot be properly polyfilled in engines that don't support
|
---|
| 63 | // the 'y' flag.
|
---|
| 64 | function (string, limit) {
|
---|
| 65 | var rx = anObject(this);
|
---|
| 66 | var S = toString(string);
|
---|
| 67 |
|
---|
| 68 | if (!BUGGY) {
|
---|
| 69 | var res = maybeCallNative(internalSplit, rx, S, limit, internalSplit !== nativeSplit);
|
---|
| 70 | if (res.done) return res.value;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | var C = speciesConstructor(rx, RegExp);
|
---|
| 74 | var unicodeMatching = rx.unicode;
|
---|
| 75 | var flags = (rx.ignoreCase ? 'i' : '') +
|
---|
| 76 | (rx.multiline ? 'm' : '') +
|
---|
| 77 | (rx.unicode ? 'u' : '') +
|
---|
| 78 | (UNSUPPORTED_Y ? 'g' : 'y');
|
---|
| 79 | // ^(? + rx + ) is needed, in combination with some S slicing, to
|
---|
| 80 | // simulate the 'y' flag.
|
---|
| 81 | var splitter = new C(UNSUPPORTED_Y ? '^(?:' + rx.source + ')' : rx, flags);
|
---|
| 82 | var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
|
---|
| 83 | if (lim === 0) return [];
|
---|
| 84 | if (S.length === 0) return regExpExec(splitter, S) === null ? [S] : [];
|
---|
| 85 | var p = 0;
|
---|
| 86 | var q = 0;
|
---|
| 87 | var A = [];
|
---|
| 88 | while (q < S.length) {
|
---|
| 89 | splitter.lastIndex = UNSUPPORTED_Y ? 0 : q;
|
---|
| 90 | var z = regExpExec(splitter, UNSUPPORTED_Y ? stringSlice(S, q) : S);
|
---|
| 91 | var e;
|
---|
| 92 | if (
|
---|
| 93 | z === null ||
|
---|
| 94 | (e = min(toLength(splitter.lastIndex + (UNSUPPORTED_Y ? q : 0)), S.length)) === p
|
---|
| 95 | ) {
|
---|
| 96 | q = advanceStringIndex(S, q, unicodeMatching);
|
---|
| 97 | } else {
|
---|
| 98 | push(A, stringSlice(S, p, q));
|
---|
| 99 | if (A.length === lim) return A;
|
---|
| 100 | for (var i = 1; i <= z.length - 1; i++) {
|
---|
| 101 | push(A, z[i]);
|
---|
| 102 | if (A.length === lim) return A;
|
---|
| 103 | }
|
---|
| 104 | q = p = e;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | push(A, stringSlice(S, p));
|
---|
| 108 | return A;
|
---|
| 109 | }
|
---|
| 110 | ];
|
---|
| 111 | }, BUGGY || !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC, UNSUPPORTED_Y);
|
---|