authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-02-07 14:43:09+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-06 18:49:12-05:00
logbcf56c32eb11306613c92128c1b95ff280ba4f68
treec9339d7953a3f99e4f2c4a9f0ea5961a9297d66a
parentfd23decbd9dc39b044867993b67607e33ec9de45
signaturelock-open Commit is signed but in an unrecognized format.

std: use ArrayList rather than Buffer for in_stream helper functions

Buffer's behaviour of retaining a trailing 0 isn't helpful here

1 files changed, 34 insertions(+), 17 deletions(-)

lib/std/io/in_stream.zig+34-17
......@@ -65,22 +65,31 @@ pub fn InStream(comptime ReadError: type) type {
6565 /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and
6666 /// the contents read from the stream are lost.
6767 pub fn readAllBuffer(self: *Self, buffer: *Buffer, max_size: usize) !void {
68 try buffer.resize(0);
68 try buffer.list.ensureCapacity(1);
69 buffer.list.len = 0;
70 errdefer buffer.resize(0) catch unreachable; // make sure we leave buffer in a valid state on error
71 try self.readAllArrayList(&buffer.list, max_size);
72 try buffer.list.append(0);
73 }
6974
75 /// Appends to the ArrayList contents by reading from the stream until end of stream is found.
76 /// If the ArrayList length would exceed `max_size`, `error.StreamTooLong` is returned and the contents
77 /// read from the stream so far are lost.
78 pub fn readAllArrayList(self: *Self, array_list: *std.ArrayList(u8), max_size: usize) !void {
7079 var actual_buf_len: usize = 0;
7180 while (true) {
72 const dest_slice = buffer.toSlice()[actual_buf_len..];
81 const dest_slice = array_list.toSlice()[actual_buf_len..];
7382 const bytes_read = try self.readFull(dest_slice);
7483 actual_buf_len += bytes_read;
7584
7685 if (bytes_read != dest_slice.len) {
77 buffer.shrink(actual_buf_len);
86 array_list.shrink(actual_buf_len);
7887 return;
7988 }
8089
8190 const new_buf_size = math.min(max_size, actual_buf_len + mem.page_size);
8291 if (new_buf_size == actual_buf_len) return error.StreamTooLong;
83 try buffer.resize(new_buf_size);
92 try array_list.resize(new_buf_size);
8493 }
8594 }
8695
......@@ -89,11 +98,10 @@ pub fn InStream(comptime ReadError: type) type {
8998 /// Caller owns returned memory.
9099 /// If this function returns an error, the contents from the stream read so far are lost.
91100 pub fn readAllAlloc(self: *Self, allocator: *mem.Allocator, max_size: usize) ![]u8 {
92 var buf = Buffer.initNull(allocator);
93 defer buf.deinit();
94
95 try self.readAllBuffer(&buf, max_size);
96 return buf.toOwnedSlice();
101 var array_list = std.ArrayList(u8).init(allocator);
102 defer array_list.deinit();
103 try self.readAllArrayList(&array_list, max_size);
104 return array_list.toOwnedSlice();
97105 }
98106
99107 /// Replaces `buffer` contents by reading from the stream until `delimiter` is found.
......@@ -101,8 +109,18 @@ pub fn InStream(comptime ReadError: type) type {
101109 /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents
102110 /// read from the stream so far are lost.
103111 pub fn readUntilDelimiterBuffer(self: *Self, buffer: *Buffer, delimiter: u8, max_size: usize) !void {
104 try buffer.resize(0);
112 try buffer.list.ensureCapacity(1);
113 buffer.list.len = 0;
114 errdefer buffer.resize(0) catch unreachable; // make sure we leave buffer in a valid state on error
115 try self.readUntilDelimiterArrayList(&buffer.list, delimiter, max_size);
116 try buffer.list.append(0);
117 }
105118
119 /// Appends to the ArrayList contents by reading from the stream until `delimiter` is found.
120 /// Does not include the delimiter in the result.
121 /// If the ArrayList length would exceed `max_size`, `error.StreamTooLong` is returned and the contents
122 /// read from the stream so far are lost.
123 pub fn readUntilDelimiterArrayList(self: *Self, array_list: *std.ArrayList(u8), delimiter: u8, max_size: usize) !void {
106124 while (true) {
107125 var byte: u8 = try self.readByte();
108126
......@@ -110,11 +128,11 @@ pub fn InStream(comptime ReadError: type) type {
110128 return;
111129 }
112130
113 if (buffer.len() == max_size) {
131 if (array_list.len == max_size) {
114132 return error.StreamTooLong;
115133 }
116134
117 try buffer.appendByte(byte);
135 try array_list.append(byte);
118136 }
119137 }
120138
......@@ -123,11 +141,10 @@ pub fn InStream(comptime ReadError: type) type {
123141 /// Caller owns returned memory.
124142 /// If this function returns an error, the contents from the stream read so far are lost.
125143 pub fn readUntilDelimiterAlloc(self: *Self, allocator: *mem.Allocator, delimiter: u8, max_size: usize) ![]u8 {
126 var buf = Buffer.initNull(allocator);
127 defer buf.deinit();
128
129 try self.readUntilDelimiterBuffer(&buf, delimiter, max_size);
130 return buf.toOwnedSlice();
144 var array_list = std.ArrayList(u8).init(allocator);
145 defer array_list.deinit();
146 try self.readUntilDelimiterArrayList(&array_list, delimiter, max_size);
147 return array_list.toOwnedSlice();
131148 }
132149
133150 /// Reads from the stream until specified byte is found. If the buffer is not