From 5b580b86a3657415e5332119611b8eef56c98812 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 18 Sep 2022 11:06:01 -0700 Subject: [PATCH] dry: u.trim_prefix -> extras.trimPrefix --- deps.zig | 2 +- src/common.zig | 9 +++++---- src/util/dep.zig | 7 ++++--- src/util/funcs.zig | 10 ++-------- src/util/module.zig | 5 +++-- zig.mod | 1 + 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/deps.zig b/deps.zig index 7051f080f011c7a63dbe5570e36e4de8f6c4a74c..f8cbe8ed32e4bccee6f4b8c242cc2304c0b7eaec 100644 --- a/deps.zig +++ b/deps.zig @@ -249,7 +249,7 @@ pub const package_data = struct { pub var _89ujp8gq842x = Package{ .name = "zigmod", .entry = "/../..//src/lib.zig", - .deps = &[_]*Package{ &_g982zq6e8wsv, &_s84v9o48ucb0, &_2ta738wrqbaq, &_0npcrzfdlrvk, &_ejw82j2ipa0e, &_ocmr9rtohgcc, &_tnj3qf44tpeq, &_2ovav391ivak, &_c1xirp1ota5p, &_u7sysdckdymi, &_iecwp4b3bsfm }, + .deps = &[_]*Package{ &_g982zq6e8wsv, &_s84v9o48ucb0, &_2ta738wrqbaq, &_0npcrzfdlrvk, &_ejw82j2ipa0e, &_ocmr9rtohgcc, &_tnj3qf44tpeq, &_2ovav391ivak, &_c1xirp1ota5p, &_u7sysdckdymi, &_iecwp4b3bsfm, &_f7dubzb7cyqe }, }; pub var _root = Package{ }; diff --git a/src/common.zig b/src/common.zig index e88b88cf8af776642fab53a1d864be441a0cfac9..77dac0806bd7c73011907c017ba84e577c0e7281 100644 --- a/src/common.zig +++ b/src/common.zig @@ -2,6 +2,7 @@ const std = @import("std"); const string = []const u8; const builtin = @import("builtin"); const ansi = @import("ansi"); +const extras = @import("extras"); const zigmod = @import("./lib.zig"); const u = @import("./util/index.zig"); @@ -243,7 +244,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO error.ManifestNotFound => { if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0 or d.keep) { var mod_from = try zigmod.Module.from(options.alloc, d.*, modpath, options); - if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..]; + if (d.type != .local) mod_from.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; if (mod_from.is_for_this()) return mod_from; return null; } @@ -257,7 +258,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO d.*.name = tryname; d.*.main = trymain.?; var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options); - if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..]; + if (d.type != .local) mod_from.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; if (mod_from.is_for_this()) return mod_from; return null; } @@ -268,7 +269,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO dd.dep = d.*; dd.for_build = d.for_build; const save = dd; - if (d.type != .local) dd.clean_path = u.trim_prefix(modpath, cachepath)[1..]; + if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; if (dd.id.len == 0) dd.id = try u.random_string(options.alloc, 48); if (d.name.len > 0) dd.name = d.name; if (d.main.len > 0) dd.main = d.main; @@ -306,7 +307,7 @@ pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, pkg_name: const cwdpath = try std.fs.cwd().realpathAlloc(alloc, "."); const mpath = try mdir.realpathAlloc(alloc, "."); - var fpath = u.trim_prefix(mpath, cwdpath); + var fpath = extras.trimPrefix(mpath, cwdpath); if (fpath.len == 0) fpath = std.fs.path.sep_str; var cachedir = try std.fs.cwd().openDir(cachepath, .{}); diff --git a/src/util/dep.zig b/src/util/dep.zig index e4417e8c9747065d0496cca2e11485247a0ea304..b111202eb8dc82413b58842d6428f9f74b98dcc4 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -2,6 +2,7 @@ const std = @import("std"); const string = []const u8; const builtin = @import("builtin"); const yaml = @import("yaml"); +const extras = @import("extras"); const zigmod = @import("../lib.zig"); const u = @import("index.zig"); @@ -36,9 +37,9 @@ pub const Dep = struct { return if (self.path.len == 0) "../.." else self.path; } var p = self.path; - p = u.trim_prefix(p, "http://"); - p = u.trim_prefix(p, "https://"); - p = u.trim_prefix(p, "git://"); + p = extras.trimPrefix(p, "http://"); + p = extras.trimPrefix(p, "https://"); + p = extras.trimPrefix(p, "git://"); p = u.trim_suffix(p, ".git"); p = try std.mem.join(alloc, "/", &.{ @tagName(self.type), p }); return p; diff --git a/src/util/funcs.zig b/src/util/funcs.zig index cabf0fafd103f477dcb358c74df8ae586a297083..dda8f4ee0bf52660a5808100874bd628f1d1cd04 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -1,5 +1,6 @@ const std = @import("std"); const string = []const u8; +const extras = @import("extras"); const u = @import("index.zig"); @@ -44,13 +45,6 @@ pub fn split(alloc: std.mem.Allocator, in: string, delim: string) ![]string { return list.toOwnedSlice(); } -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(dir: ?std.fs.Dir, fpath: string) !bool { const file = (dir orelse std.fs.cwd()).openFile(fpath, .{}) catch |e| switch (e) { error.FileNotFound => return false, @@ -255,7 +249,7 @@ pub fn detect_pkgname(alloc: std.mem.Allocator, override: string, dir: string) ! const dpath = try std.fs.realpathAlloc(alloc, try std.fs.path.join(alloc, &.{ dir, "build.zig" })); const splitP = try split(alloc, dpath, std.fs.path.sep_str); var name = splitP[splitP.len - 2]; - name = trim_prefix(name, "zig-"); + name = extras.trimPrefix(name, "zig-"); assert(name.len > 0, "package name must not be an empty string", .{}); return name; } diff --git a/src/util/module.zig b/src/util/module.zig index 9113ea7263b46af573b5dec0fb86540c731b0a77..600edbddd2b386df213903ea2d6139d72bd7b22d 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -2,6 +2,7 @@ const std = @import("std"); const string = []const u8; const builtin = @import("builtin"); const yaml = @import("yaml"); +const extras = @import("extras"); const zigmod = @import("../lib.zig"); const u = @import("index.zig"); @@ -67,8 +68,8 @@ pub const Module = struct { var file_list_2 = std.ArrayList(string).init(alloc); defer file_list_2.deinit(); for (file_list_1) |item| { - const _a = u.trim_prefix(item, cdpath); - const _b = u.trim_prefix(_a, self.clean_path); + const _a = extras.trimPrefix(item, cdpath); + const _b = extras.trimPrefix(_a, self.clean_path); if (_b[0] == '.') continue; try file_list_2.append(_b); } diff --git a/zig.mod b/zig.mod index fbadcc777fc35e54709775dfdb57b9c030622696..dd0888146dd222a17e049320db78e03a7b2c9647 100644 --- a/zig.mod +++ b/zig.mod @@ -16,6 +16,7 @@ dependencies: - src: git https://github.com/nektro/zig-inquirer - src: git https://github.com/nektro/arqv-ini - src: git https://github.com/nektro/zig-time + - src: git https://github.com/nektro/zig-extras root_dependencies: - src: git https://github.com/marlersoft/zigwin32 -- 2.54.0