es6.regexp.split.js 5.0 KB

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