authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-11 16:14:12-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-11 16:14:12-04:00
log6892865ba72ea220a12a8238ce78aedab1237cce
treeec60bbfbd034db797cb0ae9a19e52e2d2af8f9cb
parent04626c176b328f7ae937b0828f14f4b4be62b733
signaturelock-open Commit is signed but in an unrecognized format.

FixedBufferStream: match file semantics more by clamping pos


1 files changed, 8 insertions(+), 8 deletions(-)

lib/std/io/fixed_buffer_stream.zig+8-8
...@@ -76,21 +76,22 @@ pub fn FixedBufferStream(comptime Buffer: type) type {...@@ -76,21 +76,22 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
76 }76 }
7777
78 pub fn seekTo(self: *Self, pos: u64) SeekError!void {78 pub fn seekTo(self: *Self, pos: u64) SeekError!void {
79 const usize_pos = std.math.cast(usize, pos) catch std.math.maxInt(usize);79 self.pos = if (std.math.cast(usize, pos)) |x| x else |_| self.buffer.len;
80 self.pos = usize_pos;
81 }80 }
8281
83 pub fn seekBy(self: *Self, amt: i64) SeekError!void {82 pub fn seekBy(self: *Self, amt: i64) SeekError!void {
84 if (amt < 0) {83 if (amt < 0) {
85 const abs_amt = std.math.cast(usize, -amt) catch std.math.maxInt(usize);84 const abs_amt = std.math.absCast(amt);
86 if (abs_amt > self.pos) {85 const abs_amt_usize = std.math.cast(usize, abs_amt) catch std.math.maxInt(usize);
86 if (abs_amt_usize > self.pos) {
87 self.pos = 0;87 self.pos = 0;
88 } else {88 } else {
89 self.pos -= abs_amt;89 self.pos -= abs_amt_usize;
90 }90 }
91 } else {91 } else {
92 const usize_amt = std.math.cast(usize, amt) catch std.math.maxInt(usize);92 const amt_usize = std.math.cast(usize, amt) catch std.math.maxInt(usize);
93 self.pos = std.math.add(usize, self.pos, usize_amt) catch std.math.maxInt(usize);93 const new_pos = std.math.add(usize, self.pos, amt_usize) catch std.math.maxInt(usize);
94 self.pos = std.math.min(self.buffer.len, new_pos);
94 }95 }
95 }96 }
9697
...@@ -102,7 +103,6 @@ pub fn FixedBufferStream(comptime Buffer: type) type {...@@ -102,7 +103,6 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
102 return self.pos;103 return self.pos;
103 }104 }
104105
105 /// Asserts that the seek pos is within the buffer range.
106 pub fn getWritten(self: Self) []const u8 {106 pub fn getWritten(self: Self) []const u8 {
107 return self.buffer[0..self.pos];107 return self.buffer[0..self.pos];
108 }108 }