aboutsummaryrefslogtreecommitdiff
path: root/vendor/libghostty-vt/src/datastruct/circ_buf.zig
diff options
context:
space:
mode:
authorCan Celik <ogulcancelik@gmail.com>2026-09-10 21:51:57 +0300
committerGitHub <noreply@github.com>2026-09-10 21:51:57 +0300
commit425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch)
treee52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/datastruct/circ_buf.zig
parent90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff)
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/datastruct/circ_buf.zig')
-rw-r--r--vendor/libghostty-vt/src/datastruct/circ_buf.zig67
1 files changed, 61 insertions, 6 deletions
diff --git a/vendor/libghostty-vt/src/datastruct/circ_buf.zig b/vendor/libghostty-vt/src/datastruct/circ_buf.zig
index 3e373cb9..41a16832 100644
--- a/vendor/libghostty-vt/src/datastruct/circ_buf.zig
+++ b/vendor/libghostty-vt/src/datastruct/circ_buf.zig
@@ -167,18 +167,18 @@ pub fn CircBuf(comptime T: type, comptime default: T) type {
const prev_len = self.len();
const prev_cap = self.storage.len;
self.storage = try alloc.realloc(self.storage, size);
+ const retained_len = @min(prev_len, size);
// If we grew, we need to set our new defaults. We can add it
// at the end since we rotated to start.
if (size > prev_cap) {
@memset(self.storage[prev_cap..], default);
-
- // Fix up our head/tail
- if (self.full) {
- self.head = prev_len;
- self.full = false;
- }
}
+
+ // Fix up our head/tail for the new capacity.
+ self.head = if (retained_len == size) 0 else retained_len;
+ self.tail = 0;
+ self.full = retained_len == size;
}
/// Rotate the data so that it is zero-aligned.
@@ -793,6 +793,61 @@ test "CircBuf resize shrink" {
}
}
+test "CircBuf resize shrink partially full" {
+ const testing = std.testing;
+ const alloc = testing.allocator;
+
+ const Buf = CircBuf(u8, 0);
+ var buf = try Buf.init(alloc, 5);
+ defer buf.deinit(alloc);
+
+ try buf.append(1);
+ try buf.append(2);
+ try buf.append(3);
+
+ try buf.resize(alloc, 2);
+ try testing.expect(buf.full);
+ try testing.expectEqual(@as(usize, 2), buf.len());
+ try testing.expectEqual(@as(usize, 2), buf.capacity());
+
+ var it = buf.iterator(.forward);
+ try testing.expectEqual(@as(u8, 1), it.next().?.*);
+ try testing.expectEqual(@as(u8, 2), it.next().?.*);
+ try testing.expect(it.next() == null);
+ try testing.expectError(error.OutOfMemory, buf.append(4));
+}
+
+test "CircBuf resize shrink to length" {
+ const testing = std.testing;
+ const alloc = testing.allocator;
+
+ const Buf = CircBuf(u8, 0);
+ var buf = try Buf.init(alloc, 5);
+ defer buf.deinit(alloc);
+
+ try buf.append(1);
+ try buf.append(2);
+ try buf.resize(alloc, 2);
+
+ try testing.expect(buf.full);
+ try testing.expectEqual(@as(usize, 2), buf.len());
+ try testing.expectEqual(@as(usize, 2), buf.capacity());
+}
+
+test "CircBuf resize empty to zero" {
+ const testing = std.testing;
+ const alloc = testing.allocator;
+
+ const Buf = CircBuf(u8, 0);
+ var buf = try Buf.init(alloc, 5);
+ defer buf.deinit(alloc);
+
+ try buf.resize(alloc, 0);
+ try testing.expect(buf.full);
+ try testing.expectEqual(@as(usize, 0), buf.len());
+ try testing.expectEqual(@as(usize, 0), buf.capacity());
+}
+
test "CircBuf append empty slice" {
const testing = std.testing;
const alloc = testing.allocator;