From 6cb960121c4195dd9d12c92d4f3ff2819fad143e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 1 Dec 2016 14:59:32 +0200 Subject: Add path.canonicalize() function --- build2/functions-path.cxx | 24 ++++++++++++++++++++++++ tests/function/path/testscript | 13 ++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/build2/functions-path.cxx b/build2/functions-path.cxx index e21609c..243f0d7 100644 --- a/build2/functions-path.cxx +++ b/build2/functions-path.cxx @@ -93,6 +93,30 @@ namespace build2 return r; }; + // canonicalize + // + f["canonicalize"] = [](path p) {p.canonicalize (); return p;}; + f["canonicalize"] = [](dir_path p) {p.canonicalize (); return p;}; + + f["canonicalize"] = [](paths v) {for (auto& p: v) p.canonicalize (); return v;}; + f["canonicalize"] = [](dir_paths v) {for (auto& p: v) p.canonicalize (); return v;}; + + f[".canonicalize"] = [](names ns) + { + // For each path decide based on the presence of a trailing slash + // whether it is a directory. Return as untyped list of (potentially + // mixed) paths. + // + for (name& n: ns) + { + if (n.directory ()) + n.dir.canonicalize (); + else + n.value = convert (move (n)).canonicalize ().string (); + } + return ns; + }; + // normalize // f["normalize"] = [](path p) {p.normalize (); return p;}; diff --git a/tests/function/path/testscript b/tests/function/path/testscript index 8b527e3..e1f0a36 100644 --- a/tests/function/path/testscript +++ b/tests/function/path/testscript @@ -10,6 +10,17 @@ else s = '\' end +: canonicalize +: +{ + $* <'print $canonicalize([path] a/b)' >"a$(s)b" : path + $* <'print $canonicalize([paths] a/b a/c)' >"a$(s)b a$(s)c" : paths + $* <'print $canonicalize([dir_path] a/b)' >"a$(s)b$s" : dir-path + $* <'print $canonicalize([dir_paths] a/b a/c/)' >"a$(s)b$s a$(s)c$s" : dir-paths + $* <'print $path.canonicalize(a/b)' >"a$(s)b" : untyped + $* <'print $path.canonicalize(a/b/ a/c)' >"a$(s)b$s a$(s)c" : mixed +} + : normalize : { @@ -18,7 +29,7 @@ end $* <'print $normalize([dir_path] a/../b)' >"b$s" : dir-path $* <'print $normalize([dir_paths] a/../b a/../c/)' >"b$s c$s" : dir-paths $* <'print $path.normalize(a/../b)' >"b" : untyped - $* <'print $path.normalize(a/../b/ a/../b)' >"b$s b" : mixed + $* <'print $path.normalize(a/../b/ a/../c)' >"b$s c" : mixed } : invalid-path -- cgit v1.1