authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-12-29 00:20:31+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-29 19:09:36-08:00
log3634d44d08c73326c7783cf2a3095acb50eb2e1c
tree90e6ddf5f7e41e84f93ab4776a8252e979b5f379
parent3d79ae2be307372ca309e92f622a8f607ebeb926

std: fix Reader.readUntilDelimiterOrEofAlloc() API

The current API does not allow the user to distinguish between EOF and an empty line. Reader.readUntilDelimiterOrEof() gets this API right so update readUntilDelimiterOrEofAlloc() to match it. Returning an optional here additionally makes calling this in a loop much cleaner. Remove readUntilDelimiterOrEofArrayList() as it no longer needed to implement readUntilDelimiterOrEof() and has the same API issues described without a clear way to fix them.

1 files changed, 13 insertions(+), 32 deletions(-)

lib/std/io/reader.zig+13-32
...@@ -101,35 +101,6 @@ pub fn Reader(...@@ -101,35 +101,6 @@ pub fn Reader(
101 return array_list.toOwnedSlice();101 return array_list.toOwnedSlice();
102 }102 }
103103
104 /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` or end-of-stream is found.
105 /// Does not include the delimiter in the result.
106 /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the
107 /// `std.ArrayList` is populated with `max_size` bytes from the stream.
108 pub fn readUntilDelimiterOrEofArrayList(
109 self: Self,
110 array_list: *std.ArrayList(u8),
111 delimiter: u8,
112 max_size: usize,
113 ) !void {
114 array_list.shrink(0);
115 while (true) {
116 var byte: u8 = self.readByte() catch |err| switch (err) {
117 error.EndOfStream => return,
118 else => |e| return e,
119 };
120
121 if (byte == delimiter) {
122 return;
123 }
124
125 if (array_list.items.len == max_size) {
126 return error.StreamTooLong;
127 }
128
129 try array_list.append(byte);
130 }
131 }
132
133 /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` is found.104 /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` is found.
134 /// Does not include the delimiter in the result.105 /// Does not include the delimiter in the result.
135 /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the106 /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the
...@@ -173,7 +144,10 @@ pub fn Reader(...@@ -173,7 +144,10 @@ pub fn Reader(
173 }144 }
174145
175 /// Allocates enough memory to read until `delimiter` or end-of-stream.146 /// Allocates enough memory to read until `delimiter` or end-of-stream.
176 /// If the allocated memory would be greater than `max_size`, returns `error.StreamTooLong`.147 /// If the allocated memory would be greater than `max_size`, returns
148 /// `error.StreamTooLong`. If end-of-stream is found, returns the rest
149 /// of the stream. If this function is called again after that, returns
150 /// null.
177 /// Caller owns returned memory.151 /// Caller owns returned memory.
178 /// If this function returns an error, the contents from the stream read so far are lost.152 /// If this function returns an error, the contents from the stream read so far are lost.
179 pub fn readUntilDelimiterOrEofAlloc(153 pub fn readUntilDelimiterOrEofAlloc(
...@@ -181,10 +155,17 @@ pub fn Reader(...@@ -181,10 +155,17 @@ pub fn Reader(
181 allocator: *mem.Allocator,155 allocator: *mem.Allocator,
182 delimiter: u8,156 delimiter: u8,
183 max_size: usize,157 max_size: usize,
184 ) ![]u8 {158 ) !?[]u8 {
185 var array_list = std.ArrayList(u8).init(allocator);159 var array_list = std.ArrayList(u8).init(allocator);
186 defer array_list.deinit();160 defer array_list.deinit();
187 try self.readUntilDelimiterOrEofArrayList(&array_list, delimiter, max_size);161 self.readUntilDelimiterArrayList(&array_list, delimiter, max_size) catch |err| switch (err) {
162 error.EndOfStream => if (array_list.items.len == 0) {
163 return null;
164 } else {
165 return array_list.toOwnedSlice();
166 },
167 else => |e| return e,
168 };
188 return array_list.toOwnedSlice();169 return array_list.toOwnedSlice();
189 }170 }
190171