authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:10:18 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:10:18 -08:00
log7ae4d3f5b97b4d100d8bbd719f0db79c0ed993c1
tree02a03885c84476bc5ac3aaa215952e61d0fcbcbd
parente8254d11e5346de06a38f74ec7531958bdbc16a1

zig: add util/funcs


2 files changed, 63 insertions(+), 0 deletions(-)

src/util/funcs.zig created+60
...@@ -0,0 +1,60 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const u = @import("index.zig");
5
6//
7//
8
9pub fn print(comptime fmt: []const u8, args: anytype) void {
10 std.debug.print(fmt++"\n", args);
11}
12
13pub 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
20pub 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
27pub 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
36pub 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
43pub 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
53pub 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}
src/util/index.zig created+3
...@@ -0,0 +1,3 @@
1usingnamespace @import("./ascii.zig");
2usingnamespace @import("./ansi.zig");
3usingnamespace @import("./funcs.zig");