| ... | @@ -64,7 +64,7 @@ pub fn Reader( | ... | @@ -64,7 +64,7 @@ pub fn Reader( |
| 64 | self: Self, | 64 | self: Self, |
| 65 | comptime alignment: ?u29, | 65 | comptime alignment: ?u29, |
| 66 | array_list: *std.ArrayListAligned(u8, alignment), | 66 | array_list: *std.ArrayListAligned(u8, alignment), |
| 67 | max_append_size: usize | 67 | max_append_size: usize, |
| 68 | ) !void { | 68 | ) !void { |
| 69 | try array_list.ensureCapacity(math.min(max_append_size, 4096)); | 69 | try array_list.ensureCapacity(math.min(max_append_size, 4096)); |
| 70 | const original_len = array_list.items.len; | 70 | const original_len = array_list.items.len; |
| ... | @@ -101,6 +101,35 @@ pub fn Reader( | ... | @@ -101,6 +101,35 @@ pub fn Reader( |
| 101 | return array_list.toOwnedSlice(); | 101 | return array_list.toOwnedSlice(); |
| 102 | } | 102 | } |
| 103 | | 103 | |
| | 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 | |
| 104 | /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` is found. | 133 | /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` is found. |
| 105 | /// Does not include the delimiter in the result. | 134 | /// Does not include the delimiter in the result. |
| 106 | /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the | 135 | /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the |
| ... | @@ -143,6 +172,22 @@ pub fn Reader( | ... | @@ -143,6 +172,22 @@ pub fn Reader( |
| 143 | return array_list.toOwnedSlice(); | 172 | return array_list.toOwnedSlice(); |
| 144 | } | 173 | } |
| 145 | | 174 | |
| | 175 | /// 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`. |
| | 177 | /// Caller owns returned memory. |
| | 178 | /// If this function returns an error, the contents from the stream read so far are lost. |
| | 179 | pub fn readUntilDelimiterOrEofAlloc( |
| | 180 | self: Self, |
| | 181 | allocator: *mem.Allocator, |
| | 182 | delimiter: u8, |
| | 183 | max_size: usize, |
| | 184 | ) ![]u8 { |
| | 185 | var array_list = std.ArrayList(u8).init(allocator); |
| | 186 | defer array_list.deinit(); |
| | 187 | try self.readUntilDelimiterOrEofArrayList(&array_list, delimiter, max_size); |
| | 188 | return array_list.toOwnedSlice(); |
| | 189 | } |
| | 190 | |
| 146 | /// Reads from the stream until specified byte is found. If the buffer is not | 191 | /// Reads from the stream until specified byte is found. If the buffer is not |
| 147 | /// large enough to hold the entire contents, `error.StreamTooLong` is returned. | 192 | /// large enough to hold the entire contents, `error.StreamTooLong` is returned. |
| 148 | /// If end-of-stream is found, returns the rest of the stream. If this | 193 | /// If end-of-stream is found, returns the rest of the stream. If this |