diff --git a/vlib/compiler/cc.v b/vlib/compiler/cc.v index 4885d38b5f..cd29930485 100644 --- a/vlib/compiler/cc.v +++ b/vlib/compiler/cc.v @@ -7,6 +7,7 @@ module compiler import ( os time + filepath ) fn todo() { @@ -152,7 +153,7 @@ fn (v mut V) cc() { a << '-c' } else if v.pref.is_cache { - builtin_o_path := os.join(v_modules_path, 'cache', 'vlib', 'builtin.o') + builtin_o_path := filepath.join(v_modules_path, 'cache', 'vlib', 'builtin.o') a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed if os.file_exists(builtin_o_path) { libs = builtin_o_path diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 906287195a..6506f4999c 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -7,6 +7,7 @@ module compiler import ( os strings + filepath ) pub const ( @@ -650,11 +651,11 @@ pub fn (v &V) get_user_files() []string { mut user_files := []string if v.pref.is_test { - user_files << os.join(v.vroot,'vlib','compiler','preludes','tests_assertions.v') + user_files << filepath.join(v.vroot,'vlib','compiler','preludes','tests_assertions.v') } if v.pref.is_test && v.pref.is_stats { - user_files << os.join(v.vroot,'vlib','compiler','preludes','tests_with_stats.v') + user_files << filepath.join(v.vroot,'vlib','compiler','preludes','tests_with_stats.v') } // v volt/slack_test.v: compile all .v files to get the environment diff --git a/vlib/filepath/filepath.v b/vlib/filepath/filepath.v new file mode 100644 index 0000000000..016a84abf8 --- /dev/null +++ b/vlib/filepath/filepath.v @@ -0,0 +1,36 @@ +module filepath + +import( + os + strings +) + +// return the extension in the file `path` +pub fn ext(path string) string { + pos := path.last_index_byte(`.`) + if pos != -1 { + return path[pos..] + } + return '' +} + +// returns true if `path` is absolute +pub fn is_abs(path string) bool { + $if windows { + return path[0] == `/` || // incase we're in MingGW bash + (path[0].is_letter() && path[1] == `:`) + } + return path[0] == `/` +} + +// pass directories as parameters, returns path as string +// TODO use []string.join once ...string becomes "[]string" +pub fn join(base string, dirs ...string) string { + mut path := strings.new_builder(50) + path.write(base.trim_right('\\/')) + for d in dirs { + path.write(os.path_separator) + path.write(d) + } + return path.str() +} diff --git a/vlib/os/os.v b/vlib/os/os.v index 683719d314..ee225125fc 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -4,7 +4,7 @@ module os -import strings +import filepath #include #include @@ -920,13 +920,7 @@ pub fn mkdir_all(path string) { } } -// TODO use []string.join once ...string becomes "[]string" pub fn join(base string, dirs ...string) string { - mut path := strings.new_builder(50) - path.write(base.trim_right('\\/')) - for d in dirs { - path.write(os.path_separator) - path.write(d) - } - return path.str() + println('use filepath.join') + return filepath.join(base, dirs) }