aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2021-11-26 11:04:15 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2021-11-26 11:13:42 +0200
commita8a54893c09c86e560e6fee9d7972419716dd56d (patch)
treeeae16c1efe034f8593de4cc7981268d584a027c1
parent234869bfab747a373ac8815fc567957a8b05237e (diff)
Add $size(string), $size(path), and $size(dir_path) functions
-rw-r--r--libbuild2/functions-path.cxx10
-rw-r--r--libbuild2/functions-string.cxx6
-rw-r--r--tests/function/path/testscript11
-rw-r--r--tests/function/string/testscript7
4 files changed, 34 insertions, 0 deletions
diff --git a/libbuild2/functions-path.cxx b/libbuild2/functions-path.cxx
index 72597ff..c23a8a8 100644
--- a/libbuild2/functions-path.cxx
+++ b/libbuild2/functions-path.cxx
@@ -422,6 +422,16 @@ namespace build2
f["size"] += [] (paths v) {return v.size ();};
f["size"] += [] (dir_paths v) {return v.size ();};
+ // $size(<path>)
+ // $size(<dir_path>)
+ //
+ // Return the number of characters (bytes) in the path. Note that for
+ // dir_path the result does not include the trailing directory separator
+ // (except for the POSIX root directory).
+ //
+ f["size"] += [] (path v) {return v.size ();};
+ f["size"] += [] (dir_path v) {return v.size ();};
+
// $sort(<paths> [, <flags>])
// $sort(<dir_paths> [, <flags>])
//
diff --git a/libbuild2/functions-string.cxx b/libbuild2/functions-string.cxx
index 18a4ed6..4a03a5e 100644
--- a/libbuild2/functions-string.cxx
+++ b/libbuild2/functions-string.cxx
@@ -83,6 +83,12 @@ namespace build2
//
f["size"] += [] (strings v) {return v.size ();};
+ // $size(<string>)
+ //
+ // Return the number of characters (bytes) in the string.
+ //
+ f["size"] += [] (string v) {return v.size ();};
+
// $sort(<strings> [, <flags>])
//
// Sort strings in ascending order.
diff --git a/tests/function/path/testscript b/tests/function/path/testscript
index e674497..55d727f 100644
--- a/tests/function/path/testscript
+++ b/tests/function/path/testscript
@@ -119,6 +119,17 @@ if! $posix
}
}
+: size
+:
+{
+ $* <'print $size([path] abc)' >'3' : basics
+ $* <'print $size([path] )' >'0' : zero
+
+ $* <'print $size([dir_path] abc)' >'3' : dir-basics
+ $* <'print $size([dir_path] abc/)' >'3' : dir-separator
+ $* <'print $size([dir_path] )' >'0' : dir-zero
+}
+
: invalid-path
:
p = ($posix ? /../foo : 'c:/../foo');
diff --git a/tests/function/string/testscript b/tests/function/string/testscript
index 5f80bfe..00835ce 100644
--- a/tests/function/string/testscript
+++ b/tests/function/string/testscript
@@ -39,3 +39,10 @@
$* <'print $sort([strings] a c b a, dedup)' >'a b c' : dedup
$* <'print $sort([strings] a C B a, icase)' >'a a B C' : icase
}
+
+: size
+:
+{
+ $* <'print $size([string] abc)' >'3' : basics
+ $* <'print $size([string] )' >'0' : zero
+}