diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2024-05-20 12:27:06 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2024-05-20 12:27:06 +0200 |
commit | 670393b5708e0262757da5052da9a61270c907b7 (patch) | |
tree | df4051b52e25c0086e6c71405fa5febf0aee08b2 | |
parent | fd94c6f239d2436b6152935b4c99217129953a5d (diff) |
Add $path.absolute(), $path.simple(), $path.sub_path(), $path.super_path()
-rw-r--r-- | libbuild2/functions-path.cxx | 74 | ||||
-rw-r--r-- | tests/function/path/testscript | 30 |
2 files changed, 104 insertions, 0 deletions
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>) + // $path.absolute(<untyped>) + // + // Return true if the path is absolute and false otherwise. + // + f["absolute"] += [](path p) + { + return p.absolute (); + }; + + f[".absolute"] += [](names ns) + { + return convert<path> (move (ns)).absolute (); + }; + + // $simple(<path>) + // $path.simple(<untyped>) + // + // 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<path> (move (ns)).simple (); + }; + + // $sub_path(<path>, <path>) + // $path.sub_path(<untyped>, <untyped>) + // + // 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<path> (move (v))); + }; + + f[".sub_path"] += [](names ns, value v) + { + return convert<path> (move (ns)).sub (convert_to_base<path> (move (v))); + }; + + // $super_path(<path>, <path>) + // $path.super_path(<untyped>, <untyped>) + // + // 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<path> (move (v))); + }; + + f[".super_path"] += [](names ns, value v) + { + return convert<path> (move (ns)).sup (convert_to_base<path> (move (v))); + }; + // $canonicalize(<paths>) // $path.canonicalize(<untyped>) // @@ -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 : { |