diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-08-27 16:05:51 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-08-28 18:30:57 -0700 |
| commit | f7884961c230367cefd3dfbf730ac5277297dc63 (patch) | |
| tree | e9f49ef21141371c1643a28ac7aa7347afc43cba /lib | |
| parent | 4f510dba10872cf7b71d1a48b4dc5879444f89d7 (diff) | |
| download | zig-f7884961c230367cefd3dfbf730ac5277297dc63.tar.gz zig-f7884961c230367cefd3dfbf730ac5277297dc63.zip | |
update more to avoid GenericWriter
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/Io/Reader.zig | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/lib/std/Io/Reader.zig b/lib/std/Io/Reader.zig index f6cb22cb4b..43ab4482c0 100644 --- a/lib/std/Io/Reader.zig +++ b/lib/std/Io/Reader.zig @@ -784,7 +784,7 @@ pub fn peekDelimiterInclusive(r: *Reader, delimiter: u8) DelimiterError![]u8 { } /// Returns a slice of the next bytes of buffered data from the stream until -/// `delimiter` is found, advancing the seek position. +/// `delimiter` is found, advancing the seek position up to the delimiter. /// /// Returned slice excludes the delimiter. End-of-stream is treated equivalent /// to a delimiter, unless it would result in a length 0 return value, in which @@ -815,6 +815,37 @@ pub fn takeDelimiterExclusive(r: *Reader, delimiter: u8) DelimiterError![]u8 { } /// Returns a slice of the next bytes of buffered data from the stream until +/// `delimiter` is found, advancing the seek position past the delimiter. +/// +/// Returned slice excludes the delimiter. End-of-stream is treated equivalent +/// to a delimiter, unless it would result in a length 0 return value, in which +/// case `null` is returned instead. +/// +/// If the delimiter is not found within a number of bytes matching the +/// capacity of this `Reader`, `error.StreamTooLong` is returned. In +/// such case, the stream state is unmodified as if this function was never +/// called. +/// +/// Invalidates previously returned values from `peek`. +/// +/// See also: +/// * `takeDelimiterInclusive` +/// * `takeDelimiterExclusive` +pub fn takeDelimiter(r: *Reader, delimiter: u8) error{ ReadFailed, StreamTooLong }!?[]u8 { + const result = r.peekDelimiterInclusive(delimiter) catch |err| switch (err) { + error.EndOfStream => { + const remaining = r.buffer[r.seek..r.end]; + if (remaining.len == 0) return null; + r.toss(remaining.len); + return remaining; + }, + else => |e| return e, + }; + r.toss(result.len + 1); + return result[0 .. result.len - 1]; +} + +/// Returns a slice of the next bytes of buffered data from the stream until /// `delimiter` is found, without advancing the seek position. /// /// Returned slice excludes the delimiter. End-of-stream is treated equivalent |
