diff --git a/vlib/os/os.v b/vlib/os/os.v index 402e241d4d..e1c875e1fc 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -642,21 +642,46 @@ pub fn file_ext(path string) string { return path[pos..] } -// dir will return the part of `path` before the last occurence of a `path_separator`. +// dir returns all but the last element of path, typically the path's directory. +// After dropping the final element, trailing slashes are removed. +// If the path is empty, dir returns ".". If the path consists entirely of separators, +// dir returns a single separator. +// The returned path does not end in a separator unless it is the root directory. pub fn dir(path string) string { - pos := path.last_index(path_separator) or { + if path == '' { return '.' } + mut pos := path.last_index(path_separator) or { + return '.' + } + if path.ends_with(path_separator) { + pos-- + } return path[..pos] } -// base_dir will return the base directory of `path`. -pub fn base_dir(path string) string { - posx := path.last_index(path_separator) or { +// base returns the last element of path. +// Trailing path separators are removed before extracting the last element. +// If the path is empty, base returns ".". If the path consists entirely of separators, base returns a +// single separator. +pub fn base(path string) string { + if path == '' { + return '.' + } + if path == path_separator { + return path_separator + } + if path.ends_with(path_separator) { + path2 := path[..path.len-1] + pos := path2.last_index(path_separator) or { + return path2.clone() + } + return path2[pos+1..] + } + pos := path.last_index(path_separator) or { return path.clone() } - // NB: *without* terminating / - return path[..posx] + return path[pos+1..] } // file_name will return all characters found after the last occurence of `path_separator`. diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 9e8b1a9de1..452b9570be 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -446,13 +446,15 @@ fn test_dir() { assert os.dir('os') == '.' } -fn test_basedir() { +fn test_base() { $if windows { - assert os.base_dir('v\\vlib\\os') == 'v\\vlib' + assert os.base('v\\vlib\\os') == 'os' + assert os.base('v\\vlib\\os\\') == 'os' } $else { - assert os.base_dir('v/vlib/os') == 'v/vlib' + assert os.base('v/vlib/os') == 'os' + assert os.base('v/vlib/os/') == 'os' } - assert os.base_dir('filename') == 'filename' + assert os.base('filename') == 'filename' } fn test_uname() { diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index d04c885c9e..d933ebaa47 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -27,7 +27,7 @@ fn get_vtmp_filename(base_file_name, postfix string) string { } pub fn compile(command string, pref &pref.Preferences) { - odir := os.base_dir(pref.out_name) + odir := os.dir(pref.out_name) // When pref.out_name is just the name of an executable, i.e. `./v -o executable main.v` // without a folder component, just use the current folder instead: mut output_folder := odir @@ -55,9 +55,7 @@ pub fn compile(command string, pref &pref.Preferences) { println('compilation took: ${util.bold(sw.elapsed().milliseconds().str())} ms') } // running does not require the parsers anymore - unsafe { - b.myfree() - } + unsafe {b.myfree()} if pref.is_test || pref.is_run { b.run_compiled_executable_and_exit() } @@ -68,9 +66,7 @@ pub fn compile(command string, pref &pref.Preferences) { fn (mut b Builder) myfree() { // for file in b.parsed_files { // } - unsafe { - b.parsed_files.free() - } + unsafe {b.parsed_files.free()} } fn (mut b Builder) run_compiled_executable_and_exit() { @@ -142,7 +138,7 @@ fn (mut v Builder) set_module_lookup_paths() { // 3.2) search in ~/.vmodules/ (i.e. modules installed with vpm) v.module_search_paths = [] if v.pref.is_test { - v.module_search_paths << os.base_dir(v.compiled_dir) // pdir of _test.v + v.module_search_paths << os.dir(v.compiled_dir) // pdir of _test.v } v.module_search_paths << v.compiled_dir x := os.join_path(v.compiled_dir, 'modules') @@ -263,7 +259,7 @@ pub fn (v &Builder) get_user_files() []string { v.log('> That brings in all other ordinary .v files in the same module too .') } user_files << single_test_v_file - dir = os.base_dir(single_test_v_file) + dir = os.dir(single_test_v_file) } does_exist := os.exists(dir) if !does_exist { diff --git a/vlib/v/vmod/vmod.v b/vlib/v/vmod/vmod.v index 9715d18feb..242631786b 100644 --- a/vlib/v/vmod/vmod.v +++ b/vlib/v/vmod/vmod.v @@ -109,7 +109,7 @@ fn (mut mcache ModFileCacher) traverse(mfolder string) ([]string, ModFileAndFold if mcache.check_for_stop(cfolder, files) { break } - cfolder = os.base_dir(cfolder) + cfolder = os.dir(cfolder) folders_so_far << cfolder levels++ }