mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
os: add split_path/1: os.split_path('/usr/lib/test.so') -> ('/usr/lib','test','.so')
; fix platform dependent behaviour of os.dir/1, os.base/1, os.file_name/1 (#23532)
This commit is contained in:
parent
305a272067
commit
2a69b7c525
2 changed files with 156 additions and 53 deletions
80
vlib/os/os.v
80
vlib/os/os.v
|
@ -281,15 +281,14 @@ pub fn file_ext(opath string) string {
|
||||||
// If the path is empty, dir returns ".". If the path consists entirely of separators,
|
// If the path is empty, dir returns ".". If the path consists entirely of separators,
|
||||||
// dir returns a single separator.
|
// dir returns a single separator.
|
||||||
// The returned path does not end in a separator unless it is the root directory.
|
// The returned path does not end in a separator unless it is the root directory.
|
||||||
pub fn dir(opath string) string {
|
pub fn dir(path string) string {
|
||||||
if opath == '' {
|
if path == '' {
|
||||||
return '.'
|
return '.'
|
||||||
}
|
}
|
||||||
other_separator := if path_separator == '/' { '\\' } else { '/' }
|
detected_path_separator := if path.contains('/') { '/' } else { '\\' }
|
||||||
path := opath.replace(other_separator, path_separator)
|
pos := path.last_index(detected_path_separator) or { return '.' }
|
||||||
pos := path.last_index(path_separator) or { return '.' }
|
if pos == 0 {
|
||||||
if pos == 0 && path_separator == '/' {
|
return detected_path_separator
|
||||||
return '/'
|
|
||||||
}
|
}
|
||||||
return path[..pos]
|
return path[..pos]
|
||||||
}
|
}
|
||||||
|
@ -298,30 +297,71 @@ pub fn dir(opath string) string {
|
||||||
// Trailing path separators are removed before extracting the last element.
|
// 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
|
// If the path is empty, base returns ".". If the path consists entirely of separators, base returns a
|
||||||
// single separator.
|
// single separator.
|
||||||
pub fn base(opath string) string {
|
pub fn base(path string) string {
|
||||||
if opath == '' {
|
if path == '' {
|
||||||
return '.'
|
return '.'
|
||||||
}
|
}
|
||||||
other_separator := if path_separator == '/' { '\\' } else { '/' }
|
detected_path_separator := if path.contains('/') { '/' } else { '\\' }
|
||||||
path := opath.replace(other_separator, path_separator)
|
if path == detected_path_separator {
|
||||||
if path == path_separator {
|
return detected_path_separator
|
||||||
return path_separator
|
|
||||||
}
|
}
|
||||||
if path.ends_with(path_separator) {
|
if path.ends_with(detected_path_separator) {
|
||||||
path2 := path[..path.len - 1]
|
path2 := path[..path.len - 1]
|
||||||
pos := path2.last_index(path_separator) or { return path2.clone() }
|
pos := path2.last_index(detected_path_separator) or { return path2.clone() }
|
||||||
return path2[pos + 1..]
|
return path2[pos + 1..]
|
||||||
}
|
}
|
||||||
pos := path.last_index(path_separator) or { return path.clone() }
|
pos := path.last_index(detected_path_separator) or { return path.clone() }
|
||||||
return path[pos + 1..]
|
return path[pos + 1..]
|
||||||
}
|
}
|
||||||
|
|
||||||
// file_name will return all characters found after the last occurrence of `path_separator`.
|
// file_name will return all characters found after the last occurrence of `path_separator`.
|
||||||
// file extension is included.
|
// file extension is included.
|
||||||
pub fn file_name(opath string) string {
|
pub fn file_name(path string) string {
|
||||||
other_separator := if path_separator == '/' { '\\' } else { '/' }
|
detected_path_separator := if path.contains('/') { '/' } else { '\\' }
|
||||||
path := opath.replace(other_separator, path_separator)
|
return path.all_after_last(detected_path_separator)
|
||||||
return path.all_after_last(path_separator)
|
}
|
||||||
|
|
||||||
|
// split_path will split `path` into (`dir`,`filename`,`ext`).
|
||||||
|
// Examples:
|
||||||
|
// ```v
|
||||||
|
// dir,filename,ext := os.split_path('/usr/lib/test.so')
|
||||||
|
// assert [dir,filename,ext] == ['/usr/lib','test','.so']
|
||||||
|
// ```
|
||||||
|
pub fn split_path(path string) (string, string, string) {
|
||||||
|
if path == '' {
|
||||||
|
return '.', '', ''
|
||||||
|
} else if path == '.' {
|
||||||
|
return '.', '', ''
|
||||||
|
} else if path == '..' {
|
||||||
|
return '..', '', ''
|
||||||
|
}
|
||||||
|
|
||||||
|
detected_path_separator := if path.contains('/') { '/' } else { '\\' }
|
||||||
|
|
||||||
|
if path == detected_path_separator {
|
||||||
|
return detected_path_separator, '', ''
|
||||||
|
}
|
||||||
|
if path.ends_with(detected_path_separator) {
|
||||||
|
return path[..path.len - 1], '', ''
|
||||||
|
}
|
||||||
|
mut dir := '.'
|
||||||
|
/*
|
||||||
|
TODO: JS backend does not support IfGuard yet.
|
||||||
|
*/
|
||||||
|
pos := path.last_index(detected_path_separator) or { -1 }
|
||||||
|
if pos == -1 {
|
||||||
|
dir = '.'
|
||||||
|
} else if pos == 0 {
|
||||||
|
dir = detected_path_separator
|
||||||
|
} else {
|
||||||
|
dir = path[..pos]
|
||||||
|
}
|
||||||
|
file_name := path.all_after_last(detected_path_separator)
|
||||||
|
pos_ext := file_name.last_index_u8(`.`)
|
||||||
|
if pos_ext == -1 || pos_ext == 0 || pos_ext + 1 >= file_name.len {
|
||||||
|
return dir, file_name, ''
|
||||||
|
}
|
||||||
|
return dir, file_name[..pos_ext], file_name[pos_ext..]
|
||||||
}
|
}
|
||||||
|
|
||||||
// input_opt returns a one-line string from stdin, after printing a prompt.
|
// input_opt returns a one-line string from stdin, after printing a prompt.
|
||||||
|
|
|
@ -596,6 +596,7 @@ fn test_is_executable_writable_readable() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_file_ext() {
|
fn test_file_ext() {
|
||||||
|
assert os.file_ext('') == ''
|
||||||
assert os.file_ext('file.v') == '.v'
|
assert os.file_ext('file.v') == '.v'
|
||||||
assert os.file_ext('file.js.v') == '.v'
|
assert os.file_ext('file.js.v') == '.v'
|
||||||
assert os.file_ext('file.ext1.ext2.ext3') == '.ext3'
|
assert os.file_ext('file.ext1.ext2.ext3') == '.ext3'
|
||||||
|
@ -640,50 +641,112 @@ fn test_rmdir_not_exist() ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_dir() {
|
fn test_dir() {
|
||||||
$if windows {
|
assert os.dir('') == '.'
|
||||||
|
assert os.dir('\\') == '\\'
|
||||||
assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b'
|
assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b'
|
||||||
assert os.dir('C:\\a\\b\\') == 'C:\\a\\b'
|
assert os.dir('C:\\a\\b\\') == 'C:\\a\\b'
|
||||||
assert os.dir('C:/a/b/c') == 'C:\\a\\b'
|
assert os.dir('C:/a/b/c') == 'C:/a/b'
|
||||||
assert os.dir('C:/a/b/') == 'C:\\a\\b'
|
assert os.dir('C:/a/b/') == 'C:/a/b'
|
||||||
} $else {
|
|
||||||
assert os.dir('/') == '/'
|
assert os.dir('/') == '/'
|
||||||
assert os.dir('/abc') == '/'
|
assert os.dir('/abc') == '/'
|
||||||
assert os.dir('/var/tmp/foo') == '/var/tmp'
|
assert os.dir('/var/tmp/foo') == '/var/tmp'
|
||||||
assert os.dir('/var/tmp/') == '/var/tmp'
|
assert os.dir('/var/tmp/') == '/var/tmp'
|
||||||
assert os.dir('C:\\a\\b\\c') == 'C:/a/b'
|
|
||||||
assert os.dir('C:\\a\\b\\') == 'C:/a/b'
|
|
||||||
}
|
|
||||||
assert os.dir('os') == '.'
|
assert os.dir('os') == '.'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_base() {
|
fn test_base() {
|
||||||
$if windows {
|
assert os.base('') == '.'
|
||||||
assert os.base('v\\vlib\\os') == 'os'
|
assert os.base('v\\vlib\\os') == 'os'
|
||||||
assert os.base('v\\vlib\\os\\') == 'os'
|
assert os.base('v\\vlib\\os\\') == 'os'
|
||||||
assert os.base('v/vlib/os') == 'os'
|
assert os.base('v/vlib/os') == 'os'
|
||||||
assert os.base('v/vlib/os/') == 'os'
|
assert os.base('v/vlib/os/') == 'os'
|
||||||
} $else {
|
|
||||||
assert os.base('v/vlib/os') == 'os'
|
assert os.base('v/vlib/os') == 'os'
|
||||||
assert os.base('v/vlib/os/') == 'os'
|
assert os.base('v/vlib/os/') == 'os'
|
||||||
assert os.base('v\\vlib\\os') == 'os'
|
assert os.base('v\\vlib\\os') == 'os'
|
||||||
assert os.base('v\\vlib\\os\\') == 'os'
|
assert os.base('v\\vlib\\os\\') == 'os'
|
||||||
}
|
|
||||||
assert os.base('filename') == 'filename'
|
assert os.base('filename') == 'filename'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_file_name() {
|
fn test_file_name() {
|
||||||
$if windows {
|
assert os.file_name('') == ''
|
||||||
assert os.file_name('v\\vlib\\os\\os.v') == 'os.v'
|
assert os.file_name('v\\vlib\\os\\os.v') == 'os.v'
|
||||||
assert os.file_name('v\\vlib\\os\\') == ''
|
assert os.file_name('v\\vlib\\os\\') == ''
|
||||||
assert os.file_name('v\\vlib\\os') == 'os'
|
assert os.file_name('v\\vlib\\os') == 'os'
|
||||||
} $else {
|
|
||||||
assert os.file_name('v/vlib/os/os.v') == 'os.v'
|
assert os.file_name('v/vlib/os/os.v') == 'os.v'
|
||||||
assert os.file_name('v/vlib/os/') == ''
|
assert os.file_name('v/vlib/os/') == ''
|
||||||
assert os.file_name('v/vlib/os') == 'os'
|
assert os.file_name('v/vlib/os') == 'os'
|
||||||
}
|
|
||||||
assert os.file_name('filename') == 'filename'
|
assert os.file_name('filename') == 'filename'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_split_path() {
|
||||||
|
mut dir := ''
|
||||||
|
mut filename := ''
|
||||||
|
mut ext := ''
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('')
|
||||||
|
assert [dir, filename, ext] == ['.', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('a')
|
||||||
|
assert [dir, filename, ext] == ['.', 'a', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('.')
|
||||||
|
assert [dir, filename, ext] == ['.', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('..')
|
||||||
|
assert [dir, filename, ext] == ['..', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('\\')
|
||||||
|
assert [dir, filename, ext] == ['\\', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('\\x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['\\', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('.\\x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['.', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['.', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('..\\x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['..', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('\\lib\\x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['\\lib', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('\\lib\\x.c.v\\')
|
||||||
|
assert [dir, filename, ext] == ['\\lib\\x.c.v', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('\\lib\\x.c.')
|
||||||
|
assert [dir, filename, ext] == ['\\lib', 'x.c.', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('C:\\lib\\x.c.')
|
||||||
|
assert [dir, filename, ext] == ['C:\\lib', 'x.c.', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('/')
|
||||||
|
assert [dir, filename, ext] == ['/', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('/x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['/', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('./x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['.', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('../x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['..', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('/lib/x.c.v')
|
||||||
|
assert [dir, filename, ext] == ['/lib', 'x.c', '.v']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('/lib/x.c.v/')
|
||||||
|
assert [dir, filename, ext] == ['/lib/x.c.v', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('/lib/../x.c.v/')
|
||||||
|
assert [dir, filename, ext] == ['/lib/../x.c.v', '', '']
|
||||||
|
|
||||||
|
dir, filename, ext = os.split_path('/lib/x.c.')
|
||||||
|
assert [dir, filename, ext] == ['/lib', 'x.c.', '']
|
||||||
|
}
|
||||||
|
|
||||||
fn test_uname() {
|
fn test_uname() {
|
||||||
u := os.uname()
|
u := os.uname()
|
||||||
assert u.sysname.len > 0
|
assert u.sysname.len > 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue