We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 10eb39a commit 80c4703Copy full SHA for 80c4703
JavaScript/3-ring.js
@@ -11,14 +11,11 @@ class RingBuffer {
11
const { size, offset } = this;
12
const { length } = data;
13
const available = size - offset;
14
- const end = Math.min(offset + length, size + 1);
+ const len = Math.min(available, size, length);
15
const rest = available - length;
16
- let position = 0;
17
- for (let i = offset; i < end; i++) {
18
- this.buffer[i] = data.charCodeAt(position++);
19
- }
20
- this.offset += position;
21
- if (this.offset > size) this.offset = 0;
+ this.buffer.write(data, offset, len);
+ this.offset += len;
+ if (this.offset === size) this.offset = 0;
22
if (rest < 0) this.write(data.slice(rest));
23
}
24
0 commit comments