summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/file.zig15
-rw-r--r--util/mem.zig17
2 files changed, 32 insertions, 0 deletions
diff --git a/util/file.zig b/util/file.zig
new file mode 100644
index 0000000..90849e9
--- /dev/null
+++ b/util/file.zig
@@ -0,0 +1,15 @@
+const std = @import("std");
+
+/// Reads an entire file into memory, caller owns the returned slice.
+pub fn slurp(allocator: std.mem.Allocator, file_path: []const u8) ![]u8 {
+ var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
+ const path = try std.fs.realpath(file_path, &path_buffer);
+
+ const file = try std.fs.openFileAbsolute(path, .{});
+ defer file.close();
+
+ return try file.readToEndAlloc(
+ allocator,
+ (try file.stat()).size,
+ );
+}
diff --git a/util/mem.zig b/util/mem.zig
new file mode 100644
index 0000000..89ba67b
--- /dev/null
+++ b/util/mem.zig
@@ -0,0 +1,17 @@
+const std = @import("std");
+const math = std.math;
+
+/// Returns the position of the smallest number in a slice.
+pub fn min_idx(comptime T: type, slice: []const T) usize {
+ var best = slice[0];
+ var idx: usize = 0;
+
+ for (slice[1..]) |item, i| {
+ const possible_best = math.min(best, item);
+ if (best > possible_best) {
+ best = possible_best;
+ idx = i + 1;
+ }
+ }
+ return idx;
+}