v/vlib/filepath/filepath.v

51 lines
1.1 KiB
V
Raw Normal View History

2019-11-01 17:29:51 +01:00
module filepath
2019-12-23 11:09:22 +01:00
// ext returns the extension in the file `path`.
2019-11-01 17:29:51 +01:00
pub fn ext(path string) string {
2019-12-23 11:09:22 +01:00
pos := path.last_index('.') or {
return ''
2019-11-01 17:29:51 +01:00
}
2019-12-23 11:09:22 +01:00
return path[pos..]
2019-11-01 17:29:51 +01:00
}
2019-12-23 11:09:22 +01:00
// is_abs returns true if `path` is absolute.
2019-11-01 17:29:51 +01:00
pub fn is_abs(path string) bool {
$if windows {
return path[0] == `/` || // incase we're in MingGW bash
2019-12-19 22:29:37 +01:00
(path[0].is_letter() && path[1] == `:`)
2019-11-01 17:29:51 +01:00
}
return path[0] == `/`
}
2019-12-23 11:09:22 +01:00
// join returns path as string from string parameter(s).
2019-11-01 17:29:51 +01:00
pub fn join(base string, dirs ...string) string {
mut result := []string
result << base.trim_right('\\/')
2019-12-19 22:29:37 +01:00
for d in dirs {
result << d
}
2020-02-17 20:31:23 +01:00
return result.join(separator)
2019-11-01 17:29:51 +01:00
}
2019-12-19 22:29:37 +01:00
2019-12-23 11:09:22 +01:00
// dir returns all but the last element of path, typically the path's directory.
pub fn dir(path string) string {
2020-02-17 20:31:23 +01:00
pos := path.last_index(separator) or {
2019-12-23 11:09:22 +01:00
return '.'
}
return path[..pos]
}
// basedir returns a directory name from path
pub fn basedir(path string) string {
2020-02-17 20:31:23 +01:00
pos := path.last_index(separator) or {
2019-12-23 11:09:22 +01:00
return path
}
// NB: *without* terminating /
return path[..pos]
}
// filename returns a file name from path
pub fn filename(path string) string {
2020-02-17 20:31:23 +01:00
return path.all_after(separator)
2019-12-23 11:09:22 +01:00
}