Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

windowsResolveAgainstCwd

path.windowsResolveAgainstCwd
fn windowsResolveAgainstCwd(
    gpa: Allocator,
    cwd: []const u8,
    environ_map: ?*const std.process.Environ.Map,
    path: []const u8,
    parsed: WindowsPath2(u8),
) ![]u8

File

lib/std/fs/path.zig:1638

Code

fn windowsResolveAgainstCwd(
    gpa: Allocator,
    cwd: []const u8,
    environ_map: ?*const std.process.Environ.Map,
    path: []const u8,
    parsed: WindowsPath2(u8),
) ![]u8 {
    // Space for 256 WTF-16 code units; potentially 3 WTF-8 bytes per WTF-16 code unit
    var buf: [256 * 3]u8 = undefined;
    var temp_allocator_state: std.heap.BufferFirstAllocator = .init(&buf, gpa);
    return switch (parsed.kind) {
        .drive_absolute,
        .unc_absolute,
        .root_local_device,
        .local_device,
        => try resolveWindows(gpa, &.{path}),

        .relative => try resolveWindows(gpa, &.{ cwd, path }),

        .rooted => blk: {
            const parsed_cwd = parsePathWindows(u8, cwd);
            switch (parsed_cwd.kind) {
                .drive_absolute => {
                    var drive_buf = "_:\\".*;
                    drive_buf[0] = cwd[0];
                    break :blk try resolveWindows(gpa, &.{ &drive_buf, path });
                },
                .unc_absolute => {
                    break :blk try resolveWindows(gpa, &.{ parsed_cwd.root, path });
                },
                // Effectively a malformed CWD, give up and just return a normalized path
                else => break :blk try resolveWindows(gpa, &.{path}),
            }
        },
        .drive_relative => blk: {
            const temp_allocator = temp_allocator_state.allocator();
            const drive_cwd = drive_cwd: {
                const parsed_cwd = parsePathWindows(u8, cwd);

                if (parsed_cwd.kind == .drive_absolute) {
                    const drive_letter_w = parsed_cwd.root[0];
                    const drive_letters_match = drive_letter_w <= 0x7F and
                        std.ascii.toUpper(@intCast(drive_letter_w)) == std.ascii.toUpper(parsed.root[0]);
                    if (drive_letters_match)
                        break :drive_cwd cwd;

                    if (environ_map) |m| {
                        if (m.get(&.{ '=', parsed.root[0], ':' })) |v| {
                            break :drive_cwd try temp_allocator.dupe(u8, v);
                        }
                    }
                }

                const drive_buf = try temp_allocator.alloc(u8, 3);
                drive_buf[0] = parsed.root[0];
                drive_buf[1] = ':';
                drive_buf[2] = '\\';
                break :drive_cwd drive_buf;
            };
            defer temp_allocator.free(drive_cwd);
            break :blk try resolveWindows(gpa, &.{ drive_cwd, path });
        },
    };
}