1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: remove base_dir(); dir() and base() fixes

This commit is contained in:
Alexander Medvednikov
2020-10-01 01:25:52 +02:00
parent 324d547cdb
commit 4879661f5a
4 changed files with 44 additions and 21 deletions

View File

@@ -642,21 +642,46 @@ pub fn file_ext(path string) string {
return path[pos..]
}
// dir will return the part of `path` before the last occurence of a `path_separator`.
// dir returns all but the last element of path, typically the path's directory.
// After dropping the final element, trailing slashes are removed.
// If the path is empty, dir returns ".". If the path consists entirely of separators,
// dir returns a single separator.
// The returned path does not end in a separator unless it is the root directory.
pub fn dir(path string) string {
pos := path.last_index(path_separator) or {
if path == '' {
return '.'
}
mut pos := path.last_index(path_separator) or {
return '.'
}
if path.ends_with(path_separator) {
pos--
}
return path[..pos]
}
// base_dir will return the base directory of `path`.
pub fn base_dir(path string) string {
posx := path.last_index(path_separator) or {
// base returns the last element of path.
// Trailing path separators are removed before extracting the last element.
// If the path is empty, base returns ".". If the path consists entirely of separators, base returns a
// single separator.
pub fn base(path string) string {
if path == '' {
return '.'
}
if path == path_separator {
return path_separator
}
if path.ends_with(path_separator) {
path2 := path[..path.len-1]
pos := path2.last_index(path_separator) or {
return path2.clone()
}
return path2[pos+1..]
}
pos := path.last_index(path_separator) or {
return path.clone()
}
// NB: *without* terminating /
return path[..posx]
return path[pos+1..]
}
// file_name will return all characters found after the last occurence of `path_separator`.

View File

@@ -446,13 +446,15 @@ fn test_dir() {
assert os.dir('os') == '.'
}
fn test_basedir() {
fn test_base() {
$if windows {
assert os.base_dir('v\\vlib\\os') == 'v\\vlib'
assert os.base('v\\vlib\\os') == 'os'
assert os.base('v\\vlib\\os\\') == 'os'
} $else {
assert os.base_dir('v/vlib/os') == 'v/vlib'
assert os.base('v/vlib/os') == 'os'
assert os.base('v/vlib/os/') == 'os'
}
assert os.base_dir('filename') == 'filename'
assert os.base('filename') == 'filename'
}
fn test_uname() {