Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: add split_path() #23532

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions vlib/os/os.v
kbkpbot marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ pub fn dir(opath string) string {
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 '/'
if pos == 0 {
return path_separator
}
return path[..pos]
}
Expand Down Expand Up @@ -324,6 +324,45 @@ pub fn file_name(opath string) string {
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 '..', '', ''
}

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

if path == my_path_separator {
return my_path_separator, '', ''
}
if path.ends_with(my_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]
}
}
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, ''
}
return dir, file_name[..pos_ext], file_name[pos_ext..]
}

// input_opt returns a one-line string from stdin, after printing a prompt.
// Returns `none` in case of an error (end of input).
pub fn input_opt(prompt string) ?string {
Expand Down
70 changes: 70 additions & 0 deletions vlib/os/os_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ 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'
Expand Down Expand Up @@ -684,6 +685,75 @@ fn test_file_name() {
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
Expand Down
Loading