From 7ae4d3f5b97b4d100d8bbd719f0db79c0ed993c1 Mon Sep 17 00:00:00 2001 From: Meghan Date: Sat, 14 Nov 2020 02:10:18 -0800 Subject: [PATCH] zig: add util/funcs --- src/util/funcs.zig | 60 ++++++++++++++++++++++++++++++++++++++++++++++ src/util/index.zig | 3 +++ 2 files changed, 63 insertions(+) create mode 100644 src/util/funcs.zig create mode 100644 src/util/index.zig diff --git a/src/util/funcs.zig b/src/util/funcs.zig new file mode 100644 index 0000000000000000000000000000000000000000..20f688a754a0a98ae3ec1aaf6fc26051a275b3fe --- /dev/null +++ b/src/util/funcs.zig @@ -0,0 +1,60 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const u = @import("index.zig"); + +// +// + +pub fn print(comptime fmt: []const u8, args: anytype) void { + std.debug.print(fmt++"\n", args); +} + +pub fn assert(ok: bool, comptime fmt: []const u8, args: anytype) void { + if (!ok) { + print(comptime u.ansi.color.Fg(.Red, "error: " ++ fmt), args); + std.os.exit(1); + } +} + +pub fn try_index(comptime T: type, array: []T, n: usize, def: T) T { + if (array.len <= n) { + return def; + } + return array[n]; +} + +pub fn split(in: []const u8, delim: []const u8) ![][]const u8 { + const list = &std.ArrayList([]const u8).init(gpa); + const iter = &std.mem.split(in, delim); + while (iter.next()) |str| { + try list.append(str); + } + return list.items; +} + +pub fn trim_prefix(in: []const u8, prefix: []const u8) []const u8 { + if (std.mem.startsWith(u8, in, prefix)) { + return in[prefix.len..]; + } + return in; +} + +pub fn does_file_exist(fpath: []const u8) bool { + const abs_path = std.fs.realpathAlloc(gpa, fpath) catch unreachable; + const file = std.fs.openFileAbsolute(abs_path, .{}) catch |e| switch (e) { + error.FileNotFound => return false, + else => unreachable, + }; + file.close(); + return true; +} + +pub fn _join(comptime delim: []const u8, comptime xs: [][]const u8) []const u8 { + var buf: []const u8 = ""; + for (xs) |x,i| { + buf = buf ++ x; + if (i < xs.len-1) buf = buf ++ delim; + } + return buf; +} diff --git a/src/util/index.zig b/src/util/index.zig new file mode 100644 index 0000000000000000000000000000000000000000..d8dfd417af9a6a7c154e60012b0fd2291374767c --- /dev/null +++ b/src/util/index.zig @@ -0,0 +1,3 @@ +usingnamespace @import("./ascii.zig"); +usingnamespace @import("./ansi.zig"); +usingnamespace @import("./funcs.zig"); -- 2.54.0