| ... | @@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct { | ... | @@ -790,73 +790,6 @@ pub const BufferedAtomicFile = struct { |
| 790 | } | 790 | } |
| 791 | }; | 791 | }; |
| 792 | | 792 | |
| 793 | pub fn readLine(buf: *std.Buffer) ![]u8 { | | |
| 794 | var stdin_stream = getStdIn().inStream(); | | |
| 795 | return readLineFrom(&stdin_stream.stream, buf); | | |
| 796 | } | | |
| 797 | | | |
| 798 | /// Reads all characters until the next newline into buf, and returns | | |
| 799 | /// a slice of the characters read (excluding the newline character(s)). | | |
| 800 | pub fn readLineFrom(stream: var, buf: *std.Buffer) ![]u8 { | | |
| 801 | const start = buf.len(); | | |
| 802 | while (true) { | | |
| 803 | const byte = try stream.readByte(); | | |
| 804 | switch (byte) { | | |
| 805 | '\r' => { | | |
| 806 | // trash the following \n | | |
| 807 | _ = try stream.readByte(); | | |
| 808 | return buf.toSlice()[start..]; | | |
| 809 | }, | | |
| 810 | '\n' => return buf.toSlice()[start..], | | |
| 811 | else => try buf.appendByte(byte), | | |
| 812 | } | | |
| 813 | } | | |
| 814 | } | | |
| 815 | | | |
| 816 | test "io.readLineFrom" { | | |
| 817 | var buf = try std.Buffer.initSize(testing.allocator, 0); | | |
| 818 | defer buf.deinit(); | | |
| 819 | var mem_stream = SliceInStream.init( | | |
| 820 | \\Line 1 | | |
| 821 | \\Line 22 | | |
| 822 | \\Line 333 | | |
| 823 | ); | | |
| 824 | const stream = &mem_stream.stream; | | |
| 825 | | | |
| 826 | testing.expectEqualSlices(u8, "Line 1", try readLineFrom(stream, &buf)); | | |
| 827 | testing.expectEqualSlices(u8, "Line 22", try readLineFrom(stream, &buf)); | | |
| 828 | testing.expectError(error.EndOfStream, readLineFrom(stream, &buf)); | | |
| 829 | testing.expectEqualSlices(u8, "Line 1Line 22Line 333", buf.toSlice()); | | |
| 830 | } | | |
| 831 | | | |
| 832 | pub fn readLineSlice(slice: []u8) ![]u8 { | | |
| 833 | var stdin_stream = getStdIn().inStream(); | | |
| 834 | return readLineSliceFrom(&stdin_stream.stream, slice); | | |
| 835 | } | | |
| 836 | | | |
| 837 | /// Reads all characters until the next newline into slice, and returns | | |
| 838 | /// a slice of the characters read (excluding the newline character(s)). | | |
| 839 | pub fn readLineSliceFrom(stream: var, slice: []u8) ![]u8 { | | |
| 840 | // We cannot use Buffer.fromOwnedSlice, as it wants to append a null byte | | |
| 841 | // after taking ownership, which would always require an allocation. | | |
| 842 | var buf = std.Buffer{ .list = std.ArrayList(u8).fromOwnedSlice(testing.failing_allocator, slice) }; | | |
| 843 | try buf.resize(0); | | |
| 844 | return try readLineFrom(stream, &buf); | | |
| 845 | } | | |
| 846 | | | |
| 847 | test "io.readLineSliceFrom" { | | |
| 848 | var buf: [7]u8 = undefined; | | |
| 849 | var mem_stream = SliceInStream.init( | | |
| 850 | \\Line 1 | | |
| 851 | \\Line 22 | | |
| 852 | \\Line 333 | | |
| 853 | ); | | |
| 854 | const stream = &mem_stream.stream; | | |
| 855 | | | |
| 856 | testing.expectEqualSlices(u8, "Line 1", try readLineSliceFrom(stream, buf[0..])); | | |
| 857 | testing.expectError(error.OutOfMemory, readLineSliceFrom(stream, buf[0..])); | | |
| 858 | } | | |
| 859 | | | |
| 860 | pub const Packing = enum { | 793 | pub const Packing = enum { |
| 861 | /// Pack data to byte alignment | 794 | /// Pack data to byte alignment |
| 862 | Byte, | 795 | Byte, |