authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-11-24 01:02:25 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-11-24 01:02:25 -08:00
log271e7f758da34d2865cec4220e009bff6baccb92
treebb5e04773efa8b1a0e379737a9a2f808e4c6a8be
parente545025a089e4512a95c147ba4a2773553817c74

util: make random_string not need an allocator anymore


7 files changed, 8 insertions(+), 8 deletions(-)

src/cmd/aquila/install.zig+1-1
...@@ -20,7 +20,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {...@@ -20,7 +20,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
20 defer f.close();20 defer f.close();
21 const w = f.writer();21 const w = f.writer();
22 const init = @import("../init.zig");22 const init = @import("../init.zig");
23 try init.writeExeManifest(w, try u.random_string(gpa, 48), "zigmod_installation", null, null);23 try init.writeExeManifest(w, &u.random_string(48), "zigmod_installation", null, null);
24 }24 }
2525
26 // add to ~/zigmod.yml for later26 // add to ~/zigmod.yml for later
src/cmd/aquila/update.zig+1-1
...@@ -20,7 +20,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {...@@ -20,7 +20,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
20 defer f.close();20 defer f.close();
21 const w = f.writer();21 const w = f.writer();
22 const init = @import("../init.zig");22 const init = @import("../init.zig");
23 try init.writeExeManifest(w, try u.random_string(gpa, 48), "zigmod_installation", null, null);23 try init.writeExeManifest(w, &u.random_string(48), "zigmod_installation", null, null);
24 }24 }
25 u.assert(args.len == 0, "zigmod aq update accepts no parameters", .{});25 u.assert(args.len == 0, "zigmod aq update accepts no parameters", .{});
2626
src/cmd/init.zig+1-1
...@@ -26,7 +26,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {...@@ -26,7 +26,7 @@ pub fn execute(self_name: []const u8, args: [][]u8) !void {
26 const stdin = std.io.getStdIn().reader();26 const stdin = std.io.getStdIn().reader();
27 const cwd = std.fs.cwd();27 const cwd = std.fs.cwd();
2828
29 const id = try inquirer.answer(stdout, "ID (this gets autogenerated):", string, "{s}", try u.random_string(gpa, 48));29 const id = try inquirer.answer(stdout, "ID (this gets autogenerated):", string, "{s}", &u.random_string(48));
3030
31 const ptype = try inquirer.forEnum(stdout, stdin, "Are you making an application or a library?", gpa, enum { exe, lib }, null);31 const ptype = try inquirer.forEnum(stdout, stdin, "Are you making an application or a library?", gpa, enum { exe, lib }, null);
3232
src/common.zig+1-1
...@@ -277,7 +277,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -277,7 +277,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
277 dd.for_build = d.for_build;277 dd.for_build = d.for_build;
278 const save = dd;278 const save = dd;
279 if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..];279 if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..];
280 if (dd.id.len == 0) dd.id = try u.random_string(options.alloc, 48);280 if (dd.id.len == 0) dd.id = &u.random_string(48);
281 if (d.name.len > 0) dd.name = d.name;281 if (d.name.len > 0) dd.name = d.name;
282 if (d.main.len > 0) dd.main = d.main;282 if (d.main.len > 0) dd.main = d.main;
283 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;283 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;
src/util/funcs.zig+2-2
...@@ -97,11 +97,11 @@ pub fn last(in: []string) !string {...@@ -97,11 +97,11 @@ pub fn last(in: []string) !string {
9797
98const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";98const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
9999
100pub fn random_string(alloc: std.mem.Allocator, len: usize) !string {100pub fn random_string(comptime len: usize) [len]u8 {
101 const now: u64 = @intCast(std.time.nanoTimestamp());101 const now: u64 = @intCast(std.time.nanoTimestamp());
102 var rand = std.rand.DefaultPrng.init(now);102 var rand = std.rand.DefaultPrng.init(now);
103 var r = rand.random();103 var r = rand.random();
104 var buf = try alloc.alloc(u8, len);104 var buf: [len]u8 = undefined;
105 var i: usize = 0;105 var i: usize = 0;
106 while (i < len) : (i += 1) {106 while (i < len) : (i += 1) {
107 buf[i] = alphabet[r.int(usize) % alphabet.len];107 buf[i] = alphabet[r.int(usize) % alphabet.len];
src/util/modfile.zig+1-1
...@@ -62,7 +62,7 @@ pub const ModFile = struct {...@@ -62,7 +62,7 @@ pub const ModFile = struct {
62 }62 }
6363
64 return Self{64 return Self{
65 .id = if (id.len == 0) try u.random_string(alloc, 48) else id,65 .id = if (id.len == 0) &u.random_string(48) else id,
66 .name = name,66 .name = name,
67 .main = main,67 .main = main,
68 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),68 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),
src/util/module.zig+1-1
...@@ -40,7 +40,7 @@ pub const Module = struct {...@@ -40,7 +40,7 @@ pub const Module = struct {
40 }40 }
41 return Module{41 return Module{
42 .type = dep.type,42 .type = dep.type,
43 .id = if (dep.id.len > 0) dep.id else try u.random_string(alloc, 48),43 .id = if (dep.id.len > 0) dep.id else &u.random_string(48),
44 .name = dep.name,44 .name = dep.name,
45 .main = dep.main,45 .main = dep.main,
46 .c_include_dirs = dep.c_include_dirs,46 .c_include_dirs = dep.c_include_dirs,