Zig utility library
1
fork

Configure Feed

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

learned that `std.fs.path` exists

IamPyu 2bba5686 e187b9e0

-83
-81
src/path.zig
··· 1 - const builtin = @import("builtin"); 2 - const std = @import("std"); 3 - const mem = std.mem; 4 - 5 - const is_windows = builtin.os.tag == .windows; 6 - 7 - /// The path delimeter of the target system 8 - pub const path_delim: u8 = if (is_windows) '\\' else '/'; 9 - 10 - /// Return the filename of `path` 11 - pub fn basename(path: []const u8) []const u8 { 12 - if (mem.lastIndexOfScalar(u8, path, path_delim)) |index| 13 - return path[index + 1 ..]; 14 - return path; 15 - } 16 - 17 - /// Returns the extension of `path`'s filename 18 - pub fn getFileExt(path: []const u8) ?[]const u8 { 19 - const filename = basename(path); 20 - 21 - if (mem.startsWith(u8, filename, ".") and 22 - mem.count(u8, filename, ".") > 1) 23 - return getFileExt(filename[1..]); 24 - 25 - if (mem.indexOf(u8, filename, ".")) |index| 26 - return filename[index..]; 27 - 28 - return null; 29 - } 30 - 31 - /// Returns `path`'s filename without the extension 32 - pub fn stripFileExt(path: []const u8) []const u8 { 33 - const filename = basename(path); 34 - 35 - if (getFileExt(filename)) |ext| 36 - return filename[0..(filename.len - ext.len)]; 37 - 38 - return filename; 39 - } 40 - 41 - /// Returns the parent component of `path` 42 - pub fn getDirectory(path: []const u8) ?[]const u8 { 43 - if (mem.lastIndexOfScalar(u8, path, path_delim)) |index| 44 - return path[0..index]; 45 - 46 - return null; 47 - } 48 - 49 - /// Returns whether `path` is absolute 50 - pub fn isAbsolute(path: []const u8) bool { 51 - // TODO: implement isAbsolute 52 - _ = path; 53 - return false; 54 - } 55 - 56 - test "basename" { 57 - if (is_windows) { 58 - try std.testing.expectEqualStrings("file", basename("\\nested\\directory\\tree\\file")); 59 - } else { 60 - try std.testing.expectEqualStrings("file", basename("/nested/directory/tree/file")); 61 - } 62 - } 63 - 64 - test "ext" { 65 - if (!is_windows) { 66 - try std.testing.expectEqualStrings(".txt", getFileExt("hello.txt") orelse ""); 67 - try std.testing.expectEqualStrings(".tar.gz", getFileExt("/dl/archive.tar.gz") orelse ""); 68 - try std.testing.expectEqualStrings(".py", getFileExt("/dir/.file.py") orelse ""); 69 - try std.testing.expectEqualStrings(".bin", getFileExt(".bin") orelse ""); 70 - 71 - try std.testing.expectEqualStrings("hello", stripFileExt("hello.txt")); 72 - try std.testing.expectEqualStrings(".hidden", stripFileExt("/dir/.hidden.txt")); 73 - } 74 - } 75 - 76 - test "dir" { 77 - if (!is_windows) { 78 - try std.testing.expectEqualStrings("/etc", getDirectory("/etc/passwd") orelse ""); 79 - try std.testing.expectEqualStrings("/nested/directory", getDirectory("/nested/directory/") orelse ""); 80 - } 81 - }
-2
src/root.zig
··· 9 9 10 10 pub const StringBuilder = @import("./StringBuilder.zig"); 11 11 12 - pub const path = @import("./path.zig"); 13 12 pub const mem = @import("./mem.zig"); 14 13 15 14 const slice_iterator = @import("./slice_iterator.zig"); ··· 25 24 _ = packet; 26 25 _ = sparse_set; 27 26 _ = StringBuilder; 28 - _ = path; 29 27 _ = mem; 30 28 _ = slice_iterator; 31 29 _ = queue;