summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-26 09:51:02 +0100
committerTyge Løvset <[email protected]>2023-01-26 09:51:02 +0100
commit0b40c6af56231b4c2109596c88dcfce09c1429f7 (patch)
treed01d9be60a6d8d70111d8bc058b1e3a455fa0f99
parent68fd366ceaa919293d348ce15c2596d485160cec (diff)
downloadSTC-modified-0b40c6af56231b4c2109596c88dcfce09c1429f7.tar.gz
STC-modified-0b40c6af56231b4c2109596c88dcfce09c1429f7.zip
Updated API for cregex.
-rw-r--r--docs/cregex_api.md17
-rw-r--r--include/stc/ccommon.h2
-rw-r--r--include/stc/cregex.h81
-rw-r--r--misc/examples/regex_replace.c6
-rw-r--r--src/cregex.c17
5 files changed, 71 insertions, 52 deletions
diff --git a/docs/cregex_api.md b/docs/cregex_api.md
index c01f324c..3d4392b0 100644
--- a/docs/cregex_api.md
+++ b/docs/cregex_api.md
@@ -23,33 +23,34 @@ enum {
};
cregex cregex_init(void);
-cregex cregex_from(const char* pattern, int cflags);
+cregex cregex_from(const char* pattern, int cflags = CREG_DEFAULT);
/* return CREG_OK, or negative error code on failure */
-int cregex_compile(cregex *self, const char* pattern, int cflags);
+int cregex_compile(cregex *self, const char* pattern, int cflags = CREG_DEFAULT);
/* num. of capture groups in regex. 0 if RE is invalid. First group is the full match */
int cregex_captures(const cregex* self);
/* return CREG_OK, CREG_NOMATCH, or CREG_MATCHERROR */
-int cregex_find(const cregex* re, const char* input, csview match[], int mflags);
+int cregex_find(const cregex* re, const char* input, csview match[], int mflags = CREG_DEFAULT);
/* Search inside input string-view only */
int cregex_find_sv(const cregex* re, csview input, csview match[]);
/* All-in-one search (compile + find + drop) */
-int cregex_find_pattern(const char* pattern, const char* input, csview match[], int cmflags);
+int cregex_find_pattern(const char* pattern, const char* input, csview match[], int cmflags = CREG_DEFAULT);
/* Check if there are matches in input */
bool cregex_is_match(const cregex* re, const char* input);
/* Replace all matches in input */
-cstr cregex_replace(const cregex* re, const char* input, const char* replace);
+cstr cregex_replace(const cregex* re, const char* input, const char* replace, count = MAX_INT);
/* Replace count matches in input string-view. Optionally transform replacement with mfun. */
+cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count = MAX_INT);
cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count,
bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags);
/* All-in-one replacement (compile + find/replace + drop) */
-cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace);
-cstr cregex_replace_pattern_ex(const char* pattern, const char* input, const char* replace, unsigned count,
- bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags);
+cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, count = MAX_INT);
+cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, unsigned count,
+ bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags);
void cregex_drop(cregex* self); /* destroy */
```
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 9315dd99..b61c1972 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -56,7 +56,7 @@
/* Macro overloading feature support based on: https://rextester.com/ONP80107 */
#define c_MACRO_OVERLOAD(name, ...) \
- c_PASTE(c_CONCAT(name, _), c_NUMARGS(__VA_ARGS__))(__VA_ARGS__)
+ c_PASTE(name##_, c_NUMARGS(__VA_ARGS__))(__VA_ARGS__)
#define c_CONCAT(a, b) a ## b
#define c_PASTE(a, b) c_CONCAT(a, b)
#define c_EXPAND(...) __VA_ARGS__
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index 24faba6f..206c319d 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -33,6 +33,7 @@ THE SOFTWARE.
#include <stdbool.h>
#include <string.h>
#include "forward.h" // csview
+#include "ccommon.h"
enum {
CREG_DEFAULT = 0,
@@ -81,64 +82,82 @@ typedef struct {
#define c_FORMATCH(it, Re, Input) \
for (cregex_iter it = {Re, Input}; \
- cregex_find(it.re, it.input, it.match, CREG_M_NEXT) == CREG_OK; )
+ cregex_find_4(it.re, it.input, it.match, CREG_M_NEXT) == CREG_OK; )
-static inline
-cregex cregex_init(void) {
+STC_INLINE cregex cregex_init(void) {
cregex re = {0};
return re;
}
/* return CREG_OK, or negative error code on failure. */
-int cregex_compile(cregex *self, const char* pattern, int cflags);
+#define cregex_compile(...) c_MACRO_OVERLOAD(cregex_compile, __VA_ARGS__)
+#define cregex_compile_2(re, pattern) cregex_compile_3(re, pattern, CREG_DEFAULT)
+int cregex_compile_3(cregex *re, const char* pattern, int cflags);
-static inline
-cregex cregex_from(const char* pattern, int cflags) {
+/* construct and return a regex from a pattern. */
+#define cregex_from(...) c_MACRO_OVERLOAD(cregex_from, __VA_ARGS__)
+#define cregex_from_1(pattern) cregex_from_2(pattern, CREG_DEFAULT)
+
+STC_INLINE cregex cregex_from_2(const char* pattern, int cflags) {
cregex re = {0};
- cregex_compile(&re, pattern, cflags);
+ cregex_compile_3(&re, pattern, cflags);
return re;
}
-/* number of capture groups in a regex pattern, 0 if regex is invalid */
-unsigned cregex_captures(const cregex* self);
+/* number of capture groups in a regex pattern including full the match capture, 0 if regex is invalid */
+unsigned cregex_captures(const cregex* re);
/* return CREG_OK, CREG_NOMATCH or CREG_MATCHERROR. */
-int cregex_find(const cregex* re, const char* input,
- csview match[], int mflags);
-static inline
-int cregex_find_sv(const cregex* re, csview input, csview match[]) {
+#define cregex_find(...) c_MACRO_OVERLOAD(cregex_find, __VA_ARGS__)
+#define cregex_find_3(re, input, match) cregex_find_4(re, input, match, CREG_DEFAULT)
+int cregex_find_4(const cregex* re, const char* input, csview match[], int mflags);
+
+/* find with csview as input. */
+STC_INLINE int cregex_find_sv(const cregex* re, csview input, csview match[]) {
csview *mp = NULL;
if (match) { match[0] = input; mp = match; }
return cregex_find(re, input.str, mp, CREG_M_STARTEND);
}
/* match + compile RE pattern */
-int cregex_find_pattern(const char* pattern, const char* input,
- csview match[], int cmflags);
-
-static inline
-bool cregex_is_match(const cregex* re, const char* input)
- { return cregex_find(re, input, NULL, CREG_DEFAULT) == CREG_OK; }
-
-/* replace regular expression */
-cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count,
+#define cregex_find_pattern(...) c_MACRO_OVERLOAD(cregex_find_pattern, __VA_ARGS__)
+#define cregex_find_pattern_3(pattern, input, match) \
+ cregex_find_pattern_4(pattern, input, match, CREG_DEFAULT)
+int cregex_find_pattern_4(const char* pattern, const char* input,
+ csview match[], int cmflags);
+
+STC_INLINE bool cregex_is_match(const cregex* re, const char* input)
+ { return cregex_find_4(re, input, NULL, CREG_DEFAULT) == CREG_OK; }
+
+/* replace csview input with replace using regular expression pattern */
+#define cregex_replace_sv(...) c_MACRO_OVERLOAD(cregex_replace_sv, __VA_ARGS__)
+#define cregex_replace_sv_3(pattern, input, replace) \
+ cregex_replace_sv_4(pattern, input, replace, ~0U)
+#define cregex_replace_sv_4(pattern, input, replace, count) \
+ cregex_replace_sv_6(pattern, input, replace, count, NULL, CREG_DEFAULT)
+cstr cregex_replace_sv_6(const cregex* re, csview input, const char* replace, unsigned count,
bool (*mfun)(int i, csview match, cstr* mstr), int rflags);
-static inline
-cstr cregex_replace(const cregex* re, const char* input, const char* replace) {
+/* replace input with replace using regular expression */
+#define cregex_replace(...) c_MACRO_OVERLOAD(cregex_replace, __VA_ARGS__)
+#define cregex_replace_3(re, input, replace) cregex_replace_4(re, input, replace, ~0U)
+
+STC_INLINE cstr cregex_replace_4(const cregex* re, const char* input, const char* replace, unsigned count) {
csview sv = {input, strlen(input)};
- return cregex_replace_sv(re, sv, replace, ~0U, NULL, CREG_DEFAULT);
+ return cregex_replace_sv_4(re, sv, replace, count);
}
/* replace + compile RE pattern, and extra arguments */
-cstr cregex_replace_pattern_ex(const char* pattern, const char* input, const char* replace, unsigned count,
- bool (*mfun)(int i, csview match, cstr* mstr), int crflags);
-static inline
-cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace)
- { return cregex_replace_pattern_ex(pattern, input, replace, ~0U, NULL, CREG_DEFAULT); }
+#define cregex_replace_pattern(...) c_MACRO_OVERLOAD(cregex_replace_pattern, __VA_ARGS__)
+#define cregex_replace_pattern_3(pattern, input, replace) \
+ cregex_replace_pattern_4(pattern, input, replace, ~0U)
+#define cregex_replace_pattern_4(pattern, input, replace, count) \
+ cregex_replace_pattern_6(pattern, input, replace, count, NULL, CREG_DEFAULT)
+cstr cregex_replace_pattern_6(const char* pattern, const char* input, const char* replace, unsigned count,
+ bool (*mfun)(int i, csview match, cstr* mstr), int crflags);
/* destroy regex */
-void cregex_drop(cregex* self);
+void cregex_drop(cregex* re);
#endif // CREGEX_H_INCLUDED
#if defined i_extern || defined STC_EXTERN
diff --git a/misc/examples/regex_replace.c b/misc/examples/regex_replace.c
index a170856b..ec38ca56 100644
--- a/misc/examples/regex_replace.c
+++ b/misc/examples/regex_replace.c
@@ -26,15 +26,15 @@ int main()
printf("fixed: %s\n", cstr_str(&str));
/* US date format, and add 10 years to dates: */
- cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT));
+ cstr_take(&str, cregex_replace_pattern(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT));
printf("us+10: %s\n", cstr_str(&str));
/* Wrap first date inside []: */
- cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "[$0]", 1, NULL, CREG_DEFAULT));
+ cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]"));
printf("brack: %s\n", cstr_str(&str));
/* Shows how to compile RE separately */
- c_WITH (cregex re = cregex_from(pattern, CREG_DEFAULT), cregex_drop(&re)) {
+ c_WITH (cregex re = cregex_from(pattern), cregex_drop(&re)) {
if (cregex_captures(&re) == 0)
continue; /* break c_WITH */
/* European date format. */
diff --git a/src/cregex.c b/src/cregex.c
index d2b54ef1..cf2f8eaf 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -1239,7 +1239,7 @@ _build_subst(const char* replace, unsigned nmatch, const csview match[],
*/
int
-cregex_compile(cregex *self, const char* pattern, int cflags) {
+cregex_compile_3(cregex *self, const char* pattern, int cflags) {
_Parser par;
self->prog = _regcomp1(self->prog, &par, pattern, cflags);
return self->error = par.error;
@@ -1251,8 +1251,7 @@ cregex_captures(const cregex* self) {
}
int
-cregex_find(const cregex* re, const char* input,
- csview match[], int mflags) {
+cregex_find_4(const cregex* re, const char* input, csview match[], int mflags) {
int res = _regexec(re->prog, input, cregex_captures(re), match, mflags);
switch (res) {
case 1: return CREG_OK;
@@ -1262,8 +1261,8 @@ cregex_find(const cregex* re, const char* input,
}
int
-cregex_find_pattern(const char* pattern, const char* input,
- csview match[], int cmflags) {
+cregex_find_pattern_4(const char* pattern, const char* input,
+ csview match[], int cmflags) {
cregex re = cregex_init();
int res = cregex_compile(&re, pattern, cmflags);
if (res != CREG_OK) return res;
@@ -1273,8 +1272,8 @@ cregex_find_pattern(const char* pattern, const char* input,
}
cstr
-cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count,
- bool (*mfun)(int, csview, cstr*), int rflags) {
+cregex_replace_sv_6(const cregex* re, csview input, const char* replace, unsigned count,
+ bool (*mfun)(int, csview, cstr*), int rflags) {
cstr out = cstr_NULL;
cstr subst = cstr_NULL;
csview match[CREG_MAX_CAPTURES];
@@ -1296,8 +1295,8 @@ cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned
}
cstr
-cregex_replace_pattern_ex(const char* pattern, const char* input, const char* replace, unsigned count,
- bool (*mfun)(int, csview, cstr*), int crflags) {
+cregex_replace_pattern_6(const char* pattern, const char* input, const char* replace, unsigned count,
+ bool (*mfun)(int, csview, cstr*), int crflags) {
cregex re = cregex_init();
if (cregex_compile(&re, pattern, crflags) != CREG_OK)
assert(0);