diff --git a/vlib/os/os.v b/vlib/os/os.v index 5fda75b42e6adf..b298b99a910794 100644 --- a/vlib/os/os.v +++ b/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, // dir returns a single separator. // The returned path does not end in a separator unless it is the root directory. -pub fn dir(opath string) string { - if opath == '' { +pub fn dir(path string) string { + if path == '' { return '.' } - other_separator := if path_separator == '/' { '\\' } else { '/' } - path := opath.replace(other_separator, path_separator) - pos := path.last_index(path_separator) or { return '.' } - if pos == 0 && path_separator == '/' { - return '/' + detected_path_separator := if path.contains('/') { '/' } else { '\\' } + pos := path.last_index(detected_path_separator) or { return '.' } + if pos == 0 { + return detected_path_separator } return path[..pos] } @@ -298,30 +297,71 @@ pub fn dir(opath string) string { // 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(opath string) string { - if opath == '' { +pub fn base(path string) string { + if path == '' { return '.' } - other_separator := if path_separator == '/' { '\\' } else { '/' } - path := opath.replace(other_separator, path_separator) - if path == path_separator { - return path_separator + detected_path_separator := if path.contains('/') { '/' } else { '\\' } + if path == detected_path_separator { + return detected_path_separator } - if path.ends_with(path_separator) { + if path.ends_with(detected_path_separator) { 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..] } - 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..] } // file_name will return all characters found after the last occurrence of `path_separator`. // file extension is included. -pub fn file_name(opath string) string { - other_separator := if path_separator == '/' { '\\' } else { '/' } - path := opath.replace(other_separator, path_separator) - return path.all_after_last(path_separator) +pub fn file_name(path string) string { + detected_path_separator := if path.contains('/') { '/' } else { '\\' } + return path.all_after_last(detected_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. diff --git a/vlib/os/os_test.c.v b/vlib/os/os_test.c.v index f6973baaf44c55..7d8e155306fe73 100644 --- a/vlib/os/os_test.c.v +++ b/vlib/os/os_test.c.v @@ -596,6 +596,7 @@ fn test_is_executable_writable_readable() { } fn test_file_ext() { + assert os.file_ext('') == '' assert os.file_ext('file.v') == '.v' assert os.file_ext('file.js.v') == '.v' assert os.file_ext('file.ext1.ext2.ext3') == '.ext3' @@ -640,50 +641,112 @@ fn test_rmdir_not_exist() ! { } fn test_dir() { - $if windows { - 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') == 'C:\\a\\b' - assert os.dir('C:/a/b/') == 'C:\\a\\b' - } $else { - assert os.dir('/') == '/' - assert os.dir('/abc') == '/' - assert os.dir('/var/tmp/foo') == '/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('') == '.' + assert os.dir('\\') == '\\' + 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') == 'C:/a/b' + assert os.dir('C:/a/b/') == 'C:/a/b' + assert os.dir('/') == '/' + assert os.dir('/abc') == '/' + assert os.dir('/var/tmp/foo') == '/var/tmp' + assert os.dir('/var/tmp/') == '/var/tmp' assert os.dir('os') == '.' } fn test_base() { - $if windows { - 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('') == '.' + 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' } fn test_file_name() { - $if windows { - 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') == 'os' - } $else { - 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') == 'os' - } + assert os.file_name('') == '' + 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') == 'os' + 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') == 'os' 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() { u := os.uname() assert u.sysname.len > 0