es.string.split.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. var arrayPush = [].push;
  13. var min = Math.min;
  14. var MAX_UINT32 = 0xFFFFFFFF;
  15. // babel-minify transpiles RegExp('x', 'y') -> /x/y and it causes SyntaxError
  16. var SUPPORTS_Y = !fails(function () { return !RegExp(MAX_UINT32, 'y'); });
  17. // @@split logic
  18. fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCallNative) {
  19. var internalSplit;
  20. if (
  21. 'abbc'.split(/(b)*/)[1] == 'c' ||
  22. 'test'.split(/(?:)/, -1).length != 4 ||
  23. 'ab'.split(/(?:ab)*/).length != 2 ||
  24. '.'.split(/(.?)(.?)/).length != 4 ||
  25. '.'.split(/()()/).length > 1 ||
  26. ''.split(/.?/).length
  27. ) {
  28. // based on es5-shim implementation, need to rework it
  29. internalSplit = function (separator, limit) {
  30. var string = String(requireObjectCoercible(this));
  31. var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
  32. if (lim === 0) return [];
  33. if (separator === undefined) return [string];
  34. // If `separator` is not a regex, use native split
  35. if (!isRegExp(separator)) {
  36. return nativeSplit.call(string, separator, lim);
  37. }
  38. var output = [];
  39. var flags = (separator.ignoreCase ? 'i' : '') +
  40. (separator.multiline ? 'm' : '') +
  41. (separator.unicode ? 'u' : '') +
  42. (separator.sticky ? 'y' : '');
  43. var lastLastIndex = 0;
  44. // Make `global` and avoid `lastIndex` issues by working with a copy
  45. var separatorCopy = new RegExp(separator.source, flags + 'g');
  46. var match, lastIndex, lastLength;
  47. while (match = regexpExec.call(separatorCopy, string)) {
  48. lastIndex = separatorCopy.lastIndex;
  49. if (lastIndex > lastLastIndex) {
  50. output.push(string.slice(lastLastIndex, match.index));
  51. if (match.length > 1 && match.index < string.length) arrayPush.apply(output, match.slice(1));
  52. lastLength = match[0].length;
  53. lastLastIndex = lastIndex;
  54. if (output.length >= lim) break;
  55. }
  56. if (separatorCopy.lastIndex === match.index) separatorCopy.lastIndex++; // Avoid an infinite loop
  57. }
  58. if (lastLastIndex === string.length) {
  59. if (lastLength || !separatorCopy.test('')) output.push('');
  60. } else output.push(string.slice(lastLastIndex));
  61. return output.length > lim ? output.slice(0, lim) : output;
  62. };
  63. // Chakra, V8
  64. } else if ('0'.split(undefined, 0).length) {
  65. internalSplit = function (separator, limit) {
  66. return separator === undefined && limit === 0 ? [] : nativeSplit.call(this, separator, limit);
  67. };
  68. } else internalSplit = nativeSplit;
  69. return [
  70. // `String.prototype.split` method
  71. // https://tc39.github.io/ecma262/#sec-string.prototype.split
  72. function split(separator, limit) {
  73. var O = requireObjectCoercible(this);
  74. var splitter = separator == undefined ? undefined : separator[SPLIT];
  75. return splitter !== undefined
  76. ? splitter.call(separator, O, limit)
  77. : internalSplit.call(String(O), separator, limit);
  78. },
  79. // `RegExp.prototype[@@split]` method
  80. // https://tc39.github.io/ecma262/#sec-regexp.prototype-@@split
  81. //
  82. // NOTE: This cannot be properly polyfilled in engines that don't support
  83. // the 'y' flag.
  84. function (regexp, limit) {
  85. var res = maybeCallNative(internalSplit, regexp, this, limit, internalSplit !== nativeSplit);
  86. if (res.done) return res.value;
  87. var rx = anObject(regexp);
  88. var S = String(this);
  89. var C = speciesConstructor(rx, RegExp);
  90. var unicodeMatching = rx.unicode;
  91. var flags = (rx.ignoreCase ? 'i' : '') +
  92. (rx.multiline ? 'm' : '') +
  93. (rx.unicode ? 'u' : '') +
  94. (SUPPORTS_Y ? 'y' : 'g');
  95. // ^(? + rx + ) is needed, in combination with some S slicing, to
  96. // simulate the 'y' flag.
  97. var splitter = new C(SUPPORTS_Y ? rx : '^(?:' + rx.source + ')', flags);
  98. var lim = limit === undefined ? MAX_UINT32 : limit >>> 0;
  99. if (lim === 0) return [];
  100. if (S.length === 0) return callRegExpExec(splitter, S) === null ? [S] : [];
  101. var p = 0;
  102. var q = 0;
  103. var A = [];
  104. while (q < S.length) {
  105. splitter.lastIndex = SUPPORTS_Y ? q : 0;
  106. var z = callRegExpExec(splitter, SUPPORTS_Y ? S : S.slice(q));
  107. var e;
  108. if (
  109. z === null ||
  110. (e = min(toLength(splitter.lastIndex + (SUPPORTS_Y ? 0 : q)), S.length)) === p
  111. ) {
  112. q = advanceStringIndex(S, q, unicodeMatching);
  113. } else {
  114. A.push(S.slice(p, q));
  115. if (A.length === lim) return A;
  116. for (var i = 1; i <= z.length - 1; i++) {
  117. A.push(z[i]);
  118. if (A.length === lim) return A;
  119. }
  120. q = p = e;
  121. }
  122. }
  123. A.push(S.slice(p));
  124. return A;
  125. }
  126. ];
  127. }, !SUPPORTS_Y);