os: fix file_ext function (#14566)
parent
f971da9a93
commit
e201665e92
14
vlib/os/os.v
14
vlib/os/os.v
|
@ -173,8 +173,20 @@ pub fn is_dir_empty(path string) bool {
|
|||
|
||||
// file_ext will return the part after the last occurence of `.` in `path`.
|
||||
// The `.` is included.
|
||||
// Examples:
|
||||
// ```v
|
||||
// assert os.file_ext('file.v') == '.v'
|
||||
// assert os.file_ext('.ignore_me') == ''
|
||||
// assert os.file_ext('.') == ''
|
||||
// ```
|
||||
pub fn file_ext(path string) string {
|
||||
pos := path.last_index('.') or { return '' }
|
||||
if path.len < 3 {
|
||||
return empty_str
|
||||
}
|
||||
pos := path.last_index(dot_str) or { return empty_str }
|
||||
if pos + 1 >= path.len || pos == 0 {
|
||||
return empty_str
|
||||
}
|
||||
return path[pos..]
|
||||
}
|
||||
|
||||
|
|
|
@ -585,9 +585,19 @@ fn test_is_executable_writable_readable() ? {
|
|||
os.rm(file_name) or { panic(err) }
|
||||
}
|
||||
|
||||
fn test_ext() {
|
||||
fn test_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'
|
||||
assert os.file_ext('.ignore_me.v') == '.v'
|
||||
assert os.file_ext('file') == ''
|
||||
assert os.file_ext('.git') == ''
|
||||
assert os.file_ext('file.') == ''
|
||||
assert os.file_ext('.') == ''
|
||||
assert os.file_ext('..') == ''
|
||||
assert os.file_ext('file...') == ''
|
||||
assert os.file_ext('.file.') == ''
|
||||
assert os.file_ext('..file..') == ''
|
||||
}
|
||||
|
||||
fn test_join() {
|
||||
|
|
Loading…
Reference in New Issue