From 670393b5708e0262757da5052da9a61270c907b7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 20 May 2024 12:27:06 +0200 Subject: Add $path.absolute(), $path.simple(), $path.sub_path(), $path.super_path() --- libbuild2/functions-path.cxx | 74 ++++++++++++++++++++++++++++++++++++++++++ tests/function/path/testscript | 30 +++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/libbuild2/functions-path.cxx b/libbuild2/functions-path.cxx index 4b114f5..7dfbd67 100644 --- a/libbuild2/functions-path.cxx +++ b/libbuild2/functions-path.cxx @@ -344,6 +344,78 @@ namespace build2 return ns; }; + // $absolute() + // $path.absolute() + // + // Return true if the path is absolute and false otherwise. + // + f["absolute"] += [](path p) + { + return p.absolute (); + }; + + f[".absolute"] += [](names ns) + { + return convert (move (ns)).absolute (); + }; + + // $simple() + // $path.simple() + // + // Return true if the path is simple, that is, has no direcrory component, + // and false otherwise. + // + // Note that on POSIX `/foo` is not a simple path (it is `foo` in the root + // directory) while `/` is (it is the root directory). + // + f["simple"] += [](path p) + { + return p.simple (); + }; + + f[".simple"] += [](names ns) + { + return convert (move (ns)).simple (); + }; + + // $sub_path(, ) + // $path.sub_path(, ) + // + // Return true if the path specified as the first argument is a sub-path + // of the one specified as the second argument (in other words, the second + // argument is a prefix of the first) and false otherwise. Both paths are + // expected to be normalized. Note that this function returns true if the + // paths are equal. Empty path is considered a prefix of any path. + // + f["sub_path"] += [](path p, value v) + { + return p.sub (convert_to_base (move (v))); + }; + + f[".sub_path"] += [](names ns, value v) + { + return convert (move (ns)).sub (convert_to_base (move (v))); + }; + + // $super_path(, ) + // $path.super_path(, ) + // + // Return true if the path specified as the first argument is a super-path + // of the one specified as the second argument (in other words, the second + // argument is a suffix of the first) and false otherwise. Both paths are + // expected to be normalized. Note that this function returns true if the + // paths are equal. Empty path is considered a suffix of any path. + // + f["super_path"] += [](path p, value v) + { + return p.sup (convert_to_base (move (v))); + }; + + f[".super_path"] += [](names ns, value v) + { + return convert (move (ns)).sup (convert_to_base (move (v))); + }; + // $canonicalize() // $path.canonicalize() // @@ -615,6 +687,8 @@ namespace build2 // specified paths). Issue diagnostics and fail if a relative path cannot // be derived (for example, paths are on different drives on Windows). // + // Note: to check if a path if relative, use `$path.absolute()`. + // f["relative"] += [](path p, dir_path d) { return relative (p, d); diff --git a/tests/function/path/testscript b/tests/function/path/testscript index 1ed89ca..98491ea 100644 --- a/tests/function/path/testscript +++ b/tests/function/path/testscript @@ -80,6 +80,36 @@ s = ($posix ? '/' : '\') } } +: absolute +: +{ + $* <'print $absolute($src_root)' >"true" : true + $* <'print $path.absolute(a/b)' >"false" : false +} + +: simple +: +{ + $* <'print $simple([path] a)' >"true" : true + $* <'print $path.simple(a/b)' >"false" : false +} + +: sub_path +: +{ + $* <'print $sub_path($src_base, $src_root)' >"true" : true-absolute + $* <'print $path.sub_path(a/b/c, a/b)' >"true" : true-relative + $* <'print $path.sub_path(a/b/c, a/d)' >"false" : false +} + +: super_path +: +{ + $* <'print $super_path($src_base, true-absolute)' >"true" : true-absolute + $* <'print $path.super_path(a/b/c, b/c)' >"true" : true-relative + $* <'print $path.super_path(a/b/c, c/a)' >"false" : false +} + : canonicalize : { -- cgit v1.1