os: fix file_ext function (#14566)

Ben 2022-06-02 06:09:46 +02:00 committed by Chewing_Bever
parent df4dae6d40
commit 5b97307c5a
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
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`.
// 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..]
}

View File

@ -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() {