Fix os.stem, os.short_stem.

This commit is contained in:
Jeroen van Rijn
2026-02-13 12:36:15 +01:00
parent f8767e58c5
commit faca11d593
2 changed files with 33 additions and 14 deletions

View File

@@ -382,25 +382,21 @@ Returns an empty string if there is no stem. e.g: '.gitignore'.
Returns an empty string if there's a trailing path separator.
*/
stem :: proc(path: string) -> string {
if len(path) > 0 {
if is_path_separator(path[len(path) - 1]) {
// NOTE(tetra): Trailing separator
return ""
} else if path[0] == '.' {
return ""
}
// If the last character is a path separator, there is no file.
if is_path_separator(path[len(path) - 1]) {
return ""
}
// NOTE(tetra): Get the basename
path := path
if i := strings.last_index_any(path, Path_Separator_Chars); i != -1 {
path = path[i+1:]
// Get the base path.
p := base(path)
if i := strings.last_index_any(p, Path_Separator_Chars); i != -1 {
p = p[i+1:]
}
if i := strings.last_index_byte(path, '.'); i != -1 {
return path[:i]
if i := strings.last_index_byte(p, '.'); i != -1 {
return p[:i]
}
return path
return p
}
/*

View File

@@ -109,4 +109,27 @@ delete_split :: proc(s: []string) {
delete(part)
}
delete(s)
}
@(test)
test_stem :: proc(t: ^testing.T) {
@static stem := [][2]string{
{"builder.tar.gz", "builder.tar"},
{"./builder.tar.gz", "builder.tar"},
{"./builder/", ""},
}
@static short_stem := [][2]string{
{"builder.tar", "builder"},
{"./builder.tar", "builder"},
{"./builder/", ""},
}
for d in stem {
testing.expect_value(t, filepath.stem(d[0]), d[1])
}
for d in short_stem {
testing.expect_value(t, filepath.stem(d[0]), d[1])
}
}