os: add windows_volume function (#14721)
parent
5ac9b5c9f1
commit
c6b1c8d07a
|
@ -12,6 +12,7 @@ const (
|
||||||
bslash = `\\`
|
bslash = `\\`
|
||||||
dot = `.`
|
dot = `.`
|
||||||
qmark = `?`
|
qmark = `?`
|
||||||
|
fslash_str = '/'
|
||||||
dot_dot = '..'
|
dot_dot = '..'
|
||||||
empty_str = ''
|
empty_str = ''
|
||||||
dot_str = '.'
|
dot_str = '.'
|
||||||
|
@ -62,8 +63,13 @@ pub fn norm_path(path string) string {
|
||||||
return os.dot_str
|
return os.dot_str
|
||||||
}
|
}
|
||||||
rooted := is_abs_path(path)
|
rooted := is_abs_path(path)
|
||||||
volume := get_volume(path)
|
// get the volume name from the path
|
||||||
volume_len := volume.len
|
// if the current operating system is Windows
|
||||||
|
volume_len := win_volume_len(path)
|
||||||
|
mut volume := path[..volume_len]
|
||||||
|
if volume_len != 0 && volume.contains(os.fslash_str) {
|
||||||
|
volume = volume.replace(os.fslash_str, path_separator)
|
||||||
|
}
|
||||||
cpath := clean_path(path[volume_len..])
|
cpath := clean_path(path[volume_len..])
|
||||||
if cpath.len == 0 && volume_len == 0 {
|
if cpath.len == 0 && volume_len == 0 {
|
||||||
return os.dot_str
|
return os.dot_str
|
||||||
|
@ -217,6 +223,9 @@ fn clean_path(path string) string {
|
||||||
// win_volume_len returns the length of the
|
// win_volume_len returns the length of the
|
||||||
// Windows volume/drive from the given `path`.
|
// Windows volume/drive from the given `path`.
|
||||||
fn win_volume_len(path string) int {
|
fn win_volume_len(path string) int {
|
||||||
|
$if !windows {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
plen := path.len
|
plen := path.len
|
||||||
if plen < 2 {
|
if plen < 2 {
|
||||||
return 0
|
return 0
|
||||||
|
@ -244,20 +253,6 @@ fn win_volume_len(path string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_volume(path string) string {
|
|
||||||
$if !windows {
|
|
||||||
return os.empty_str
|
|
||||||
}
|
|
||||||
volume := path[..win_volume_len(path)]
|
|
||||||
if volume.len == 0 {
|
|
||||||
return os.empty_str
|
|
||||||
}
|
|
||||||
if volume[0] == os.fslash {
|
|
||||||
return volume.replace('/', '\\')
|
|
||||||
}
|
|
||||||
return volume
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_slash(b u8) bool {
|
fn is_slash(b u8) bool {
|
||||||
$if windows {
|
$if windows {
|
||||||
return b == os.bslash || b == os.fslash
|
return b == os.bslash || b == os.fslash
|
||||||
|
|
|
@ -152,3 +152,20 @@ fn test_existing_path() {
|
||||||
assert existing_path('$wd//././/.//') or { '' } == '$wd//././/.//'
|
assert existing_path('$wd//././/.//') or { '' } == '$wd//././/.//'
|
||||||
assert existing_path('$wd//././/.//oh') or { '' } == '$wd//././/.//'
|
assert existing_path('$wd//././/.//oh') or { '' } == '$wd//././/.//'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_windows_volume() {
|
||||||
|
$if windows {
|
||||||
|
assert windows_volume('C:/path\\to/file.v') == 'C:'
|
||||||
|
assert windows_volume('D:\\.\\') == 'D:'
|
||||||
|
assert windows_volume('G:') == 'G:'
|
||||||
|
assert windows_volume('G') == ''
|
||||||
|
assert windows_volume(r'\\Host\share\files\file.v') == r'\\Host\share'
|
||||||
|
assert windows_volume('\\\\Host\\') == ''
|
||||||
|
assert windows_volume(r'\\.\BootPartition2\\files\.\\') == r'\\.\BootPartition2'
|
||||||
|
assert windows_volume(r'\/.\BootPartition2\\files\.\\') == r'\/.\BootPartition2'
|
||||||
|
assert windows_volume(r'\\\.\BootPartition2\\files\.\\') == ''
|
||||||
|
assert windows_volume('') == ''
|
||||||
|
assert windows_volume('\\') == ''
|
||||||
|
assert windows_volume('/') == ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
module os
|
||||||
|
|
||||||
|
// windows_volume returns the volume name from the given `path` on a Windows system.
|
||||||
|
// An empty string is returned if no Windows volume is present.
|
||||||
|
// NOTE: An error is returned if the current operating system is not Windows.
|
||||||
|
// Examples (on a Windows system):
|
||||||
|
// ```v
|
||||||
|
// assert os.windows_volume(r'C:\path\to\file.v') == 'C:'
|
||||||
|
// assert os.windows_volume('D:') == 'D:'
|
||||||
|
// assert os.windows_volume(r'\\Host\share\files\file.v') == r'\\Host\share'
|
||||||
|
// ```
|
||||||
|
pub fn windows_volume(path string) string {
|
||||||
|
volume_len := win_volume_len(path)
|
||||||
|
if volume_len == 0 {
|
||||||
|
return empty_str
|
||||||
|
}
|
||||||
|
return path[..volume_len]
|
||||||
|
}
|
Loading…
Reference in New Issue