just read all bytes
This commit is contained in:
parent
d37ce56eef
commit
c290ad1e65
@ -2,4 +2,3 @@
|
|||||||
pqr3stu8vwx
|
pqr3stu8vwx
|
||||||
a1b2c3d4e5f
|
a1b2c3d4e5f
|
||||||
treb7uchet
|
treb7uchet
|
||||||
|
|
||||||
|
57
src/main.zig
57
src/main.zig
@ -16,18 +16,12 @@ pub fn main() !void {
|
|||||||
|
|
||||||
fn day01part1(allocator: std.mem.Allocator, comptime is_test: bool) !void {
|
fn day01part1(allocator: std.mem.Allocator, comptime is_test: bool) !void {
|
||||||
const filename = file_name(is_test, "01");
|
const filename = file_name(is_test, "01");
|
||||||
const line_reader = try LineReader.init(allocator, filename);
|
const buff = try read_file(allocator, filename);
|
||||||
errdefer line_reader.deinit();
|
defer allocator.free(buff);
|
||||||
|
var lines = std.mem.split(u8, buff, "\n");
|
||||||
|
|
||||||
while (true) {
|
while (lines.next()) |line| {
|
||||||
const bytes = line_reader.next() catch |err| switch (err) {
|
print("line! {s}\n", .{line});
|
||||||
error.EndOfStream => {
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
else => return err,
|
|
||||||
};
|
|
||||||
|
|
||||||
print("Bytes: {s}\n", .{bytes});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,41 +31,14 @@ fn file_name(comptime is_test: bool, comptime day: []const u8) []const u8 {
|
|||||||
return "inputs" ++ input_suffix ++ day;
|
return "inputs" ++ input_suffix ++ day;
|
||||||
}
|
}
|
||||||
|
|
||||||
const U8ArrayList = std.ArrayList(u8);
|
fn read_file(allocator: std.mem.Allocator, filename: []const u8) ![]u8 {
|
||||||
|
const file = try std.fs.cwd().openFile(filename, .{});
|
||||||
|
defer file.close();
|
||||||
|
|
||||||
const LineReader = struct {
|
const stat = try file.stat();
|
||||||
allocator: std.mem.Allocator,
|
const buff = try file.readToEndAlloc(allocator, stat.size);
|
||||||
file: *std.fs.File,
|
return buff;
|
||||||
line: *U8ArrayList,
|
}
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, filename: []const u8) !LineReader {
|
|
||||||
var file = try std.fs.cwd().openFile(filename, .{ .read = true });
|
|
||||||
var line = std.ArrayList(u8).init(allocator);
|
|
||||||
|
|
||||||
return .{
|
|
||||||
.allocator = allocator,
|
|
||||||
.file = &file,
|
|
||||||
.line = &line,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn next(self: LineReader) ![]u8 {
|
|
||||||
// self.line.clearRetainingCapacity();
|
|
||||||
var line = std.ArrayList(u8).init(self.allocator);
|
|
||||||
errdefer line.deinit();
|
|
||||||
|
|
||||||
const reader = self.file.reader();
|
|
||||||
const writer = line.writer();
|
|
||||||
|
|
||||||
try reader.streamUntilDelimiter(writer, '\n', null);
|
|
||||||
return self.line.items;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: LineReader) void {
|
|
||||||
self.file.close();
|
|
||||||
self.line.deinit();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
test "simple test" {
|
test "simple test" {
|
||||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
var list = std.ArrayList(i32).init(std.testing.allocator);
|
||||||
|
Loading…
Reference in New Issue
Block a user