1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os, filepath: reorganize functions

This commit is contained in:
Alexey
2019-12-23 13:09:22 +03:00
committed by Alexander Medvednikov
parent 6e130cd446
commit dced76d1a4
28 changed files with 174 additions and 98 deletions

View File

@ -3,18 +3,16 @@ module filepath
import (
os
)
// return the extension in the file `path`
// ext returns the extension in the file `path`.
pub fn ext(path string) string {
pos := path.last_index_byte(`.`)
if pos != -1 {
return path[pos..]
pos := path.last_index('.') or {
return ''
}
return ''
return path[pos..]
}
// returns true if `path` is absolute
// is_abs returns true if `path` is absolute.
pub fn is_abs(path string) bool {
$if windows {
return path[0] == `/` || // incase we're in MingGW bash
@ -23,8 +21,7 @@ pub fn is_abs(path string) bool {
return path[0] == `/`
}
// pass directories as parameters, returns path as string
// TODO use []string.join once ...string becomes "[]string"
// join returns path as string from string parameter(s).
pub fn join(base string, dirs ...string) string {
mut result := []string
result << base.trim_right('\\/')
@ -34,3 +31,27 @@ pub fn join(base string, dirs ...string) string {
return result.join(os.path_separator)
}
// dir returns all but the last element of path, typically the path's directory.
pub fn dir(path string) string {
if path == '.' {
return os.getwd()
}
pos := path.last_index(os.path_separator) or {
return '.'
}
return path[..pos]
}
// basedir returns a directory name from path
pub fn basedir(path string) string {
pos := path.last_index(os.path_separator) or {
return path
}
// NB: *without* terminating /
return path[..pos]
}
// filename returns a file name from path
pub fn filename(path string) string {
return path.all_after(os.path_separator)
}