From 3ed9e69f7cecc22cd9f9ff18b8676329148f4798 Mon Sep 17 00:00:00 2001
From: John Costa <johncosta027@gmail.com>
Date: Sun, 19 Nov 2023 17:14:23 +0000
Subject: [PATCH] FEAT: Util functions to split by lijnes

---
 zig/read_input.zig | 32 ++++++++++++++++++++++++++++++++
 zig/test.txt       |  2 ++
 2 files changed, 34 insertions(+)
 create mode 100644 zig/read_input.zig
 create mode 100644 zig/test.txt

diff --git a/zig/read_input.zig b/zig/read_input.zig
new file mode 100644
index 0000000..4a545ac
--- /dev/null
+++ b/zig/read_input.zig
@@ -0,0 +1,32 @@
+const std = @import("std");
+
+const mem = std.mem;
+const ArrayList = std.ArrayList;
+const expect = std.testing.expect;
+
+const MAX_SIZE = 1_000_000_000;
+
+pub fn split_lines(file_path: []const u8, allocator: mem.Allocator) !ArrayList([]const u8) {
+    const file = try std.fs.cwd().openFile(file_path, .{});
+    var content = try file.reader().readAllAlloc(allocator, MAX_SIZE);
+
+    var lines = ArrayList([]const u8).init(allocator);
+    var iterator = mem.tokenize(u8, content, "\n");
+
+    while (iterator.next()) |line| {
+        try lines.append(line);
+    }
+
+    return lines;
+}
+
+test "Reads file" {
+    var test_alloc = std.heap.page_allocator;
+    var lines = try split_lines("test.txt", test_alloc);
+
+    defer lines.deinit();
+
+    try expect(lines.items.len == 2);
+    try expect(mem.eql(u8, lines.items[0], "hello"));
+    try expect(mem.eql(u8, lines.items[1], "world"));
+}
diff --git a/zig/test.txt b/zig/test.txt
new file mode 100644
index 0000000..94954ab
--- /dev/null
+++ b/zig/test.txt
@@ -0,0 +1,2 @@
+hello
+world