InputBuffer.js
3 months ago
InputBuffer.js.map
3 months ago
OutputBuffer.js
3 months ago
OutputBuffer.js.map
3 months ago
InputBuffer.js
69 lines
| 1 | export function createInputBuffer() { |
| 2 | let buffer = ''; |
| 3 | let offset = 0; |
| 4 | let currentLength = 0; |
| 5 | let closed = false; |
| 6 | function ensure(index) { |
| 7 | if (index < offset) { |
| 8 | throw new Error("".concat(indexOutOfRangeMessage, " (index: ").concat(index, ", offset: ").concat(offset, ")")); |
| 9 | } |
| 10 | if (index >= currentLength) { |
| 11 | if (!closed) { |
| 12 | throw new Error("".concat(indexOutOfRangeMessage, " (index: ").concat(index, ")")); |
| 13 | } |
| 14 | } |
| 15 | } |
| 16 | function push(chunk) { |
| 17 | buffer += chunk; |
| 18 | currentLength += chunk.length; |
| 19 | } |
| 20 | function flush(position) { |
| 21 | if (position > currentLength) { |
| 22 | return; |
| 23 | } |
| 24 | buffer = buffer.substring(position - offset); |
| 25 | offset = position; |
| 26 | } |
| 27 | function charAt(index) { |
| 28 | ensure(index); |
| 29 | return buffer.charAt(index - offset); |
| 30 | } |
| 31 | function charCodeAt(index) { |
| 32 | ensure(index); |
| 33 | return buffer.charCodeAt(index - offset); |
| 34 | } |
| 35 | function substring(start, end) { |
| 36 | ensure(end - 1); // -1 because end is excluded |
| 37 | ensure(start); |
| 38 | return buffer.slice(start - offset, end - offset); |
| 39 | } |
| 40 | function length() { |
| 41 | if (!closed) { |
| 42 | throw new Error('Cannot get length: input is not yet closed'); |
| 43 | } |
| 44 | return currentLength; |
| 45 | } |
| 46 | function isEnd(index) { |
| 47 | if (!closed) { |
| 48 | ensure(index); |
| 49 | } |
| 50 | return index >= currentLength; |
| 51 | } |
| 52 | function close() { |
| 53 | closed = true; |
| 54 | } |
| 55 | return { |
| 56 | push, |
| 57 | flush, |
| 58 | charAt, |
| 59 | charCodeAt, |
| 60 | substring, |
| 61 | length, |
| 62 | currentLength: () => currentLength, |
| 63 | currentBufferSize: () => buffer.length, |
| 64 | isEnd, |
| 65 | close |
| 66 | }; |
| 67 | } |
| 68 | const indexOutOfRangeMessage = 'Index out of range, please configure a larger buffer size'; |
| 69 | //# sourceMappingURL=InputBuffer.js.map |