es.string.split.js 6.6 KB

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