| ... | ... | @@ -184,35 +184,38 @@ pub fn InStream( |
| 184 | 184 | return @bitCast(i8, try self.readByte()); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | /// Reads exactly `num_bytes` bytes and returns as an array. |
| 188 | /// `num_bytes` must be comptime-known |
| 189 | pub fn readBytesNoEof(self: Self, comptime num_bytes: usize) ![num_bytes]u8 { |
| 190 | var bytes: [num_bytes]u8 = undefined; |
| 191 | try self.readNoEof(&bytes); |
| 192 | return bytes; |
| 193 | } |
| 194 | |
| 187 | 195 | /// Reads a native-endian integer |
| 188 | 196 | pub fn readIntNative(self: Self, comptime T: type) !T { |
| 189 | | var bytes: [(T.bit_count + 7) / 8]u8 = undefined; |
| 190 | | try self.readNoEof(bytes[0..]); |
| 197 | const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8); |
| 191 | 198 | return mem.readIntNative(T, &bytes); |
| 192 | 199 | } |
| 193 | 200 | |
| 194 | 201 | /// Reads a foreign-endian integer |
| 195 | 202 | pub fn readIntForeign(self: Self, comptime T: type) !T { |
| 196 | | var bytes: [(T.bit_count + 7) / 8]u8 = undefined; |
| 197 | | try self.readNoEof(bytes[0..]); |
| 203 | const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8); |
| 198 | 204 | return mem.readIntForeign(T, &bytes); |
| 199 | 205 | } |
| 200 | 206 | |
| 201 | 207 | pub fn readIntLittle(self: Self, comptime T: type) !T { |
| 202 | | var bytes: [(T.bit_count + 7) / 8]u8 = undefined; |
| 203 | | try self.readNoEof(bytes[0..]); |
| 208 | const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8); |
| 204 | 209 | return mem.readIntLittle(T, &bytes); |
| 205 | 210 | } |
| 206 | 211 | |
| 207 | 212 | pub fn readIntBig(self: Self, comptime T: type) !T { |
| 208 | | var bytes: [(T.bit_count + 7) / 8]u8 = undefined; |
| 209 | | try self.readNoEof(bytes[0..]); |
| 213 | const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8); |
| 210 | 214 | return mem.readIntBig(T, &bytes); |
| 211 | 215 | } |
| 212 | 216 | |
| 213 | 217 | pub fn readInt(self: Self, comptime T: type, endian: builtin.Endian) !T { |
| 214 | | var bytes: [(T.bit_count + 7) / 8]u8 = undefined; |
| 215 | | try self.readNoEof(bytes[0..]); |
| 218 | const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8); |
| 216 | 219 | return mem.readInt(T, &bytes, endian); |
| 217 | 220 | } |
| 218 | 221 | |