Skip to content

Commit

Permalink
handle both windows/\!windows case
Browse files Browse the repository at this point in the history
  • Loading branch information
kbkpbot committed Jan 20, 2025
1 parent b03c828 commit c25f991
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 57 deletions.
26 changes: 14 additions & 12 deletions vlib/os/os.v
Original file line number Diff line number Diff line change
Expand Up @@ -324,36 +324,38 @@ pub fn file_name(opath string) string {
return path.all_after_last(path_separator)
}

// split_path will split `opath` into (`dir`,`filename`,`ext`).
// 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(opath string) (string, string, string) {
if opath == '.' {
pub fn split_path(path string) (string, string, string) {
if path == '' {
return '', '', ''
} else if path == '.' {
return '.', '', ''
} else if opath == '..' {
} else if path == '..' {
return '..', '', ''
}

other_separator := if path_separator == '/' { '\\' } else { '/' }
path := opath.replace(other_separator, path_separator)
if path == path_separator {
return path_separator, '', ''
my_path_separator := if path.contains('/') { '/' } else { '\\' }

if path == my_path_separator {
return my_path_separator, '', ''
}
if path.ends_with(path_separator) {
if path.ends_with(my_path_separator) {
return path[..path.len - 1], '', ''
}
mut dir := '.'
if pos := path.last_index(path_separator) {
if pos := path.last_index(my_path_separator) {
if pos == 0 {
dir = path_separator
dir = my_path_separator
} else {
dir = path[..pos]
}
}
file_name := path.all_after_last(path_separator)
file_name := path.all_after_last(my_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
89 changes: 44 additions & 45 deletions vlib/os/os_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -689,70 +689,69 @@ fn test_split_path() {
mut dir := ''
mut filename := ''
mut ext := ''
$if windows {
dir, filename, ext = os.split_path(r'\')
assert [dir, filename, ext] == [r'\', '', '']

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('a')
assert [dir, filename, ext] == ['.', 'a', '']

dir, filename, ext = os.split_path(r'\x.c.v')
assert [dir, filename, ext] == [r'\', 'x.c', '.v']
dir, filename, ext = os.split_path('.')
assert [dir, filename, ext] == ['.', '', '']

dir, filename, ext = os.split_path(r'.\x.c.v')
assert [dir, filename, ext] == ['.', 'x.c', '.v']
dir, filename, ext = os.split_path('..')
assert [dir, filename, ext] == ['..', '', '']

dir, filename, ext = os.split_path(r'x.c.v')
assert [dir, filename, ext] == ['.', 'x.c', '.v']
dir, filename, ext = os.split_path('\\')
assert [dir, filename, ext] == ['\\', '', '']

dir, filename, ext = os.split_path(r'..\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(r'\lib\x.c.v')
assert [dir, filename, ext] == [r'\lib', '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(r'\lib\x.c.v\')
assert [dir, filename, ext] == [r'\lib\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(r'\lib\x.c.')
assert [dir, filename, ext] == [r'\lib', 'x.c.', '']
} $else {
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('.')
assert [dir, filename, ext] == ['.', '', '']
dir, filename, ext = os.split_path('\\lib\\x.c.v')
assert [dir, filename, ext] == ['\\lib', 'x.c', '.v']

dir, filename, ext = os.split_path('..')
assert [dir, filename, ext] == ['..', '', '']
dir, filename, ext = os.split_path('\\lib\\x.c.v\\')
assert [dir, filename, ext] == ['\\lib\\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.')
assert [dir, filename, ext] == ['\\lib', 'x.c.', '']

dir, filename, ext = os.split_path('./x.c.v')
assert [dir, filename, ext] == ['.', 'x.c', '.v']
dir, filename, ext = os.split_path('C:\\lib\\x.c.')
assert [dir, filename, ext] == ['C:\\lib', 'x.c.', '']

dir, filename, ext = os.split_path('x.c.v')
assert [dir, filename, ext] == ['.', 'x.c', '.v']
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('/lib/x.c.v')
assert [dir, filename, ext] == ['/lib', '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('../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('/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() {
Expand Down

0 comments on commit c25f991

Please sign in to comment.