Example
test windowsParsePath {
{
const parsed = windowsParsePath("//a/b");
try testing.expect(parsed.is_abs);
try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare);
try testing.expect(mem.eql(u8, parsed.disk_designator, "//a/b"));
}
{
const parsed = windowsParsePath("\\\\a\\b");
try testing.expect(parsed.is_abs);
try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare);
try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\b"));
}
{
const parsed = windowsParsePath("\\\\a\\");
try testing.expect(!parsed.is_abs);
try testing.expect(parsed.kind == WindowsPath.Kind.None);
try testing.expect(mem.eql(u8, parsed.disk_designator, ""));
}
{
const parsed = windowsParsePath("/usr/local");
try testing.expect(parsed.is_abs);
try testing.expect(parsed.kind == WindowsPath.Kind.None);
try testing.expect(mem.eql(u8, parsed.disk_designator, ""));
}
{
const parsed = windowsParsePath("c:../");
try testing.expect(!parsed.is_abs);
try testing.expect(parsed.kind == WindowsPath.Kind.Drive);
try testing.expect(mem.eql(u8, parsed.disk_designator, "c:"));
}
}
Source
pub fn windowsParsePath(path: []const u8) WindowsPath {
if (path.len >= 2 and path[1] == ':') {
return WindowsPath{
.is_abs = isAbsoluteWindows(path),
.kind = WindowsPath.Kind.Drive,
.disk_designator = path[0..2],
};
}
if (path.len >= 1 and (path[0] == '/' or path[0] == '\\') and
(path.len == 1 or (path[1] != '/' and path[1] != '\\')))
{
return WindowsPath{
.is_abs = true,
.kind = WindowsPath.Kind.None,
.disk_designator = path[0..0],
};
}
const relative_path = WindowsPath{
.kind = WindowsPath.Kind.None,
.disk_designator = &[_]u8{},
.is_abs = false,
};
if (path.len < "//a/b".len) {
return relative_path;
}
inline for ("/\\") |this_sep| {
const two_sep = [_]u8{ this_sep, this_sep };
if (mem.startsWith(u8, path, &two_sep)) {
if (path[2] == this_sep) {
return relative_path;
}
var it = mem.tokenizeScalar(u8, path, this_sep);
_ = (it.next() orelse return relative_path);
_ = (it.next() orelse return relative_path);
return WindowsPath{
.is_abs = isAbsoluteWindows(path),
.kind = WindowsPath.Kind.NetworkShare,
.disk_designator = path[0..it.index],
};
}
}
return relative_path;
}