aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-06-24 13:37:09 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-06-24 13:37:09 -0400
commit271017ba9970e4220e1bd0dc83ce146afe9222a2 (patch)
tree995233d21141f164aa5f9e1d9b7bab757124e622 /src
parente122e5525ef044c25c2cd888bc267f7a2bd91b4f (diff)
Add Path.has_extension() and update manpages/api docs
Diffstat (limited to 'src')
-rw-r--r--src/environment.c1
-rw-r--r--src/stdlib/paths.c16
-rw-r--r--src/stdlib/paths.h1
3 files changed, 18 insertions, 0 deletions
diff --git a/src/environment.c b/src/environment.c
index db17da94..5fb6714a 100644
--- a/src/environment.c
+++ b/src/environment.c
@@ -308,6 +308,7 @@ env_t *global_env(bool source_mapping)
{"from_components", "Path$from_components", "func(components:[Text] -> Path)"},
{"glob", "Path$glob", "func(path:Path -> [Path])"},
{"group", "Path$group", "func(path:Path, follow_symlinks=yes -> Text?)"},
+ {"has_extension", "Path$has_extension", "func(path:Path, extension:Text -> Bool)"},
{"is_directory", "Path$is_directory", "func(path:Path, follow_symlinks=yes -> Bool)"},
{"is_file", "Path$is_file", "func(path:Path, follow_symlinks=yes -> Bool)"},
{"is_pipe", "Path$is_pipe", "func(path:Path, follow_symlinks=yes -> Bool)"},
diff --git a/src/stdlib/paths.c b/src/stdlib/paths.c
index 5047d615..772fa1fd 100644
--- a/src/stdlib/paths.c
+++ b/src/stdlib/paths.c
@@ -609,6 +609,22 @@ public Text_t Path$extension(Path_t path, bool full)
return Text$from_str(extension);
}
+public bool Path$has_extension(Path_t path, Text_t extension)
+{
+ if (path.components.length < 2)
+ return extension.length == 0;
+
+ Text_t last = *(Text_t*)(path.components.data + path.components.stride*(path.components.length-1));
+
+ if (extension.length == 0)
+ return !Text$has(Text$from(last, I(2)), Text(".")) || Text$equal_values(last, Text(".."));
+
+ if (!Text$starts_with(extension, Text(".")))
+ extension = Texts(Text("."), extension);
+
+ return Text$ends_with(Text$from(last, I(2)), extension);
+}
+
public Path_t Path$child(Path_t path, Text_t name)
{
if (Text$has(name, Text("/")) || Text$has(name, Text(";")))
diff --git a/src/stdlib/paths.h b/src/stdlib/paths.h
index 4f94d3e4..3a9cdef7 100644
--- a/src/stdlib/paths.h
+++ b/src/stdlib/paths.h
@@ -51,6 +51,7 @@ Path_t Path$write_unique_bytes(Path_t path, List_t bytes);
Path_t Path$parent(Path_t path);
Text_t Path$base_name(Path_t path);
Text_t Path$extension(Path_t path, bool full);
+bool Path$has_extension(Path_t path, Text_t extension);
Path_t Path$child(Path_t path, Text_t name);
Path_t Path$sibling(Path_t path, Text_t name);
Path_t Path$with_extension(Path_t path, Text_t extension, bool replace);