aboutsummaryrefslogtreecommitdiff
path: root/stdlib/patterns.h
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-11-09 16:27:54 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-11-09 16:27:54 -0500
commit898bee15817573b5ab865a1dae7e52da310affa8 (patch)
treeb8531a828190997a63a1e1ef32f4aa568304e61c /stdlib/patterns.h
parent7a4f2e73addf6dfcde2a6b17b62b961608e556a0 (diff)
Introduce a `Match` struct to represent pattern matching results, which
improves the usability of a lot of the APIs. Also bugfix some issues with ranges.
Diffstat (limited to 'stdlib/patterns.h')
-rw-r--r--stdlib/patterns.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/stdlib/patterns.h b/stdlib/patterns.h
index 3c281754..bf8dee6b 100644
--- a/stdlib/patterns.h
+++ b/stdlib/patterns.h
@@ -14,12 +14,21 @@
#define Pattern(text) ((Pattern_t)Text(text))
#define Patterns(...) ((Pattern_t)Texts(__VA_ARGS__))
+typedef struct {
+ Text_t text;
+ Int_t index;
+ Array_t captures;
+} Match_t;
+
+typedef Match_t OptionalMatch_t;
+#define NULL_MATCH ((OptionalMatch_t){.index=NULL_INT})
+
Text_t Text$replace(Text_t str, Pattern_t pat, Text_t replacement, Pattern_t backref_pat, bool recursive);
Pattern_t Pattern$escape_text(Text_t text);
Text_t Text$replace_all(Text_t text, Table_t replacements, Pattern_t backref_pat, bool recursive);
Array_t Text$split(Text_t text, Pattern_t pattern);
Text_t Text$trim(Text_t text, Pattern_t pattern, bool trim_left, bool trim_right);
-OptionalInt_t Text$find(Text_t text, Pattern_t pattern, Int_t i);
+OptionalMatch_t Text$find(Text_t text, Pattern_t pattern, Int_t i);
Array_t Text$find_all(Text_t text, Pattern_t pattern);
PUREFUNC bool Text$has(Text_t text, Pattern_t pattern);
OptionalArray_t Text$matches(Text_t text, Pattern_t pattern);
@@ -29,6 +38,7 @@ Text_t Text$map(Text_t text, Pattern_t pattern, Closure_t fn);
#define Pattern$compare Text$compare
#define Pattern$equal Text$equal
+extern const TypeInfo_t Match$info;
extern const TypeInfo_t Pattern$info;
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0