index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __exportStar = (this && this.__exportStar) || function(m, exports) {
  14. for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
  15. };
  16. Object.defineProperty(exports, "__esModule", { value: true });
  17. exports.replaceRange = exports.replaceSourceRange = exports.replaceAll = exports.replace = exports.create = exports.toString = exports.getLength = void 0;
  18. const binarySearch_1 = require("./binarySearch");
  19. const track_1 = require("./track");
  20. __exportStar(require("./types"), exports);
  21. __exportStar(require("./track"), exports);
  22. function getLength(segments) {
  23. let length = 0;
  24. for (const segment of segments) {
  25. length += typeof segment == 'string' ? segment.length : segment[0].length;
  26. }
  27. return length;
  28. }
  29. exports.getLength = getLength;
  30. function toString(segments) {
  31. return segments.map(s => typeof s === 'string' ? s : s[0]).join('');
  32. }
  33. exports.toString = toString;
  34. function create(source) {
  35. return [[source, undefined, 0]];
  36. }
  37. exports.create = create;
  38. function replace(segments, pattern, ...replacers) {
  39. const str = toString(segments);
  40. const match = str.match(pattern);
  41. if (match && match.index !== undefined) {
  42. const startOffset = match.index;
  43. const endOffset = startOffset + match[0].length;
  44. (0, track_1.offsetStack)();
  45. replaceRange(segments, startOffset, endOffset, ...replacers.map(replacer => typeof replacer === 'function' ? replacer(match[0]) : replacer));
  46. (0, track_1.resetOffsetStack)();
  47. }
  48. }
  49. exports.replace = replace;
  50. function replaceAll(segments, pattern, ...replacers) {
  51. const str = toString(segments);
  52. const allMatch = str.matchAll(pattern);
  53. let length = str.length;
  54. let lengthDiff = 0;
  55. for (const match of allMatch) {
  56. if (match.index !== undefined) {
  57. const startOffset = match.index + lengthDiff;
  58. const endOffset = startOffset + match[0].length;
  59. (0, track_1.offsetStack)();
  60. replaceRange(segments, startOffset, endOffset, ...replacers.map(replacer => typeof replacer === 'function' ? replacer(match[0]) : replacer));
  61. (0, track_1.resetOffsetStack)();
  62. const newLength = getLength(segments);
  63. lengthDiff += newLength - length;
  64. length = newLength;
  65. }
  66. }
  67. }
  68. exports.replaceAll = replaceAll;
  69. function replaceSourceRange(segments, source, startOffset, endOffset, ...newSegments) {
  70. for (const segment of segments) {
  71. if (typeof segment === 'string') {
  72. continue;
  73. }
  74. if (segment[1] === source) {
  75. const segmentStart = typeof segment[2] === 'number' ? segment[2] : segment[2][0];
  76. const segmentEnd = typeof segment[2] === 'number' ? segment[2] + segment[0].length : segment[2][1];
  77. if (segmentStart <= startOffset && segmentEnd >= endOffset) {
  78. const inserts = [];
  79. if (startOffset > segmentStart) {
  80. inserts.push(trimSegmentEnd(segment, startOffset - segmentStart));
  81. }
  82. for (const newSegment of newSegments) {
  83. inserts.push(newSegment);
  84. }
  85. if (endOffset < segmentEnd) {
  86. inserts.push(trimSegmentStart(segment, endOffset - segmentEnd));
  87. }
  88. combineStrings(inserts);
  89. (0, track_1.offsetStack)();
  90. segments.splice(segments.indexOf(segment), 1, ...inserts);
  91. (0, track_1.resetOffsetStack)();
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. exports.replaceSourceRange = replaceSourceRange;
  99. function replaceRange(segments, startOffset, endOffset, ...newSegments) {
  100. const offsets = toOffsets(segments);
  101. const startIndex = (0, binarySearch_1.binarySearch)(offsets, startOffset);
  102. const endIndex = (0, binarySearch_1.binarySearch)(offsets, endOffset);
  103. const startSegment = segments[startIndex];
  104. const endSegment = segments[endIndex];
  105. const startSegmentStart = offsets[startIndex];
  106. const endSegmentStart = offsets[endIndex];
  107. const endSegmentEnd = offsets[endIndex] + (typeof endSegment === 'string' ? endSegment.length : endSegment[0].length);
  108. const inserts = [];
  109. if (startOffset > startSegmentStart) {
  110. inserts.push(trimSegmentEnd(startSegment, startOffset - startSegmentStart));
  111. }
  112. for (const newSegment of newSegments) {
  113. inserts.push(newSegment);
  114. }
  115. if (endOffset < endSegmentEnd) {
  116. inserts.push(trimSegmentStart(endSegment, endOffset - endSegmentStart));
  117. }
  118. combineStrings(inserts);
  119. (0, track_1.offsetStack)();
  120. segments.splice(startIndex, endIndex - startIndex + 1, ...inserts);
  121. (0, track_1.resetOffsetStack)();
  122. }
  123. exports.replaceRange = replaceRange;
  124. function combineStrings(segments) {
  125. for (let i = segments.length - 1; i >= 1; i--) {
  126. if (typeof segments[i] === 'string' && typeof segments[i - 1] === 'string') {
  127. segments[i - 1] = segments[i - 1] + segments[i];
  128. (0, track_1.offsetStack)();
  129. segments.splice(i, 1);
  130. (0, track_1.resetOffsetStack)();
  131. }
  132. }
  133. }
  134. function trimSegmentEnd(segment, trimEnd) {
  135. if (typeof segment === 'string') {
  136. return segment.slice(0, trimEnd);
  137. }
  138. const originalString = segment[0];
  139. const originalRange = segment[2];
  140. const newString = originalString.slice(0, trimEnd);
  141. const newRange = typeof originalRange === 'number' ? originalRange : [originalRange[0], originalRange[1] - (originalString.length - newString.length)];
  142. return [
  143. newString,
  144. segment[1],
  145. newRange,
  146. ...segment.slice(3),
  147. ];
  148. }
  149. function trimSegmentStart(segment, trimStart) {
  150. if (typeof segment === 'string') {
  151. return segment.slice(trimStart);
  152. }
  153. const originalString = segment[0];
  154. const originalRange = segment[2];
  155. const newString = originalString.slice(trimStart);
  156. if (trimStart < 0) {
  157. trimStart += originalString.length;
  158. }
  159. const newRange = typeof originalRange === 'number' ? originalRange + trimStart : [originalRange[0] + trimStart, originalRange[1]];
  160. return [
  161. newString,
  162. segment[1],
  163. newRange,
  164. ...segment.slice(3),
  165. ];
  166. }
  167. function toOffsets(segments) {
  168. const offsets = [];
  169. let offset = 0;
  170. for (const segment of segments) {
  171. offsets.push(offset);
  172. offset += typeof segment == 'string' ? segment.length : segment[0].length;
  173. }
  174. return offsets;
  175. }
  176. //# sourceMappingURL=index.js.map