move current expandHome functionality into separate expandHomeUnix function

This commit is contained in:
z-jxy
2024-12-28 21:06:56 -05:00
committed by Mitchell Hashimoto
parent 138a8f1602
commit 5ae2cc01ac
2 changed files with 22 additions and 18 deletions

View File

@@ -115,7 +115,16 @@ fn trimSpace(input: []const u8) []const u8 {
/// Errors if `home` fails or if the size of the expanded path is larger than `buf.len`.
///
/// Returns null if the value returned from `home` is null, otherwise returns a slice to the expanded path.
pub fn expandHome(path: []const u8, buf: []u8) !?[]u8 {
pub inline fn expandHome(path: []const u8, buf: []u8) !?[]u8 {
return switch (builtin.os.tag) {
inline .linux, .macos => expandHomeUnix(path, buf),
.ios => return null,
else => @compileError("unimplemented"),
};
}
fn expandHomeUnix(path: []const u8, buf: []u8) !?[]u8 {
if (!std.mem.startsWith(u8, path, "~/")) return null;
const home_dir = try home(buf) orelse return null;
const rest = path[1..]; // Skip the ~
const expanded_len = home_dir.len + rest.len;