v2 / vlib / os / filepath_windows.v
37 lines · 35 sloc · 1.24 KB · 00bca89bfc4220191997de6c61225e90c5330225
Raw
1module os
2
3// windows_volume returns the volume name from the given `path` on a Windows system.
4// An empty string is returned if no Windows volume is present.
5// Examples (on a Windows system):
6// ```v
7// assert os.windows_volume(r'C:\path\to\file.v') == 'C:'
8// assert os.windows_volume('D:') == 'D:'
9// assert os.windows_volume(r'\\Host\share\files\file.v') == r'\\Host\share'
10// ```
11pub fn windows_volume(path string) string {
12 volume_len := win_volume_len(path)
13 if volume_len == 0 {
14 return empty_str
15 }
16 return path[..volume_len]
17}
18
19// trim_extended_length_path_prefix converts Win32 extended-length DOS/UNC
20// paths returned by APIs like GetFinalPathNameByHandleW into regular paths.
21// Paths that are not standard DOS or UNC paths are left unchanged.
22fn trim_extended_length_path_prefix(path string) string {
23 $if !windows {
24 return path
25 }
26 if path.len < 4 || !starts_w_slash_slash(path) || path[2] != qmark || !is_slash(path[3]) {
27 return path
28 }
29 if path.len >= 8 && (path[4] == `U` || path[4] == `u`) && (path[5] == `N` || path[5] == `n`)
30 && (path[6] == `C` || path[6] == `c`) && is_slash(path[7]) {
31 return '\\\\' + path[8..]
32 }
33 if path.len >= 7 && path[4].is_letter() && path[5] == `:` && is_slash(path[6]) {
34 return path[4..]
35 }
36 return path
37}
38