os: fix file_ext function (#14566)

master
Ben 2022-06-02 06:09:46 +02:00 committed by GitHub
parent f971da9a93
commit e201665e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -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`. // file_ext will return the part after the last occurence of `.` in `path`.
// The `.` is included. // 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 { 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..] return path[pos..]
} }

View File

@ -585,9 +585,19 @@ fn test_is_executable_writable_readable() ? {
os.rm(file_name) or { panic(err) } 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.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('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() { fn test_join() {