Zig utility library
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Rename the project

IamPyu 38873d91 d8f7713c

+37 -16
+1 -1
README.md
··· 1 - # laz 1 + # mitochondria 2 2 3 3 Zig utility library providing data structures, linear algebra, and helper functions
+3 -3
build.zig
··· 4 4 const target = b.standardTargetOptions(.{}); 5 5 const optimize = b.standardOptimizeOption(.{}); 6 6 7 - const mod = b.addModule("laz", .{ 7 + const mod = b.addModule("mitochondria", .{ 8 8 .root_source_file = b.path("src/root.zig"), 9 9 .target = target, 10 10 .optimize = optimize, 11 11 }); 12 - mod.addImport("laz", mod); 12 + mod.addImport("mitochondria", mod); 13 13 14 14 const lib = b.addLibrary(.{ 15 - .name = "laz", 15 + .name = "mitochondria", 16 16 .root_module = mod, 17 17 }); 18 18
+2 -2
build.zig.zon
··· 1 1 .{ 2 - .name = .laz, 2 + .name = .mitochondria, 3 3 .version = "0.1.0", 4 4 // Together with name, this represents a globally unique package 5 5 // identifier. This field is generated by the Zig toolchain when the ··· 13 13 // original project's identity. Thus it is recommended to leave the comment 14 14 // on the following line intact, so that it shows up in code reviews that 15 15 // modify the field. 16 - .fingerprint = 0x72ba2992beecd756, // Changing this has security and trust implications. 16 + .fingerprint = 0xceefe49077877544, // Changing this has security and trust implications. 17 17 .minimum_zig_version = "0.15.2", 18 18 .dependencies = .{}, 19 19 .paths = .{
+2 -2
flake.nix
··· 29 29 formatter = pkgs.alejandra; 30 30 31 31 packages = rec { 32 - default = laz; 33 - laz = pkgs.callPackage ./default.nix {}; 32 + default = mitochondria; 33 + mitochondria = pkgs.callPackage ./default.nix {}; 34 34 }; 35 35 36 36 devShells.default = pkgs.mkShell {
+9
src/SparseSet.zig
··· 138 138 const i = try list.insert("bad message"); 139 139 _ = list.remove(i); 140 140 _ = try list.insert("it's a beautiful day"); 141 + 142 + const SliceIterator = @import("mitochondria").SliceIterator; 143 + 144 + var iter = SliceIterator([]const u8).init(list.asSlice()); 145 + 146 + while (iter.next()) |item| { 147 + std.debug.print("{s} ", .{item}); 148 + } 149 + std.debug.print("\n", .{}); 141 150 }
+20 -8
src/StringBuilder.zig
··· 1 1 const std = @import("std"); 2 - const mem = std.mem; 3 2 const Allocator = std.mem.Allocator; 4 3 5 4 pub const StringBuilder = struct { ··· 35 34 return str; 36 35 } 37 36 38 - /// Reserve additional space for the string builder 39 - pub fn reserveSpace(self: *Self, size: usize) !void { 37 + /// Reserve additional space for the string builder or reduce its size. 38 + pub fn resize(self: *Self, size: usize) !void { 40 39 if (size < self.capacity) { 40 + self.capacity = size; 41 + self.buf = try self.allocator.realloc(self.buf, self.capacity); 41 42 return; 42 43 } 43 44 44 - const capacity = size * 2; 45 - self.buf = try self.allocator.realloc(self.buf, capacity); 46 - self.capacity = capacity; 45 + self.capacity = size * 2; 46 + self.buf = try self.allocator.realloc(self.buf, self.capacity); 47 47 } 48 48 49 49 /// Append a slice to the string builder 50 50 pub fn append(self: *Self, buf: []const u8) !void { 51 51 const new_size = self.size + buf.len; 52 - try self.reserveSpace(new_size); 52 + try self.resize(new_size); 53 53 54 54 const p = self.buf.ptr + self.ptr; 55 55 @memcpy(p, buf); ··· 71 71 72 72 const orig_size = self.size; 73 73 const new_size = orig_size * n; 74 - try self.reserveSpace(new_size); 74 + try self.resize(new_size); 75 75 76 76 const tmpbuf = try self.allocator.alloc(u8, orig_size); 77 77 defer self.allocator.free(tmpbuf); ··· 144 144 145 145 try std.testing.expectEqualStrings("", str); 146 146 } 147 + 148 + test "simple" { 149 + var sb = try StringBuilder.init(std.testing.allocator, 15); 150 + defer sb.deinit(); 151 + try sb.append("Hello"); 152 + try sb.append(" World!"); 153 + 154 + const str = try sb.build(std.testing.allocator); 155 + defer std.testing.allocator.free(str); 156 + 157 + try std.testing.expectEqualStrings("Hello World!", str); 158 + }