Skip to content

Commit

Permalink
dry: u.trim_prefix -> extras.trimPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Sep 18, 2022
1 parent ef97218 commit 5b580b8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion deps.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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{
};
Expand Down
9 changes: 5 additions & 4 deletions src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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, .{});
Expand Down
7 changes: 4 additions & 3 deletions src/util/dep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 2 additions & 8 deletions src/util/funcs.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const string = []const u8;
const extras = @import("extras");

const u = @import("index.zig");

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/util/module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions zig.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5b580b8

Please sign in to comment.