| ... | @@ -0,0 +1,60 @@ |
| 1 | const std = @import("std"); |
| 2 | const gpa = std.heap.c_allocator; |
| 3 | |
| 4 | const u = @import("index.zig"); |
| 5 | |
| 6 | // |
| 7 | // |
| 8 | |
| 9 | pub fn print(comptime fmt: []const u8, args: anytype) void { |
| 10 | std.debug.print(fmt++"\n", args); |
| 11 | } |
| 12 | |
| 13 | pub fn assert(ok: bool, comptime fmt: []const u8, args: anytype) void { |
| 14 | if (!ok) { |
| 15 | print(comptime u.ansi.color.Fg(.Red, "error: " ++ fmt), args); |
| 16 | std.os.exit(1); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | pub fn try_index(comptime T: type, array: []T, n: usize, def: T) T { |
| 21 | if (array.len <= n) { |
| 22 | return def; |
| 23 | } |
| 24 | return array[n]; |
| 25 | } |
| 26 | |
| 27 | pub fn split(in: []const u8, delim: []const u8) ![][]const u8 { |
| 28 | const list = &std.ArrayList([]const u8).init(gpa); |
| 29 | const iter = &std.mem.split(in, delim); |
| 30 | while (iter.next()) |str| { |
| 31 | try list.append(str); |
| 32 | } |
| 33 | return list.items; |
| 34 | } |
| 35 | |
| 36 | pub fn trim_prefix(in: []const u8, prefix: []const u8) []const u8 { |
| 37 | if (std.mem.startsWith(u8, in, prefix)) { |
| 38 | return in[prefix.len..]; |
| 39 | } |
| 40 | return in; |
| 41 | } |
| 42 | |
| 43 | pub fn does_file_exist(fpath: []const u8) bool { |
| 44 | const abs_path = std.fs.realpathAlloc(gpa, fpath) catch unreachable; |
| 45 | const file = std.fs.openFileAbsolute(abs_path, .{}) catch |e| switch (e) { |
| 46 | error.FileNotFound => return false, |
| 47 | else => unreachable, |
| 48 | }; |
| 49 | file.close(); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | pub fn _join(comptime delim: []const u8, comptime xs: [][]const u8) []const u8 { |
| 54 | var buf: []const u8 = ""; |
| 55 | for (xs) |x,i| { |
| 56 | buf = buf ++ x; |
| 57 | if (i < xs.len-1) buf = buf ++ delim; |
| 58 | } |
| 59 | return buf; |
| 60 | } |