Convert std.fs.realpath to file:// uri

is there a library function that would convert my (windows) "C:\dir\..." realpath string into something suitable for a file:// uri???

After replacing backslashes to slashes you can use std.Uri to encode it:

    const uri: std.Uri = .{
        .scheme = "file",
        .path = .{
            .raw = "C:/Program Files/foo",
        },
    };
    std.debug.print("{}\n", .{uri});

prints file:C:/Program%20Files/foo

3 Likes

Just a side note, file:C:/Program%20Files/foo is technically not a valid file URI. File URI paths must be absolute and begin with a slash, so when encoding an absolute path with a Windows drive letter as a file URI you are required to prepend a slash (which you must then detect and strip when decoding). A correct file URI would be one of the following:

  • file:///C:/Program%20Files/foo (most common)
  • file:/C:/Program%20Files/foo
  • file://localhost/C:/Program%20Files/foo
3 Likes