es6.regexp.split.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 fails = require('./_fails');
  10. var $min = Math.min;
  11. var $push = [].push;
  12. var $SPLIT = 'split';
  13. var LENGTH = 'length';
  14. var LAST_INDEX = 'lastIndex';
  15. var MAX_UINT32 = 0xffffffff;
  16. // babel-minify transpiles RegExp('x', 'y') -> /x/y and it causes SyntaxError
  17. var SUPPORTS_Y = !fails(function () { RegExp(MAX_UINT32, 'y'); });
  18. // @@split logic
  19. require('./_fix-re-wks')('split', 2, function (defined, SPLIT, $split, maybeCallNative) {
  20. var internalSplit;
  21. if (
  22. 'abbc'[$SPLIT](/(b)*/)[1] == 'c' ||
  23. 'test'[$SPLIT](/(?:)/, -1)[LENGTH] != 4 ||
  24. 'ab'[$SPLIT](/(?:ab)*/)[LENGTH] != 2 ||
  25. '.'[$SPLIT](/(.?)(.?)/)[LENGTH] != 4 ||
  26. '.'[$SPLIT](/()()/)[LENGTH] > 1 ||
  27. ''[$SPLIT](/.?/)[LENGTH]
  28. ) {
  29. // based on es5-shim implementation, need to rework it
  30. internalSplit = function (separator, limit) {
  31. var string = String(this);
  32. if (separator === undefined && limit === 0) return [];
  33. // If `separator` is not a regex, use native split
  34. if (!isRegExp(separator)) return $split.call(string, separator, limit);
  35. var output = [];
  36. var flags = (separator.ignoreCase ? 'i' : '') +
  37. (separator.multiline ? 'm' : '') +
  38. (separator.unicode ? 'u' : '') +
  39. (separator.sticky ? 'y' : '');
  40. var lastLastIndex = 0;
  41. var splitLimit = limit === undefined ? MAX_UINT32 : limit >>> 0;
  42. // Make `global` and avoid `lastIndex` issues by working with a copy
  43. var separatorCopy = new RegExp(separator.source, flags + 'g');
  44. var match, lastIndex, lastLength;
  45. while (match = regexpExec.call(separatorCopy, string)) {
  46. lastIndex = separatorCopy[LAST_INDEX];
  47. if (lastIndex > lastLastIndex) {
  48. output.push(string.slice(lastLastIndex, match.index));
  49. if (match[LENGTH] > 1 && match.index < string[LENGTH]) $push.apply(output, match.slice(1));
  50. lastLength = match[0][LENGTH];
  51. lastLastIndex = lastIndex;
  52. if (output[LENGTH] >= splitLimit) break;
  53. }
  54. if (separatorCopy[LAST_INDEX] === match.index) separatorCopy[LAST_INDEX]++; // Avoid an infinite loop
  55. }
  56. if (lastLastIndex === string[LENGTH]) {
  57. if (lastLength || !separatorCopy.test('')) output.push('');
  58. } else output.push(string.slice(lastLastIndex));
  59. return output[LENGTH] > splitLimit ? output.slice(0, splitLimit) : output;
  60. };
  61. // Chakra, V8
  62. } else if ('0'[$SPLIT](undefined, 0)[LENGTH]) {
  63. internalSplit = function (separator, limit) {
  64. return separator === undefined && limit === 0 ? [] : $split.call(this, separator, limit);
  65. };
  66. } else {
  67. internalSplit = $split;
  68. }
  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 = defined(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 !== $split);
  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. });