| ... | ... | @@ -101,35 +101,6 @@ pub fn Reader( |
| 101 | 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 | | |
| 133 | 104 | /// Replaces the `std.ArrayList` contents by reading from the stream until `delimiter` is found. |
| 134 | 105 | /// Does not include the delimiter in the result. |
| 135 | 106 | /// If the `std.ArrayList` length would exceed `max_size`, `error.StreamTooLong` is returned and the |
| ... | ... | @@ -173,7 +144,10 @@ pub fn Reader( |
| 173 | 144 | } |
| 174 | 145 | |
| 175 | 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 | 151 | /// Caller owns returned memory. |
| 178 | 152 | /// If this function returns an error, the contents from the stream read so far are lost. |
| 179 | 153 | pub fn readUntilDelimiterOrEofAlloc( |
| ... | ... | @@ -181,10 +155,17 @@ pub fn Reader( |
| 181 | 155 | allocator: *mem.Allocator, |
| 182 | 156 | delimiter: u8, |
| 183 | 157 | max_size: usize, |
| 184 | | ) ![]u8 { |
| 158 | ) !?[]u8 { |
| 185 | 159 | var array_list = std.ArrayList(u8).init(allocator); |
| 186 | 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 | 169 | return array_list.toOwnedSlice(); |
| 189 | 170 | } |
| 190 | 171 | |