authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-09-03 15:36:44 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-09-03 15:36:44 -07:00
log21e2ffd96b6390a15f214cd33fff7b8f80e11d65
tree44689c2400be21af2f63a2a43f6ebfa823b2b808
parent4d2b9a70c8347fd5ce877eb087e1b3cad54e3cae

add randomSlice


1 files changed, 11 insertions(+), 0 deletions(-)

src/lib.zig+11
...@@ -30,3 +30,14 @@ pub fn addSentinel(alloc: *std.mem.Allocator, comptime T: type, input: []const T...@@ -30,3 +30,14 @@ pub fn addSentinel(alloc: *std.mem.Allocator, comptime T: type, input: []const T
30 const str = list.toOwnedSlice();30 const str = list.toOwnedSlice();
31 return str[0 .. str.len - 1 :sentinel];31 return str[0 .. str.len - 1 :sentinel];
32}32}
33
34const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
35
36pub fn randomSlice(alloc: *std.mem.Allocator, rand: *std.rand.Random, comptime T: type, len: usize) ![]const T {
37 var buf = try alloc.alloc(T, len);
38 var i: usize = 0;
39 while (i < len) : (i += 1) {
40 buf[i] = alphabet[rand.int(usize) % alphabet.len];
41 }
42 return buf;
43}