Zig utility library
1
fork

Configure Feed

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

`StringBuilder` improvements

IamPyu fe7c0504 72303629

+52 -13
+44 -5
src/StringBuilder.zig
··· 58 58 self.ptr = self.size; 59 59 } 60 60 61 + /// Append a single character to the string builder 62 + pub fn appendChar(self: *Self, c: u8) !void { 63 + const new_size = self.size + 1; 64 + try self.resize(new_size); 65 + 66 + const p = @as(*u8, @ptrCast(self.buf.ptr + self.ptr)); 67 + p.* = c; 68 + 69 + self.size = new_size; 70 + self.ptr = self.size; 71 + } 72 + 61 73 /// Join this string builder with another 62 74 pub fn join(self: *Self, other: *const Self) !void { 63 75 try self.append(other.buf[0..other.size]); ··· 65 77 66 78 /// Repeat the contents of the string builder `n` times 67 79 pub fn repeat(self: *Self, n: usize) !void { 68 - if (n == 0) { 80 + if (n == 0) 69 81 return; 70 - } 71 82 72 83 const orig_size = self.size; 73 84 const new_size = orig_size * n; ··· 77 88 defer self.allocator.free(tmpbuf); 78 89 @memcpy(tmpbuf, self.buf[0..orig_size]); 79 90 80 - for (0..n) |_| { 91 + for (0..n) |_| 81 92 try self.append(tmpbuf); 82 - } 83 93 } 84 94 85 95 /// Clear the buffer of the string builder ··· 88 98 self.size = 0; 89 99 @memset(self.buf, 0); 90 100 } 101 + 102 + /// Remove the most recent character from the string 103 + pub fn pop(self: *Self) void { 104 + self.size -= 1; 105 + self.ptr = self.size; 106 + } 107 + 108 + /// Repeat `pop` `n` times 109 + pub fn popMany(self: *Self, n: usize) void { 110 + if (n == 0) 111 + return; 112 + 113 + for (0..n) |_| 114 + self.pop(); 115 + } 91 116 }; 92 117 93 118 test "append" { 94 119 var sb = try StringBuilder.init(std.testing.allocator, 5); 95 120 defer sb.deinit(); 96 - try sb.append("Hello World!"); 121 + try sb.append("Hello World"); 122 + try sb.appendChar('!'); 97 123 98 124 const str = try sb.build(std.testing.allocator); 99 125 defer std.testing.allocator.free(str); ··· 156 182 157 183 try std.testing.expectEqualStrings("Hello World!", str); 158 184 } 185 + 186 + test "pop" { 187 + var sb = try StringBuilder.init(std.testing.allocator, 15); 188 + defer sb.deinit(); 189 + try sb.append("cool"); 190 + try sb.append("abc"); 191 + sb.popMany(3); 192 + 193 + const str = try sb.build(std.testing.allocator); 194 + defer std.testing.allocator.free(str); 195 + 196 + try std.testing.expectEqualStrings("cool", str); 197 + }
+8 -8
src/path.zig
··· 2 2 const std = @import("std"); 3 3 const mem = std.mem; 4 4 5 - const IS_WINDOWS = builtin.os.tag == .windows; 5 + const is_windows = builtin.os.tag == .windows; 6 6 7 7 /// The path delimeter of the target system 8 - pub const PATH_DELIM: u8 = if (IS_WINDOWS) '\\' else '/'; 8 + pub const path_delim: u8 = if (is_windows) '\\' else '/'; 9 9 10 10 /// Return the filename of `path` 11 11 pub fn basename(path: []const u8) []const u8 { 12 - if (std.mem.lastIndexOfScalar(u8, path, PATH_DELIM)) |index| { 12 + if (mem.lastIndexOfScalar(u8, path, path_delim)) |index| { 13 13 return path[index + 1 ..]; 14 14 } 15 15 return path; ··· 25 25 return getFileExt(filename[1..]); 26 26 } 27 27 28 - if (std.mem.indexOf(u8, filename, ".")) |index| { 28 + if (mem.indexOf(u8, filename, ".")) |index| { 29 29 return filename[index..]; 30 30 } 31 31 ··· 45 45 46 46 /// Returns the parent component of `path` 47 47 pub fn getDirectory(path: []const u8) ?[]const u8 { 48 - if (mem.lastIndexOfScalar(u8, path, PATH_DELIM)) |index| { 48 + if (mem.lastIndexOfScalar(u8, path, path_delim)) |index| { 49 49 return path[0..index]; 50 50 } 51 51 ··· 60 60 } 61 61 62 62 test "basename" { 63 - if (IS_WINDOWS) { 63 + if (is_windows) { 64 64 try std.testing.expectEqualStrings("file", basename("\\nested\\directory\\tree\\file")); 65 65 } else { 66 66 try std.testing.expectEqualStrings("file", basename("/nested/directory/tree/file")); ··· 68 68 } 69 69 70 70 test "ext" { 71 - if (!IS_WINDOWS) { 71 + if (!is_windows) { 72 72 try std.testing.expectEqualStrings(".txt", getFileExt("hello.txt") orelse ""); 73 73 try std.testing.expectEqualStrings(".tar.gz", getFileExt("/dl/archive.tar.gz") orelse ""); 74 74 try std.testing.expectEqualStrings(".py", getFileExt("/dir/.file.py") orelse ""); ··· 80 80 } 81 81 82 82 test "dir" { 83 - if (!IS_WINDOWS) { 83 + if (!is_windows) { 84 84 try std.testing.expectEqualStrings("/etc", getDirectory("/etc/passwd") orelse ""); 85 85 try std.testing.expectEqualStrings("/nested/directory", getDirectory("/nested/directory/") orelse ""); 86 86 }