From f844b67f8110f601bf6a4ef0d573635dabd7f81d Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 22 Oct 2021 18:10:05 -0700 Subject: [PATCH] alias `[]const u8` -> `string` kept as lowercase so we know its still a 'primitive' type --- build.zig | 3 +- src/cmd/aq.zig | 3 +- src/cmd/aquila/add.zig | 3 +- src/cmd/aquila/install.zig | 3 +- src/cmd/ci.zig | 3 +- src/cmd/fetch.zig | 5 ++- src/cmd/init.zig | 15 +++++---- src/cmd/version.zig | 1 - src/cmd/zpm.zig | 15 +++++---- src/common.zig | 28 ++++++++-------- src/main.zig | 3 +- src/util/dep.zig | 27 ++++++++------- src/util/dep_type.zig | 7 ++-- src/util/funcs.zig | 69 +++++++++++++++++++------------------- src/util/modfile.zig | 27 ++++++++------- src/util/module.zig | 33 +++++++++--------- src/util/yaml.zig | 27 ++++++++------- 17 files changed, 142 insertions(+), 130 deletions(-) diff --git a/build.zig b/build.zig index c6445774ad339276902fb546525337f55c34ffa8..1796c122f0a1fdec3e485584bf3697d5e8edaf1f 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const builtin = @import("builtin"); const deps = @import("./deps.zig"); @@ -19,7 +20,7 @@ pub fn build(b: *std.build.Builder) void { const exe_options = b.addOptions(); exe.addOptions("build_options", exe_options); - exe_options.addOption([]const u8, "version", b.option([]const u8, "tag", "") orelse "dev"); + exe_options.addOption(string, "version", b.option(string, "tag", "") orelse "dev"); exe_options.addOption(bool, "bootstrap", bootstrap != null); if (bootstrap != null) { diff --git a/src/cmd/aq.zig b/src/cmd/aq.zig index 9b164cd48b6ceeb948ab304f60fe9ec875a097a6..c4fb9ae51b69e01b2269fe89c277024d0514c3da 100644 --- a/src/cmd/aq.zig +++ b/src/cmd/aq.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const zfetch = @import("zfetch"); @@ -42,7 +43,7 @@ pub fn execute(args: [][]u8) !void { u.fail("unknown command \"{s}\" for \"zigmod aq\"", .{args[0]}); } -pub fn server_fetch(url: []const u8) !json.Value { +pub fn server_fetch(url: string) !json.Value { const req = try zfetch.Request.init(gpa, url, null); defer req.deinit(); diff --git a/src/cmd/aquila/add.zig b/src/cmd/aquila/add.zig index 10edec4a5da6b4eec3c236676fae0673fbd24288..147f69ea71318466f2086e618b10d2d6a3e11aae 100644 --- a/src/cmd/aquila/add.zig +++ b/src/cmd/aquila/add.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const u = @import("./../../util/index.zig"); @@ -13,7 +14,7 @@ pub fn execute(args: [][]u8) !void { std.log.info("Successfully added package {s}", .{pkg_id}); } -pub fn do(dir: std.fs.Dir, pkg_id: []const u8) ![]const u8 { +pub fn do(dir: std.fs.Dir, pkg_id: string) !string { const url = try std.mem.join(gpa, "/", &.{ aq.server_root, pkg_id }); const val = try aq.server_fetch(url); diff --git a/src/cmd/aquila/install.zig b/src/cmd/aquila/install.zig index c951e14b5dd6fd56e1c726a872cb019d1bfbe1cc..b4f7687a7a070bc8171285a593169d3dc9d63da7 100644 --- a/src/cmd/aquila/install.zig +++ b/src/cmd/aquila/install.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const knownfolders = @import("known-folders"); @@ -54,7 +55,7 @@ pub fn execute(args: [][]u8) !void { try ci.do(modpath, moddir); // zig build - const argv: []const []const u8 = &.{ + const argv: []const string = &.{ "zig", "build", "--prefix", try std.fs.path.join(gpa, &.{ homepath, ".zigmod" }), "--cache-dir", try std.fs.path.join(gpa, &.{ cache.?, "zigmod", "zig" }), diff --git a/src/cmd/ci.zig b/src/cmd/ci.zig index 5f7262118746faf2fa17186eb1ef69acb602bb76..c3ec0d50a04bec951a05c5623c534e29c2dcc1dd 100644 --- a/src/cmd/ci.zig +++ b/src/cmd/ci.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const u = @import("./../util/index.zig"); @@ -15,7 +16,7 @@ pub fn execute(args: [][]u8) !void { try do(cachepath, dir); } -pub fn do(cachepath: []const u8, dir: std.fs.Dir) !void { +pub fn do(cachepath: string, dir: std.fs.Dir) !void { var options = common.CollectOptions{ .log = true, .update = false, diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index e62c0b62268f426fead304f4666847c326663726..03085c7d4dd2b56e227472a18e1eaaeebef2f1cd 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -1,7 +1,6 @@ const std = @import("std"); -const gpa = std.heap.c_allocator; - const string = []const u8; +const gpa = std.heap.c_allocator; const u = @import("./../util/index.zig"); const common = @import("./../common.zig"); @@ -360,7 +359,7 @@ fn print_imports(w: std.fs.File.Writer, m: u.Module, path: string) !void { } } -fn zig_name_from_pkg_name(name: []const u8) ![]const u8 { +fn zig_name_from_pkg_name(name: string) !string { var legal = name; legal = try std.mem.replaceOwned(u8, gpa, legal, "-", "_"); legal = try std.mem.replaceOwned(u8, gpa, legal, "/", "_"); diff --git a/src/cmd/init.zig b/src/cmd/init.zig index 32b49b356fc2dc597ffc640c93b3c297f3a4d502..96a5d8487f01420e1778574badc40c36e8d20b80 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const inquirer = @import("inquirer"); @@ -25,18 +26,18 @@ pub fn execute(args: [][]u8) !void { const stdin = std.io.getStdIn().reader(); const cwd = std.fs.cwd(); - const id = try inquirer.answer(stdout, "ID (this gets autogenerated):", []const u8, "{s}", try u.random_string(48)); + const id = try inquirer.answer(stdout, "ID (this gets autogenerated):", string, "{s}", try u.random_string(48)); const ptype = try inquirer.forEnum(stdout, stdin, "Are you making an application or a library?", gpa, enum { exe, lib }, null); - const name = try inquirer.forString(stdout, stdin, "package name:", gpa, u.detect_pkgname(u.try_index([]const u8, args, 0, ""), "") catch |err| switch (err) { + const name = try inquirer.forString(stdout, stdin, "package name:", gpa, u.detect_pkgname(u.try_index(string, args, 0, ""), "") catch |err| switch (err) { error.NoBuildZig => { u.fail("init requires a build.zig file", .{}); }, else => return err, }); - const entry = if (ptype == .lib) try inquirer.forString(stdout, stdin, "package entry point:", gpa, u.detct_mainfile(u.try_index([]const u8, args, 1, ""), null, name) catch |err| switch (err) { + const entry = if (ptype == .lib) try inquirer.forString(stdout, stdin, "package entry point:", gpa, u.detct_mainfile(u.try_index(string, args, 1, ""), null, name) catch |err| switch (err) { error.CantFindMain => null, else => return err, }) else null; @@ -100,7 +101,7 @@ pub fn execute(args: [][]u8) !void { realtext = try std.mem.replaceOwned(u8, gpa, realtext, "", try inquirer.answer( stdout, "year:", - []const u8, + string, "{s}", try std.fmt.allocPrint(gpa, "{d}", .{1970 + @divFloor(std.time.timestamp(), s_in_y)}), )); @@ -121,7 +122,7 @@ pub fn execute(args: [][]u8) !void { } } -pub fn writeExeManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, license: ?[]const u8, description: ?[]const u8) !void { +pub fn writeExeManifest(w: std.fs.File.Writer, id: string, name: string, license: ?string, description: ?string) !void { try w.print("id: {s}\n", .{id}); try w.print("name: {s}\n", .{name}); if (license) |_| try w.print("license: {s}\n", .{license.?}); @@ -129,7 +130,7 @@ pub fn writeExeManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, try w.print("dev_dependencies:\n", .{}); } -pub fn writeLibManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, entry: []const u8, license: []const u8, description: []const u8) !void { +pub fn writeLibManifest(w: std.fs.File.Writer, id: string, name: string, entry: string, license: string, description: string) !void { try w.print("id: {s}\n", .{id}); try w.print("name: {s}\n", .{name}); try w.print("main: {s}\n", .{entry}); @@ -138,7 +139,7 @@ pub fn writeLibManifest(w: std.fs.File.Writer, id: []const u8, name: []const u8, try w.print("dependencies:\n", .{}); } -fn guessCopyrightName() !?[]const u8 { +fn guessCopyrightName() !?string { const home = (try knownfolders.open(gpa, .home, .{})).?; if (!(try u.does_file_exist(".gitconfig", home))) return null; const file = try home.openFile(".gitconfig", .{}); diff --git a/src/cmd/version.zig b/src/cmd/version.zig index 8d73d42c9d1b3d7de537e21d965567f056dcffa4..e12659a57a1970a4869080db238098c4210b8260 100644 --- a/src/cmd/version.zig +++ b/src/cmd/version.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const string = []const u8; const gpa = std.heap.c_allocator; const builtin = @import("builtin"); diff --git a/src/cmd/zpm.zig b/src/cmd/zpm.zig index 13b6f0ac6b3536e659222aa324f970217c964326..95aac3463128248cac2e62fe52373e5d1ba9de84 100644 --- a/src/cmd/zpm.zig +++ b/src/cmd/zpm.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const zfetch = @import("zfetch"); @@ -19,12 +20,12 @@ pub const commands = struct { pub const server_root = "https://zpm.random-projects.net/api"; pub const Package = struct { - author: []const u8, - name: []const u8, - tags: [][]const u8, - git: []const u8, - root_file: ?[]const u8, - description: []const u8, + author: string, + name: string, + tags: []string, + git: string, + root_file: ?string, + description: string, }; pub fn execute(args: [][]u8) !void { @@ -53,7 +54,7 @@ pub fn execute(args: [][]u8) !void { u.fail("unknown command \"{s}\" for \"zigmod zpm\"", .{args[0]}); } -pub fn server_fetch(url: []const u8) !json.Value { +pub fn server_fetch(url: string) !json.Value { const req = try zfetch.Request.init(gpa, url, null); defer req.deinit(); try req.do(.GET, null, null); diff --git a/src/common.zig b/src/common.zig index 51cce6cbc44f6ca09f0980009fb76c1b191999b7..67159c9d70a632eeef1fa14e5e31c35856c76eb9 100644 --- a/src/common.zig +++ b/src/common.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const builtin = @import("builtin"); const gpa = std.heap.c_allocator; @@ -6,7 +7,6 @@ const ansi = @import("ansi"); const u = @import("./util/index.zig"); const yaml = @import("./util/yaml.zig"); -const string = []const u8; const root = @import("root"); const build_options = if (@hasDecl(root, "build_options")) root.build_options else struct {}; @@ -18,7 +18,7 @@ const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootst pub const CollectOptions = struct { log: bool, update: bool, - lock: ?[]const [4][]const u8 = null, + lock: ?[]const [4]string = null, alloc: *std.mem.Allocator = gpa, already_fetched: *std.ArrayList(string) = undefined, @@ -28,7 +28,7 @@ pub const CollectOptions = struct { } }; -pub fn collect_deps_deep(cachepath: []const u8, mdir: std.fs.Dir, options: *CollectOptions) !u.Module { +pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) !u.Module { const m = try u.ModFile.from_dir(gpa, mdir); try options.init(); var moduledeps = std.ArrayList(u.Module).init(gpa); @@ -55,7 +55,7 @@ pub fn collect_deps_deep(cachepath: []const u8, mdir: std.fs.Dir, options: *Coll }; } -pub fn collect_deps(cachepath: []const u8, mdir: std.fs.Dir, options: *CollectOptions) anyerror!u.Module { +pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!u.Module { const m = try u.ModFile.from_dir(gpa, mdir); var moduledeps = std.ArrayList(u.Module).init(gpa); defer moduledeps.deinit(); @@ -93,7 +93,7 @@ pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void } } -pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![]const u8 { +pub fn get_modpath(cachepath: string, d: u.Dep, options: *CollectOptions) !string { const p = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path() }); const pv = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path_v() }); @@ -197,7 +197,7 @@ pub fn get_modpath(cachepath: []const u8, d: u.Dep, options: *CollectOptions) ![ } } -pub fn get_module_from_dep(d: *u.Dep, cachepath: []const u8, options: *CollectOptions) anyerror!?u.Module { +pub fn get_module_from_dep(d: *u.Dep, cachepath: string, options: *CollectOptions) anyerror!?u.Module { if (options.lock) |lock| { for (lock) |item| { if (std.mem.eql(u8, item[0], try d.clean_path())) { @@ -275,11 +275,11 @@ pub fn get_module_from_dep(d: *u.Dep, cachepath: []const u8, options: *CollectOp } } -pub fn add_files_package(pkg_name: []const u8, mdir: std.fs.Dir, dirs: []const []const u8) !u.Module { +pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !u.Module { const destination = ".zigmod/deps/files"; const fname = try std.mem.join(gpa, "", &.{ pkg_name, ".zig" }); - const map = &std.StringHashMap([]const u8).init(gpa); + const map = &std.StringHashMap(string).init(gpa); defer map.deinit(); for (dirs) |dir_path| { @@ -310,7 +310,7 @@ pub fn add_files_package(pkg_name: []const u8, mdir: std.fs.Dir, dirs: []const [ ); try w.print("const srcpath = \"../../../{}\";\n\n", .{std.zig.fmtEscapes(fpath[1..])}); try w.writeAll( - \\const files = std.ComptimeStringMap([]const u8, .{ + \\const files = std.ComptimeStringMap(string, .{ \\ ); var iter = map.iterator(); @@ -321,7 +321,7 @@ pub fn add_files_package(pkg_name: []const u8, mdir: std.fs.Dir, dirs: []const [ try w.writeAll( \\}); \\ - \\pub fn open(comptime path: []const u8) ?[]const u8 { + \\pub fn open(comptime path: string) ?string { \\ if (path.len == 0) return null; \\ if (path[0] != '/') return null; \\ return files.get(path); @@ -346,8 +346,8 @@ pub fn add_files_package(pkg_name: []const u8, mdir: std.fs.Dir, dirs: []const [ return (try get_module_from_dep(&d, destination, &options)).?; } -pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4][]const u8 { - var list = std.ArrayList([4][]const u8).init(gpa); +pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string { + var list = std.ArrayList([4]string).init(gpa); const max = std.math.maxInt(usize); const f = try dir.openFile("zigmod.lock", .{}); const r = f.reader(); @@ -361,7 +361,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4][]const u8 { switch (v) { 1 => { var iter = std.mem.split(u8, line, " "); - try list.append([4][]const u8{ + try list.append([4]string{ iter.next().?, iter.next().?, iter.next().?, @@ -380,7 +380,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4][]const u8 { .yaml = null, .deps = &.{}, }; - try list.append([4][]const u8{ + try list.append([4]string{ try asdep.clean_path(), @tagName(asdep.type), asdep.path, diff --git a/src/main.zig b/src/main.zig index 4ff923a7e7a8bf436c077f4b936c82f58e646724..444035787a2b261f2166a31092ce0aa8f9172872 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const builtin = @import("builtin"); pub const build_options = @import("build_options"); @@ -55,7 +56,7 @@ pub fn main() !void { } } - var sub_cmd_args = std.ArrayList([]const u8).init(gpa); + var sub_cmd_args = std.ArrayList(string).init(gpa); try sub_cmd_args.append(try std.fmt.allocPrint(gpa, "zigmod-{s}", .{args[0]})); for (args[1..]) |item| { try sub_cmd_args.append(item); diff --git a/src/util/dep.zig b/src/util/dep.zig index a9e184b419ff76e77b152b68abd70846520469c8..34298edf5e2d47ef05b023f3da5f9d70bf387f61 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const builtin = std.builtin; @@ -12,21 +13,21 @@ pub const Dep = struct { const Self = @This(); type: u.DepType, - path: []const u8, + path: string, - id: []const u8, - name: []const u8, - main: []const u8, - version: []const u8, - c_include_dirs: []const []const u8 = &.{}, - c_source_flags: []const []const u8 = &.{}, - c_source_files: []const []const u8 = &.{}, - only_os: []const []const u8 = &.{}, - except_os: []const []const u8 = &.{}, + id: string, + name: string, + main: string, + version: string, + c_include_dirs: []const string = &.{}, + c_source_flags: []const string = &.{}, + c_source_files: []const string = &.{}, + only_os: []const string = &.{}, + except_os: []const string = &.{}, yaml: ?yaml.Mapping, deps: []u.Dep, - pub fn clean_path(self: Dep) ![]const u8 { + pub fn clean_path(self: Dep) !string { if (self.type == .local) { return if (self.path.len == 0) "../.." else self.path; } @@ -39,7 +40,7 @@ pub const Dep = struct { return p; } - pub fn clean_path_v(self: Dep) ![]const u8 { + pub fn clean_path_v(self: Dep) !string { if (self.type == .http and self.version.len > 0) { const i = std.mem.indexOf(u8, self.version, "-").?; return std.mem.join(gpa, "/", &.{ "v", try self.clean_path(), self.version[i + 1 .. 15] }); @@ -58,7 +59,7 @@ pub const Dep = struct { return true; } - pub fn exact_version(self: Dep, dpath: []const u8) ![]const u8 { + pub fn exact_version(self: Dep, dpath: string) !string { if (self.version.len == 0) { return try self.type.exact_version(dpath); } diff --git a/src/util/dep_type.zig b/src/util/dep_type.zig index 435f589b7de582293fd60cc4a0da5c423e7c6a63..189a90db91a456fcfb78151d6a5bbbedd756cce5 100644 --- a/src/util/dep_type.zig +++ b/src/util/dep_type.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const u = @import("./index.zig"); @@ -32,7 +33,7 @@ pub const DepType = enum { // hypercore, // https://hypercore-protocol.org/ // zig fmt: on - pub fn pull(self: DepType, rpath: []const u8, dpath: []const u8) !void { + pub fn pull(self: DepType, rpath: string, dpath: string) !void { switch (self) { .local => {}, .system_lib => {}, @@ -57,7 +58,7 @@ pub const DepType = enum { } // zig fmt: on - pub fn update(self: DepType, dpath: []const u8, rpath: []const u8) !void { + pub fn update(self: DepType, dpath: string, rpath: string) !void { _ = rpath; switch (self) { @@ -77,7 +78,7 @@ pub const DepType = enum { } // zig fmt: on - pub fn exact_version(self: DepType, mpath: []const u8) ![]const u8 { + pub fn exact_version(self: DepType, mpath: string) !string { var mdir = try std.fs.cwd().openDir(mpath, .{}); defer mdir.close(); return switch (self) { diff --git a/src/util/funcs.zig b/src/util/funcs.zig index 9f3c9be1ce2cb4b456e14df6b47f58fd3ec6e713..0f06ad6d82637320612f283f1003bce3f51c0069 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const u = @import("index.zig"); @@ -11,21 +12,21 @@ pub const kb = b * 1024; pub const mb = kb * 1024; pub const gb = mb * 1024; -pub fn print(comptime fmt: []const u8, args: anytype) void { +pub fn print(comptime fmt: string, args: anytype) void { std.debug.print(fmt ++ "\n", args); } const ansi_red = "\x1B[31m"; const ansi_reset = "\x1B[39m"; -pub fn assert(ok: bool, comptime fmt: []const u8, args: anytype) void { +pub fn assert(ok: bool, comptime fmt: string, args: anytype) void { if (!ok) { print(ansi_red ++ fmt ++ ansi_reset, args); std.os.exit(1); } } -pub fn fail(comptime fmt: []const u8, args: anytype) noreturn { +pub fn fail(comptime fmt: string, args: anytype) noreturn { assert(false, fmt, args); unreachable; } @@ -37,8 +38,8 @@ pub fn try_index(comptime T: type, array: []T, n: usize, def: T) T { return array[n]; } -pub fn split(in: []const u8, delim: []const u8) ![][]const u8 { - var list = std.ArrayList([]const u8).init(gpa); +pub fn split(in: string, delim: string) ![]string { + var list = std.ArrayList(string).init(gpa); defer list.deinit(); var iter = std.mem.split(u8, in, delim); while (iter.next()) |str| { @@ -47,14 +48,14 @@ pub fn split(in: []const u8, delim: []const u8) ![][]const u8 { return list.toOwnedSlice(); } -pub fn trim_prefix(in: []const u8, prefix: []const u8) []const u8 { +pub fn trim_prefix(in: string, prefix: string) string { if (std.mem.startsWith(u8, in, prefix)) { return in[prefix.len..]; } return in; } -pub fn does_file_exist(fpath: []const u8, dir: ?std.fs.Dir) !bool { +pub fn does_file_exist(fpath: string, dir: ?std.fs.Dir) !bool { const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) { error.FileNotFound => return false, error.IsDir => return true, @@ -64,7 +65,7 @@ pub fn does_file_exist(fpath: []const u8, dir: ?std.fs.Dir) !bool { return true; } -pub fn does_folder_exist(fpath: []const u8) !bool { +pub fn does_folder_exist(fpath: string) !bool { const file = std.fs.cwd().openFile(fpath, .{}) catch |e| switch (e) { error.FileNotFound => return false, error.IsDir => return true, @@ -78,8 +79,8 @@ pub fn does_folder_exist(fpath: []const u8) !bool { return true; } -pub fn _join(comptime delim: []const u8, comptime xs: [][]const u8) []const u8 { - var buf: []const u8 = ""; +pub fn _join(comptime delim: string, comptime xs: []string) string { + var buf: string = ""; for (xs) |x, i| { buf = buf ++ x; if (i < xs.len - 1) buf = buf ++ delim; @@ -87,15 +88,15 @@ pub fn _join(comptime delim: []const u8, comptime xs: [][]const u8) []const u8 { return buf; } -pub fn trim_suffix(in: []const u8, suffix: []const u8) []const u8 { +pub fn trim_suffix(in: string, suffix: string) string { if (std.mem.endsWith(u8, in, suffix)) { return in[0 .. in.len - suffix.len]; } return in; } -pub fn repeat(s: []const u8, times: i32) ![]const u8 { - var list = std.ArrayList([]const u8).init(gpa); +pub fn repeat(s: string, times: i32) !string { + var list = std.ArrayList(string).init(gpa); var i: i32 = 0; while (i < times) : (i += 1) { try list.append(s); @@ -103,15 +104,15 @@ pub fn repeat(s: []const u8, times: i32) ![]const u8 { return join(list.items, ""); } -pub fn join(xs: [][]const u8, delim: []const u8) ![]const u8 { - var res: []const u8 = ""; +pub fn join(xs: []string, delim: string) !string { + var res: string = ""; for (xs) |x, i| { res = try std.fmt.allocPrint(gpa, "{s}{s}{s}", .{ res, x, if (i < xs.len - 1) delim else "" }); } return res; } -pub fn concat(items: [][]const u8) ![]const u8 { +pub fn concat(items: []string) !string { return std.mem.join(gpa, "", items); } @@ -128,7 +129,7 @@ pub fn print_all(w: std.fs.File.Writer, items: anytype, ln: bool) !void { } } -pub fn list_contains(haystack: []const []const u8, needle: []const u8) bool { +pub fn list_contains(haystack: []const string, needle: string) bool { for (haystack) |item| { if (std.mem.eql(u8, item, needle)) { return true; @@ -146,7 +147,7 @@ pub fn list_contains_gen(comptime T: type, haystack: []const T, needle: T) bool return false; } -pub fn file_list(dpath: []const u8, list: *std.ArrayList([]const u8)) !void { +pub fn file_list(dpath: string, list: *std.ArrayList(string)) !void { const dir = try std.fs.cwd().openDir(dpath, .{ .iterate = true }); var walk = try dir.walk(gpa); defer walk.deinit(); @@ -162,7 +163,7 @@ pub fn file_list(dpath: []const u8, list: *std.ArrayList([]const u8)) !void { } } -pub fn run_cmd_raw(dir: ?[]const u8, args: []const []const u8) !std.ChildProcess.ExecResult { +pub fn run_cmd_raw(dir: ?string, args: []const string) !std.ChildProcess.ExecResult { return std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, .max_output_bytes = std.math.maxInt(usize) }) catch |e| switch (e) { error.FileNotFound => { u.fail("\"{s}\" command not found", .{args[0]}); @@ -171,15 +172,15 @@ pub fn run_cmd_raw(dir: ?[]const u8, args: []const []const u8) !std.ChildProcess }; } -pub fn run_cmd(dir: ?[]const u8, args: []const []const u8) !u32 { +pub fn run_cmd(dir: ?string, args: []const string) !u32 { const result = try run_cmd_raw(dir, args); gpa.free(result.stdout); gpa.free(result.stderr); return result.term.Exited; } -pub fn list_remove(input: [][]const u8, search: []const u8) ![][]const u8 { - var list = std.ArrayList([]const u8).init(gpa); +pub fn list_remove(input: []string, search: string) ![]string { + var list = std.ArrayList(string).init(gpa); defer list.deinit(); for (input) |item| { if (!std.mem.eql(u8, item, search)) { @@ -189,7 +190,7 @@ pub fn list_remove(input: [][]const u8, search: []const u8) ![][]const u8 { return list.toOwnedSlice(); } -pub fn last(in: [][]const u8) ![]const u8 { +pub fn last(in: []string) !string { if (in.len == 0) { return error.EmptyArray; } @@ -198,7 +199,7 @@ pub fn last(in: [][]const u8) ![]const u8 { const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"; -pub fn random_string(len: usize) ![]const u8 { +pub fn random_string(len: usize) !string { const now = @intCast(u64, std.time.nanoTimestamp()); var rand = std.rand.DefaultPrng.init(now); const r = &rand.random; @@ -210,14 +211,14 @@ pub fn random_string(len: usize) ![]const u8 { return buf; } -pub fn parse_split(comptime T: type, delim: []const u8) type { +pub fn parse_split(comptime T: type, delim: string) type { return struct { const Self = @This(); id: T, - string: []const u8, + string: string, - pub fn do(input: []const u8) !Self { + pub fn do(input: string) !Self { var iter = std.mem.split(u8, input, delim); return Self{ .id = std.meta.stringToEnum(T, iter.next() orelse return error.IterEmpty) orelse return error.NoMemberFound, @@ -233,7 +234,7 @@ pub const HashFn = enum { sha512, }; -pub fn validate_hash(input: []const u8, file_path: []const u8) !bool { +pub fn validate_hash(input: string, file_path: string) !bool { const hash = parse_split(HashFn, "-").do(input) catch return false; const file = try std.fs.cwd().openFile(file_path, .{}); defer file.close(); @@ -251,7 +252,7 @@ pub fn validate_hash(input: []const u8, file_path: []const u8) !bool { return result; } -pub fn do_hash(comptime algo: type, data: []const u8) ![]const u8 { +pub fn do_hash(comptime algo: type, data: string) !string { const h = &algo.init(.{}); var out: [algo.digest_length]u8 = undefined; h.update(data); @@ -261,7 +262,7 @@ pub fn do_hash(comptime algo: type, data: []const u8) ![]const u8 { } /// Returns the result of running `git rev-parse HEAD` -pub fn git_rev_HEAD(alloc: *std.mem.Allocator, dir: std.fs.Dir) ![]const u8 { +pub fn git_rev_HEAD(alloc: *std.mem.Allocator, dir: std.fs.Dir) !string { const max = std.math.maxInt(usize); const dirg = try dir.openDir(".git", .{}); const h = std.mem.trim(u8, try dirg.readFileAlloc(alloc, "HEAD", max), "\n"); @@ -275,7 +276,7 @@ pub fn slice(comptime T: type, input: []const T, from: usize, to: usize) []const return input[f..t]; } -pub fn detect_pkgname(override: []const u8, dir: []const u8) ![]const u8 { +pub fn detect_pkgname(override: string, dir: string) !string { if (override.len > 0) { return override; } @@ -291,7 +292,7 @@ pub fn detect_pkgname(override: []const u8, dir: []const u8) ![]const u8 { return name; } -pub fn detct_mainfile(override: []const u8, dir: ?std.fs.Dir, name: []const u8) ![]const u8 { +pub fn detct_mainfile(override: string, dir: ?std.fs.Dir, name: string) !string { if (override.len > 0) { if (try does_file_exist(override, dir)) { if (std.mem.endsWith(u8, override, ".zig")) { @@ -312,7 +313,7 @@ pub fn detct_mainfile(override: []const u8, dir: ?std.fs.Dir, name: []const u8) return error.CantFindMain; } -pub fn indexOfN(haystack: []const u8, needle: u8, n: usize) ?usize { +pub fn indexOfN(haystack: string, needle: u8, n: usize) ?usize { var i: usize = 0; var c: usize = 0; while (c < n) { @@ -322,7 +323,7 @@ pub fn indexOfN(haystack: []const u8, needle: u8, n: usize) ?usize { return i; } -pub fn indexOfAfter(haystack: []const u8, needle: u8, after: usize) ?usize { +pub fn indexOfAfter(haystack: string, needle: u8, after: usize) ?usize { for (haystack) |c, i| { if (i <= after) continue; if (c == needle) return i; diff --git a/src/util/modfile.zig b/src/util/modfile.zig index f9b5f314c63f902c70189bbf1309205e8cf40e9d..b7a2e9ee3ec2da69c683f84982aa644681553bd0 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const u = @import("index.zig"); const yaml = @import("./yaml.zig"); @@ -14,19 +15,19 @@ pub const ModFile = struct { const Self = @This(); alloc: *std.mem.Allocator, - id: []const u8, - name: []const u8, - main: []const u8, - c_include_dirs: []const []const u8, - c_source_flags: []const []const u8, - c_source_files: []const []const u8, + id: string, + name: string, + main: string, + c_include_dirs: []const string, + c_source_flags: []const string, + c_source_files: []const string, deps: []u.Dep, yaml: yaml.Mapping, devdeps: []u.Dep, - root_files: []const []const u8, - files: []const []const u8, + root_files: []const string, + files: []const string, - pub fn init(alloc: *std.mem.Allocator, mpath: []const u8) !Self { + pub fn init(alloc: *std.mem.Allocator, mpath: string) !Self { const file = try std.fs.cwd().openFile(mpath, .{}); defer file.close(); const input = try file.reader().readAllAlloc(alloc, mb); @@ -67,14 +68,14 @@ pub const ModFile = struct { }; } - fn dep_list_by_name(alloc: *std.mem.Allocator, mapping: yaml.Mapping, prop: []const u8) anyerror![]u.Dep { + fn dep_list_by_name(alloc: *std.mem.Allocator, mapping: yaml.Mapping, prop: string) anyerror![]u.Dep { var dep_list = std.ArrayList(u.Dep).init(alloc); if (mapping.get(prop)) |dep_seq| { if (dep_seq == .sequence) { for (dep_seq.sequence) |item| { - var dtype: []const u8 = undefined; - var path: []const u8 = undefined; - var version: ?[]const u8 = null; + var dtype: string = undefined; + var path: string = undefined; + var version: ?string = null; var name = item.mapping.get_string("name"); var main = item.mapping.get_string("main"); diff --git a/src/util/module.zig b/src/util/module.zig index c2fb89f5f1a25c6750c92808725a1006c2fbae54..bd7e64e8a1390cc109673504d0265b817a0c3cbe 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const gpa = std.heap.c_allocator; const builtin = @import("builtin"); @@ -11,20 +12,20 @@ const common = @import("./../common.zig"); pub const Module = struct { is_sys_lib: bool, - id: []const u8, - name: []const u8, - main: []const u8, - c_include_dirs: []const []const u8 = &.{}, - c_source_flags: []const []const u8 = &.{}, - c_source_files: []const []const u8 = &.{}, - only_os: []const []const u8 = &.{}, - except_os: []const []const u8 = &.{}, + id: string, + name: string, + main: string, + c_include_dirs: []const string = &.{}, + c_source_flags: []const string = &.{}, + c_source_files: []const string = &.{}, + only_os: []const string = &.{}, + except_os: []const string = &.{}, yaml: ?yaml.Mapping, deps: []Module, - clean_path: []const u8, + clean_path: string, dep: ?u.Dep, - pub fn from(dep: u.Dep, dir: []const u8, options: *common.CollectOptions) !Module { + pub fn from(dep: u.Dep, dir: string, options: *common.CollectOptions) !Module { var moddeps = std.ArrayList(Module).init(gpa); defer moddeps.deinit(); for (dep.deps) |*d| { @@ -53,12 +54,12 @@ pub const Module = struct { return std.mem.eql(u8, self.id, another.id); } - pub fn get_hash(self: Module, cdpath: []const u8) ![]const u8 { - var file_list_1 = std.ArrayList([]const u8).init(gpa); + pub fn get_hash(self: Module, cdpath: string) !string { + var file_list_1 = std.ArrayList(string).init(gpa); defer file_list_1.deinit(); try u.file_list(try u.concat(&.{ cdpath, "/", self.clean_path }), &file_list_1); - var file_list_2 = std.ArrayList([]const u8).init(gpa); + var file_list_2 = std.ArrayList(string).init(gpa); defer file_list_2.deinit(); for (file_list_1.items) |item| { const _a = u.trim_prefix(item, cdpath); @@ -67,8 +68,8 @@ pub const Module = struct { try file_list_2.append(_b); } - std.sort.sort([]const u8, file_list_2.items, void{}, struct { - pub fn lt(context: void, lhs: []const u8, rhs: []const u8) bool { + std.sort.sort(string, file_list_2.items, void{}, struct { + pub fn lt(context: void, lhs: string, rhs: string) bool { _ = context; return std.mem.lessThan(u8, lhs, rhs); } @@ -117,7 +118,7 @@ pub const Module = struct { return false; } - pub fn short_id(self: Module) []const u8 { + pub fn short_id(self: Module) string { return u.slice(u8, self.id, 0, 12); } }; diff --git a/src/util/yaml.zig b/src/util/yaml.zig index 32af0ca694c7935b9508a4a91a7495284267c5ca..592d482d32357f3fab140f7977314fc18ed2fed5 100644 --- a/src/util/yaml.zig +++ b/src/util/yaml.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const string = []const u8; const yaml = @import("yaml"); const c = @cImport({ @@ -9,7 +10,7 @@ const u = @import("./index.zig"); // // -const Array = []const []const u8; +const Array = []const string; pub const Stream = struct { docs: []const Document, @@ -25,10 +26,10 @@ pub const Item = union(enum) { mapping: Mapping, sequence: Sequence, document: Document, - string: []const u8, + string: string, stream: Stream, - pub fn format(self: Item, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { + pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { _ = fmt; _ = options; @@ -62,16 +63,16 @@ pub const Item = union(enum) { pub const Sequence = []const Item; pub const Key = struct { - key: []const u8, + key: string, value: Value, }; pub const Value = union(enum) { - string: []const u8, + string: string, mapping: Mapping, sequence: Sequence, - pub fn format(self: Value, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { + pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { _ = fmt; _ = options; @@ -98,7 +99,7 @@ pub const Value = union(enum) { pub const Mapping = struct { items: []const Key, - pub fn get(self: Mapping, k: []const u8) ?Value { + pub fn get(self: Mapping, k: string) ?Value { for (self.items) |item| { if (std.mem.eql(u8, item.key, k)) { return item.value; @@ -107,12 +108,12 @@ pub const Mapping = struct { return null; } - pub fn get_string(self: Mapping, k: []const u8) []const u8 { + pub fn get_string(self: Mapping, k: string) string { return if (self.get(k)) |v| v.string else ""; } - pub fn get_string_array(self: Mapping, alloc: *std.mem.Allocator, k: []const u8) ![][]const u8 { - var list = std.ArrayList([]const u8).init(alloc); + pub fn get_string_array(self: Mapping, alloc: *std.mem.Allocator, k: string) ![]string { + var list = std.ArrayList(string).init(alloc); defer list.deinit(); if (self.get(k)) |val| { if (val == .sequence) { @@ -127,7 +128,7 @@ pub const Mapping = struct { return list.toOwnedSlice(); } - pub fn format(self: Mapping, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { + pub fn format(self: Mapping, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { _ = fmt; _ = options; @@ -146,7 +147,7 @@ pub const TokenList = []const Token; // // -pub fn parse(alloc: *std.mem.Allocator, input: []const u8) !Document { +pub fn parse(alloc: *std.mem.Allocator, input: string) !Document { var parser: c.yaml_parser_t = undefined; _ = c.yaml_parser_initialize(&parser); @@ -291,7 +292,7 @@ fn parse_sequence(p: *Parser) Error!Sequence { } } -fn get_event_string(event: Token, lines: Array) []const u8 { +fn get_event_string(event: Token, lines: Array) string { const sm = event.start_mark; const em = event.end_mark; return lines[sm.line][sm.column..em.column]; -- 2.54.0