authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-11 23:35:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:57-07:00
log2dc6ddd7e8cfb83f5ffd57b8b40f33dde9de892d
tree7d19676be4468c859bef8a2f7540e221dc142501
parentc8b983c59a91441a6cfa134764c6413195e824ad

std.Io.Writer: add toArrayList/fromArrayList


1 files changed, 28 insertions(+), 5 deletions(-)

lib/std/Io/Writer.zig+28-5
...@@ -8,6 +8,7 @@ const Limit = std.Io.Limit;...@@ -8,6 +8,7 @@ const Limit = std.Io.Limit;
8const File = std.fs.File;8const File = std.fs.File;
9const testing = std.testing;9const testing = std.testing;
10const Allocator = std.mem.Allocator;10const Allocator = std.mem.Allocator;
11const ArrayList = std.ArrayList;
1112
12vtable: *const VTable,13vtable: *const VTable,
13/// If this has length zero, the writer is unbuffered, and `flush` is a no-op.14/// If this has length zero, the writer is unbuffered, and `flush` is a no-op.
...@@ -2374,6 +2375,28 @@ pub fn unreachableRebase(w: *Writer, preserve: usize, capacity: usize) Error!voi...@@ -2374,6 +2375,28 @@ pub fn unreachableRebase(w: *Writer, preserve: usize, capacity: usize) Error!voi
2374 unreachable;2375 unreachable;
2375}2376}
23762377
2378pub fn fromArrayList(array_list: *ArrayList(u8)) Writer {
2379 defer array_list.* = .empty;
2380 return .{
2381 .vtable = &.{
2382 .drain = fixedDrain,
2383 .flush = noopFlush,
2384 },
2385 .buffer = array_list.allocatedSlice(),
2386 .end = array_list.items.len,
2387 };
2388}
2389
2390pub fn toArrayList(w: *Writer) ArrayList(u8) {
2391 const result: ArrayList(u8) = .{
2392 .items = w.buffer[0..w.end],
2393 .capacity = w.buffer.len,
2394 };
2395 w.buffer = &.{};
2396 w.end = 0;
2397 return result;
2398}
2399
2377/// Provides a `Writer` implementation based on calling `Hasher.update`, sending2400/// Provides a `Writer` implementation based on calling `Hasher.update`, sending
2378/// all data also to an underlying `Writer`.2401/// all data also to an underlying `Writer`.
2379///2402///
...@@ -2546,7 +2569,7 @@ pub const Allocating = struct {...@@ -2546,7 +2569,7 @@ pub const Allocating = struct {
2546 }2569 }
25472570
2548 /// Replaces `array_list` with empty, taking ownership of the memory.2571 /// Replaces `array_list` with empty, taking ownership of the memory.
2549 pub fn fromArrayList(allocator: Allocator, array_list: *std.ArrayListUnmanaged(u8)) Allocating {2572 pub fn fromArrayList(allocator: Allocator, array_list: *ArrayList(u8)) Allocating {
2550 defer array_list.* = .empty;2573 defer array_list.* = .empty;
2551 return .{2574 return .{
2552 .allocator = allocator,2575 .allocator = allocator,
...@@ -2572,9 +2595,9 @@ pub const Allocating = struct {...@@ -2572,9 +2595,9 @@ pub const Allocating = struct {
25722595
2573 /// Returns an array list that takes ownership of the allocated memory.2596 /// Returns an array list that takes ownership of the allocated memory.
2574 /// Resets the `Allocating` to an empty state.2597 /// Resets the `Allocating` to an empty state.
2575 pub fn toArrayList(a: *Allocating) std.ArrayListUnmanaged(u8) {2598 pub fn toArrayList(a: *Allocating) ArrayList(u8) {
2576 const w = &a.writer;2599 const w = &a.writer;
2577 const result: std.ArrayListUnmanaged(u8) = .{2600 const result: ArrayList(u8) = .{
2578 .items = w.buffer[0..w.end],2601 .items = w.buffer[0..w.end],
2579 .capacity = w.buffer.len,2602 .capacity = w.buffer.len,
2580 };2603 };
...@@ -2603,7 +2626,7 @@ pub const Allocating = struct {...@@ -2603,7 +2626,7 @@ pub const Allocating = struct {
26032626
2604 pub fn toOwnedSliceSentinel(a: *Allocating, comptime sentinel: u8) error{OutOfMemory}![:sentinel]u8 {2627 pub fn toOwnedSliceSentinel(a: *Allocating, comptime sentinel: u8) error{OutOfMemory}![:sentinel]u8 {
2605 const gpa = a.allocator;2628 const gpa = a.allocator;
2606 var list = toArrayList(a);2629 var list = @This().toArrayList(a);
2607 defer a.setArrayList(list);2630 defer a.setArrayList(list);
2608 return list.toOwnedSliceSentinel(gpa, sentinel);2631 return list.toOwnedSliceSentinel(gpa, sentinel);
2609 }2632 }
...@@ -2670,7 +2693,7 @@ pub const Allocating = struct {...@@ -2670,7 +2693,7 @@ pub const Allocating = struct {
2670 list.ensureUnusedCapacity(gpa, minimum_len) catch return error.WriteFailed;2693 list.ensureUnusedCapacity(gpa, minimum_len) catch return error.WriteFailed;
2671 }2694 }
26722695
2673 fn setArrayList(a: *Allocating, list: std.ArrayListUnmanaged(u8)) void {2696 fn setArrayList(a: *Allocating, list: ArrayList(u8)) void {
2674 a.writer.buffer = list.allocatedSlice();2697 a.writer.buffer = list.allocatedSlice();
2675 a.writer.end = list.items.len;2698 a.writer.end = list.items.len;
2676 }2699 }