|
1 | 1 | immutable WindowsPath <: AbstractPath |
2 | | - parts::Tuple |
| 2 | + parts::Tuple{Vararg{String}} |
3 | 3 | drive::String |
| 4 | + root::String |
4 | 5 | end |
5 | 6 |
|
6 | | -WindowsPath() = WindowsPath(tuple(), "") |
| 7 | +WindowsPath() = WindowsPath(tuple(), "", "") |
| 8 | + |
| 9 | +WindowsPath(parts::Tuple) = WindowsPath(parts, "", "") |
7 | 10 |
|
8 | 11 | function WindowsPath(str::AbstractString) |
9 | 12 | if isempty(str) |
10 | | - return WindowsPath(tuple("."), "") |
| 13 | + return WindowsPath(tuple("."), "", "") |
11 | 14 | end |
12 | 15 |
|
13 | | - drive, path = splitdir(str) |
14 | | - tokenized = split(path, WIN_PATH_SEPARATOR) |
15 | | - |
16 | | - if isempty(tokenized[1]) |
17 | | - tokenized[1] = WIN_PATH_SEPARATOR |
| 16 | + if startswith(str, "\\\\?\\") |
| 17 | + error("The \\\\?\\ prefix is currently not supported.") |
18 | 18 | end |
19 | 19 |
|
20 | | - return WindowsPath(tuple(map(String, tokenized)...), drive) |
21 | | -end |
| 20 | + str = replace(str, POSIX_PATH_SEPARATOR, WIN_PATH_SEPARATOR) |
| 21 | + |
| 22 | + if startswith(str, "\\\\") |
| 23 | + error("UNC paths are currently not supported.") |
| 24 | + elseif startswith(str, "\\") |
| 25 | + tokenized = split(str, WIN_PATH_SEPARATOR) |
| 26 | + |
| 27 | + return WindowsPath(tuple(WIN_PATH_SEPARATOR, String.(tokenized[2:end])...), "", WIN_PATH_SEPARATOR) |
| 28 | + elseif contains(str, ":") |
| 29 | + l_drive, l_path = splitdrive(str) |
| 30 | + |
| 31 | + tokenized = split(l_path, WIN_PATH_SEPARATOR) |
| 32 | + |
| 33 | + l_root = isempty(tokenized[1]) ? WIN_PATH_SEPARATOR : "" |
| 34 | + |
| 35 | + if isempty(tokenized[1]) |
| 36 | + tokenized = tokenized[2:end] |
| 37 | + end |
| 38 | + |
| 39 | + if !isempty(l_drive) || !isempty(l_root) |
| 40 | + tokenized = tuple(string(l_drive, l_root), tokenized...) |
| 41 | + end |
22 | 42 |
|
| 43 | + return WindowsPath(tuple(String.(tokenized)...), l_drive, l_root) |
| 44 | + else |
| 45 | + tokenized = split(str, WIN_PATH_SEPARATOR) |
23 | 46 |
|
24 | | -# The following should be implemented in the concrete types |
25 | | -==(a::WindowsPath, b::WindowsPath) = parts(a) == parts(b) && drive(a) == drive(b) |
| 47 | + return WindowsPath(tuple(String.(tokenized)...), "", "") |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +function ==(a::WindowsPath, b::WindowsPath) |
| 52 | + return lowercase.(parts(a)) == lowercase.(parts(b)) && |
| 53 | + lowercase(drive(a)) == lowercase(drive(b)) && |
| 54 | + lowercase(root(a)) == lowercase(root(b)) |
| 55 | +end |
26 | 56 | Base.String(path::WindowsPath) = joinpath(parts(path)...) |
27 | 57 | parts(path::WindowsPath) = path.parts |
28 | 58 | drive(path::WindowsPath) = path.drive |
| 59 | +root(path::WindowsPath) = path.root |
29 | 60 |
|
30 | | -function isabs(path::WindowsPath) |
31 | | - if parts(path[1]) == WIN_PATH_SEPARATOR && !isempty(drive(path)) |
32 | | - return true |
| 61 | +function Base.show(io::IO, path::WindowsPath) |
| 62 | + print(io, "p\"") |
| 63 | + if isabs(path) |
| 64 | + print(io, replace(anchor(path), "\\", "/")) |
| 65 | + print(io, join(parts(path)[2:end], "/")) |
33 | 66 | else |
34 | | - return false |
| 67 | + print(io, join(parts(path), "/")) |
35 | 68 | end |
| 69 | + print(io, "\"") |
36 | 70 | end |
37 | 71 |
|
38 | | -function root(path::WindowsPath) |
39 | | - if parts(path)[1] == WIN_PATH_SEPARATOR |
40 | | - return WIN_PATH_SEPARATOR |
41 | | - else |
42 | | - return "" |
43 | | - end |
| 72 | +function isabs(path::WindowsPath) |
| 73 | + return !isempty(drive(path)) || !isempty(root(path)) |
44 | 74 | end |
45 | 75 |
|
46 | 76 | expanduser(path::WindowsPath) = path |
0 commit comments