aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-16 14:04:51 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-16 14:04:51 -0400
commit3cbacbd675d373edc83dd8f20be7cb7efbb3a506 (patch)
treea504695848f4383a24f1802fd4237c9260752cac
parentc5396d65a8f766266b81b58acb7b1a457e7491a2 (diff)
Add some basic pattern matching/suffix code for paths
-rw-r--r--environment.c8
-rw-r--r--test/paths.tm6
2 files changed, 14 insertions, 0 deletions
diff --git a/environment.c b/environment.c
index 10aa0b58..d3e27069 100644
--- a/environment.c
+++ b/environment.c
@@ -290,6 +290,14 @@ env_t *new_compilation_unit(CORD *libname)
{"write_bytes", "Path$write_bytes", "func(path:Path, bytes:[Byte], permissions=0o644[32])"},
{"write_unique", "Path$write_unique", "func(path:Path, text:Text)->Path"},
{"write_unique_bytes", "Path$write_unique_bytes", "func(path:Path, bytes:[Byte])->Path"},
+
+ // Text methods:
+ {"ends_with", "Text$ends_with", "func(path:Path, suffix:Text)->Bool"},
+ {"has", "Text$has", "func(path:Path, pattern:Pattern)->Bool"},
+ {"matches", "Text$matches", "func(path:Path, pattern:Pattern)->Bool"},
+ {"replace", "Text$replace", "func(path:Path, pattern:Pattern, replacement:Text, backref=$/\\/, recursive=yes)->Path"},
+ {"replace_all", "Text$replace_all", "func(path:Path, replacements:{Pattern:Text}, backref=$/\\/, recursive=yes)->Path"},
+ {"starts_with", "Text$starts_with", "func(path:Path, prefix:Text)->Bool"},
)},
{"Shell", Type(TextType, .lang="Shell", .env=namespace_env(env, "Shell")), "Shell_t", "Shell$info", TypedArray(ns_entry_t,
{"escape_int", "Int$value_as_text", "func(i:Int)->Shell"},
diff --git a/test/paths.tm b/test/paths.tm
index 946526bb..4ef0b028 100644
--- a/test/paths.tm
+++ b/test/paths.tm
@@ -81,6 +81,12 @@ func main():
>> (../foo):parent()
= (../)
+ >> (./foo.txt):ends_with(".txt")
+ = yes
+ >> (./foo.txt):matches($|{..}/foo{..}|)
+ = yes
+ >> (./foo.txt):replace($/.txt/, ".md")
+ = (./foo.md)
# Concatenation tests:
!! Basic relative path concatenation: