es.string.split.js 6.5 KB

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