Skip to content

Commit

Permalink
os: add split_path
Browse files Browse the repository at this point in the history
  • Loading branch information
kbkpbot committed Jan 20, 2025
1 parent f9d3bd3 commit b03c828
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
41 changes: 39 additions & 2 deletions vlib/os/os.v
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,43 @@ pub fn file_name(opath string) string {
return path.all_after_last(path_separator)
}

// split_path will split `opath` 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 == '.' {
return '.', '', ''
} else if opath == '..' {
return '..', '', ''
}

other_separator := if path_separator == '/' { '\\' } else { '/' }
path := opath.replace(other_separator, path_separator)
if path == path_separator {
return path_separator, '', ''
}
if path.ends_with(path_separator) {
return path[..path.len - 1], '', ''
}
mut dir := '.'
if pos := path.last_index(path_separator) {
if pos == 0 {
dir = path_separator
} else {
dir = path[..pos]
}
}
file_name := path.all_after_last(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
71 changes: 71 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,76 @@ fn test_file_name() {
assert os.file_name('filename') == 'filename'
}

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

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

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

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

0 comments on commit b03c828

Please sign in to comment.