| ... | ... | @@ -60,12 +60,7 @@ pub fn Reader( |
| 60 | 60 | return self.readAllArrayListAligned(null, array_list, max_append_size); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | | pub fn readAllArrayListAligned( |
| 64 | | self: Self, |
| 65 | | comptime alignment: ?u29, |
| 66 | | array_list: *std.ArrayListAligned(u8, alignment), |
| 67 | | max_append_size: usize |
| 68 | | ) !void { |
| 63 | pub fn readAllArrayListAligned(self: Self, comptime alignment: ?u29, array_list: *std.ArrayListAligned(u8, alignment), max_append_size: usize) !void { |
| 69 | 64 | try array_list.ensureCapacity(math.min(max_append_size, 4096)); |
| 70 | 65 | const original_len = array_list.items.len; |
| 71 | 66 | var start_index: usize = original_len; |
| ... | ... | @@ -101,6 +96,35 @@ pub fn Reader( |
| 101 | 96 | return array_list.toOwnedSlice(); |
| 102 | 97 | } |
| 103 | 98 | |
| 99 | /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` or end-of-stream is found. |
| 100 | /// Does not include the delimiter in the result. |
| 101 | /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the |
| 102 | /// `std.ArrayList` is populated with `max_size` bytes from the stream. |
| 103 | pub fn readUntilDelimiterOrEofArrayList( |
| 104 | self: Self, |
| 105 | array_list: *std.ArrayList(u8), |
| 106 | delimiter: u8, |
| 107 | max_size: usize, |
| 108 | ) !void { |
| 109 | array_list.shrink(0); |
| 110 | while (true) { |
| 111 | var byte: u8 = self.readByte() catch |err| switch (err) { |
| 112 | error.EndOfStream => return, |
| 113 | else => |e| return e, |
| 114 | }; |
| 115 | |
| 116 | if (byte == delimiter) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (array_list.items.len == max_size) { |
| 121 | return error.StreamTooLong; |
| 122 | } |
| 123 | |
| 124 | try array_list.append(byte); |
| 125 | } |
| 126 | } |
| 127 | |
| 104 | 128 | /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` is found. |
| 105 | 129 | /// Does not include the delimiter in the result. |
| 106 | 130 | /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the |
| ... | ... | @@ -143,6 +167,22 @@ pub fn Reader( |
| 143 | 167 | return array_list.toOwnedSlice(); |
| 144 | 168 | } |
| 145 | 169 | |
| 170 | /// Allocates enough memory to read until `delimiter` or end-of-stream. |
| 171 | /// If the allocated memory would be greater than `max_size`, returns `error.StreamTooLong`. |
| 172 | /// Caller owns returned memory. |
| 173 | /// If this function returns an error, the contents from the stream read so far are lost. |
| 174 | pub fn readUntilDelimiterOrEofAlloc( |
| 175 | self: Self, |
| 176 | allocator: *mem.Allocator, |
| 177 | delimiter: u8, |
| 178 | max_size: usize, |
| 179 | ) ![]u8 { |
| 180 | var array_list = std.ArrayList(u8).init(allocator); |
| 181 | defer array_list.deinit(); |
| 182 | try self.readUntilDelimiterOrEofArrayList(&array_list, delimiter, max_size); |
| 183 | return array_list.toOwnedSlice(); |
| 184 | } |
| 185 | |
| 146 | 186 | /// Reads from the stream until specified byte is found. If the buffer is not |
| 147 | 187 | /// large enough to hold the entire contents, `error.StreamTooLong` is returned. |
| 148 | 188 | /// If end-of-stream is found, returns the rest of the stream. If this |