Skip to content

Commit

Permalink
fix IfGuard; fix dir(),base(),file_name() support Win/non-Win
Browse files Browse the repository at this point in the history
  • Loading branch information
kbkpbot committed Jan 21, 2025
1 parent 33fa7d3 commit e9d6c48
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 66 deletions.
63 changes: 32 additions & 31 deletions vlib/os/os.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.' }
detected_path_separator := if path.contains('/') { '/' } else { '\\' }
pos := path.last_index(detected_path_separator) or { return '.' }
if pos == 0 {
return path_separator
return detected_path_separator
}
return path[..pos]
}
Expand All @@ -298,30 +297,28 @@ 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`).
Expand All @@ -332,30 +329,34 @@ pub fn file_name(opath string) string {
// ```
pub fn split_path(path string) (string, string, string) {
if path == '' {
return '', '', ''
return '.', '', ''
} else if path == '.' {
return '.', '', ''
} else if path == '..' {
return '..', '', ''
}

my_path_separator := if path.contains('/') { '/' } else { '\\' }
detected_path_separator := if path.contains('/') { '/' } else { '\\' }

if path == my_path_separator {
return my_path_separator, '', ''
if path == detected_path_separator {
return detected_path_separator, '', ''
}
if path.ends_with(my_path_separator) {
if path.ends_with(detected_path_separator) {
return path[..path.len - 1], '', ''
}
mut dir := '.'
if pos := path.last_index(my_path_separator) {
if pos == 0 {
dir = my_path_separator
} else {
dir = path[..pos]
}
/*
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(my_path_separator)
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, ''
Expand Down
63 changes: 28 additions & 35 deletions vlib/os/os_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -640,48 +641,40 @@ fn test_rmdir_not_exist() ! {
}

fn test_dir() {
$if windows {
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'
} $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'
}

Expand All @@ -691,7 +684,7 @@ fn test_split_path() {
mut ext := ''

dir, filename, ext = os.split_path('')
assert [dir, filename, ext] == ['', '', '']
assert [dir, filename, ext] == ['.', '', '']

dir, filename, ext = os.split_path('a')
assert [dir, filename, ext] == ['.', 'a', '']
Expand Down

0 comments on commit e9d6c48

Please sign in to comment.