aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2026-02-22 09:41:09 -0500
committerBruce Hill <bruce@bruce-hill.com>2026-02-22 09:41:09 -0500
commitbc89c94c76ce5b50f7b1508dd8d4be4bd37a50f5 (patch)
treec97236572f818d9727e13e68849063039929f43f
parent6128ee2156daa900f4528375b66dfaad525108f8 (diff)
Fixes for Path.with_extension()dev
-rw-r--r--src/environment.c1
-rw-r--r--src/stdlib/paths.c9
2 files changed, 6 insertions, 4 deletions
diff --git a/src/environment.c b/src/environment.c
index 82d20373..05a27c8d 100644
--- a/src/environment.c
+++ b/src/environment.c
@@ -338,6 +338,7 @@ env_t *global_env(bool source_mapping) {
{"sibling", "Path$sibling", "func(path:Path, name:Text -> Path)"}, //
{"subdirectories", "Path$children", "func(path:Path, include_hidden=no -> [Path])"}, //
{"unique_directory", "Path$unique_directory", "func(path:Path -> Path)"}, //
+ {"with_extension", "Path$with_extension", "func(path:Path, extension:Text, replace:Bool=yes -> Path)"}, //
{"write", "Path$write", "func(path:Path, text:Text, permissions=Int32(0o644) -> Result)"}, //
{"writer", "Path$writer",
"func(path:Path, append=no, permissions=Int32(0o644) -> func(text:Text, close=no -> Result))"}, //
diff --git a/src/stdlib/paths.c b/src/stdlib/paths.c
index e50f0420..9e4face7 100644
--- a/src/stdlib/paths.c
+++ b/src/stdlib/paths.c
@@ -755,14 +755,15 @@ OptionalPath_t Path$with_extension(Path_t path, Text_t extension, bool replace)
if (!path || path[0] == '\0') return NULL;
static char buf[PATH_MAX];
+ const char *ext = Text$as_c_string(extension);
if (replace) {
char *base = (char *)base_name_start(path);
char *dot = strchrnul(base, '.');
- if (extension.length > 0)
- snprintf(buf, sizeof(buf), "%.*s.%s", (int)(dot - path), path, Text$as_c_string(extension));
- else snprintf(buf, sizeof(buf), "%.*s", (int)(dot - path), path);
+ if (ext[0] == '.' || ext[0] == '\0') snprintf(buf, sizeof(buf), "%.*s%s", (int)(dot - path), path, ext);
+ else snprintf(buf, sizeof(buf), "%.*s.%s", (int)(dot - path), path, ext);
} else {
- snprintf(buf, sizeof(buf), "%s.%s", path, Text$as_c_string(extension));
+ if (ext[0] == '.' || ext[0] == '\0') snprintf(buf, sizeof(buf), "%s%s", path, ext);
+ else snprintf(buf, sizeof(buf), "%s.%s", path, ext);
}
return path_from_buf(buf);
}