diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-02-10 00:26:33 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-02-10 00:26:33 -0500 |
| commit | cdc5070f216a924d24588b8d0fe06400e036e6bf (patch) | |
| tree | c1943e1831725e41810ea4db4eb1785a130e18e1 /lib/std/fs/path.zig | |
| parent | 9e5b2489913f72764ded2089bccd7e612a3cc347 (diff) | |
| parent | 014f66e6de4aaf81f32c796b12f981326a479397 (diff) | |
| download | zig-cdc5070f216a924d24588b8d0fe06400e036e6bf.tar.gz zig-cdc5070f216a924d24588b8d0fe06400e036e6bf.zip | |
Merge remote-tracking branch 'origin/master' into llvm10
Diffstat (limited to 'lib/std/fs/path.zig')
| -rw-r--r-- | lib/std/fs/path.zig | 60 |
1 files changed, 20 insertions, 40 deletions
diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index 66e24a4805..f6f34585be 100644 --- a/lib/std/fs/path.zig +++ b/lib/std/fs/path.zig @@ -146,72 +146,51 @@ pub fn isAbsolute(path: []const u8) bool { } } -pub fn isAbsoluteW(path_w: [*:0]const u16) bool { - if (path_w[0] == '/') - return true; - - if (path_w[0] == '\\') { - return true; - } - if (path_w[0] == 0 or path_w[1] == 0 or path_w[2] == 0) { +fn isAbsoluteWindowsImpl(comptime T: type, path: []const T) bool { + if (path.len < 1) return false; - } - if (path_w[1] == ':') { - if (path_w[2] == '/') - return true; - if (path_w[2] == '\\') - return true; - } - return false; -} -pub fn isAbsoluteWindows(path: []const u8) bool { if (path[0] == '/') return true; - if (path[0] == '\\') { + if (path[0] == '\\') return true; - } - if (path.len < 3) { + + if (path.len < 3) return false; - } + if (path[1] == ':') { if (path[2] == '/') return true; if (path[2] == '\\') return true; } + return false; } -pub fn isAbsoluteWindowsC(path_c: [*:0]const u8) bool { - if (path_c[0] == '/') - return true; +pub fn isAbsoluteWindows(path: []const u8) bool { + return isAbsoluteWindowsImpl(u8, path); +} - if (path_c[0] == '\\') { - return true; - } - if (path_c[0] == 0 or path_c[1] == 0 or path_c[2] == 0) { - return false; - } - if (path_c[1] == ':') { - if (path_c[2] == '/') - return true; - if (path_c[2] == '\\') - return true; - } - return false; +pub fn isAbsoluteWindowsW(path_w: [*:0]const u16) bool { + return isAbsoluteWindowsImpl(u16, mem.toSliceConst(u16, path_w)); +} + +pub fn isAbsoluteWindowsC(path_c: [*:0]const u8) bool { + return isAbsoluteWindowsImpl(u8, mem.toSliceConst(u8, path_c)); } pub fn isAbsolutePosix(path: []const u8) bool { - return path[0] == sep_posix; + return path.len > 0 and path[0] == sep_posix; } pub fn isAbsolutePosixC(path_c: [*:0]const u8) bool { - return path_c[0] == sep_posix; + return isAbsolutePosix(mem.toSliceConst(u8, path_c)); } test "isAbsoluteWindows" { + testIsAbsoluteWindows("", false); testIsAbsoluteWindows("/", true); testIsAbsoluteWindows("//", true); testIsAbsoluteWindows("//server", true); @@ -234,6 +213,7 @@ test "isAbsoluteWindows" { } test "isAbsolutePosix" { + testIsAbsolutePosix("", false); testIsAbsolutePosix("/home/foo", true); testIsAbsolutePosix("/home/foo/..", true); testIsAbsolutePosix("bar/", false); |
