InputBuffer.js
3 months ago
InputBuffer.js.map
3 months ago
OutputBuffer.js
3 months ago
OutputBuffer.js.map
3 months ago
OutputBuffer.js
111 lines
| 1 | import { isWhitespace } from '../../utils/stringUtils.js'; |
| 2 | export function createOutputBuffer(_ref) { |
| 3 | let { |
| 4 | write, |
| 5 | chunkSize, |
| 6 | bufferSize |
| 7 | } = _ref; |
| 8 | let buffer = ''; |
| 9 | let offset = 0; |
| 10 | function flushChunks() { |
| 11 | let minSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : bufferSize; |
| 12 | while (buffer.length >= minSize + chunkSize) { |
| 13 | const chunk = buffer.substring(0, chunkSize); |
| 14 | write(chunk); |
| 15 | offset += chunkSize; |
| 16 | buffer = buffer.substring(chunkSize); |
| 17 | } |
| 18 | } |
| 19 | function flush() { |
| 20 | flushChunks(0); |
| 21 | if (buffer.length > 0) { |
| 22 | write(buffer); |
| 23 | offset += buffer.length; |
| 24 | buffer = ''; |
| 25 | } |
| 26 | } |
| 27 | function push(text) { |
| 28 | buffer += text; |
| 29 | flushChunks(); |
| 30 | } |
| 31 | function unshift(text) { |
| 32 | if (offset > 0) { |
| 33 | throw new Error("Cannot unshift: ".concat(flushedMessage)); |
| 34 | } |
| 35 | buffer = text + buffer; |
| 36 | flushChunks(); |
| 37 | } |
| 38 | function remove(start, end) { |
| 39 | if (start < offset) { |
| 40 | throw new Error("Cannot remove: ".concat(flushedMessage)); |
| 41 | } |
| 42 | if (end !== undefined) { |
| 43 | buffer = buffer.substring(0, start - offset) + buffer.substring(end - offset); |
| 44 | } else { |
| 45 | buffer = buffer.substring(0, start - offset); |
| 46 | } |
| 47 | } |
| 48 | function insertAt(index, text) { |
| 49 | if (index < offset) { |
| 50 | throw new Error("Cannot insert: ".concat(flushedMessage)); |
| 51 | } |
| 52 | buffer = buffer.substring(0, index - offset) + text + buffer.substring(index - offset); |
| 53 | } |
| 54 | function length() { |
| 55 | return offset + buffer.length; |
| 56 | } |
| 57 | function stripLastOccurrence(textToStrip) { |
| 58 | let stripRemainingText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; |
| 59 | const bufferIndex = buffer.lastIndexOf(textToStrip); |
| 60 | if (bufferIndex !== -1) { |
| 61 | if (stripRemainingText) { |
| 62 | buffer = buffer.substring(0, bufferIndex); |
| 63 | } else { |
| 64 | buffer = buffer.substring(0, bufferIndex) + buffer.substring(bufferIndex + textToStrip.length); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | function insertBeforeLastWhitespace(textToInsert) { |
| 69 | let bufferIndex = buffer.length; // index relative to the start of the buffer, not taking `offset` into account |
| 70 | |
| 71 | if (!isWhitespace(buffer.charCodeAt(bufferIndex - 1))) { |
| 72 | // no trailing whitespaces |
| 73 | push(textToInsert); |
| 74 | return; |
| 75 | } |
| 76 | while (isWhitespace(buffer.charCodeAt(bufferIndex - 1))) { |
| 77 | bufferIndex--; |
| 78 | } |
| 79 | if (bufferIndex <= 0) { |
| 80 | throw new Error("Cannot insert: ".concat(flushedMessage)); |
| 81 | } |
| 82 | buffer = buffer.substring(0, bufferIndex) + textToInsert + buffer.substring(bufferIndex); |
| 83 | flushChunks(); |
| 84 | } |
| 85 | function endsWithIgnoringWhitespace(char) { |
| 86 | let i = buffer.length - 1; |
| 87 | while (i > 0) { |
| 88 | if (char === buffer.charAt(i)) { |
| 89 | return true; |
| 90 | } |
| 91 | if (!isWhitespace(buffer.charCodeAt(i))) { |
| 92 | return false; |
| 93 | } |
| 94 | i--; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | return { |
| 99 | push, |
| 100 | unshift, |
| 101 | remove, |
| 102 | insertAt, |
| 103 | length, |
| 104 | flush, |
| 105 | stripLastOccurrence, |
| 106 | insertBeforeLastWhitespace, |
| 107 | endsWithIgnoringWhitespace |
| 108 | }; |
| 109 | } |
| 110 | const flushedMessage = 'start of the output is already flushed from the buffer'; |
| 111 | //# sourceMappingURL=OutputBuffer.js.map |