| ... | ... | @@ -143,6 +143,23 @@ pub fn Reader( |
| 143 | 143 | return array_list.toOwnedSlice(); |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | /// 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. |
| 148 | /// Returns a slice of the stream data, with ptr equal to `buf.ptr`. The |
| 149 | /// delimiter byte is not included in the returned slice. |
| 150 | pub fn readUntilDelimiter(self: Self, buf: []u8, delimiter: u8) ![]u8 { |
| 151 | var index: usize = 0; |
| 152 | while (true) { |
| 153 | const byte = try self.readByte(); |
| 154 | |
| 155 | if (byte == delimiter) return buf[0..index]; |
| 156 | if (index >= buf.len) return error.StreamTooLong; |
| 157 | |
| 158 | buf[index] = byte; |
| 159 | index += 1; |
| 160 | } |
| 161 | } |
| 162 | |
| 146 | 163 | /// Allocates enough memory to read until `delimiter` or end-of-stream. |
| 147 | 164 | /// If the allocated memory would be greater than `max_size`, returns |
| 148 | 165 | /// `error.StreamTooLong`. If end-of-stream is found, returns the rest |