summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-07-22 13:52:38 +0200
committerTyge Løvset <[email protected]>2022-07-22 13:52:38 +0200
commit28ee78e128c14fe309cb5f7cfc3f2172bf675ea7 (patch)
tree484370fe1b83e0954cfaa081b03d3f0a87341b86
parent698e70e7d3e7dcdf6b6e7b03689dabc184d34bf6 (diff)
downloadSTC-modified-28ee78e128c14fe309cb5f7cfc3f2172bf675ea7.tar.gz
STC-modified-28ee78e128c14fe309cb5f7cfc3f2172bf675ea7.zip
Last minor API changes for cregex. Added descriptions in header. Updated RE examples.
-rw-r--r--examples/regex2.c4
-rw-r--r--examples/regex_match.c1
-rw-r--r--examples/regex_replace.c28
-rw-r--r--include/stc/cregex.h24
-rw-r--r--src/cregex.c9
5 files changed, 35 insertions, 31 deletions
diff --git a/examples/regex2.c b/examples/regex2.c
index 7c17e663..23f774ba 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -1,6 +1,6 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
-#include <stc/csview.h>
int main()
{
@@ -18,7 +18,7 @@ int main()
printf("error in regex pattern: %d\n", res);
continue;
}
- cregmatch m[20];
+ csview m[20];
printf("input: %s\n", inputs[i]);
if (cregex_match(inputs[i], &re, m, 0) == 1)
{
diff --git a/examples/regex_match.c b/examples/regex_match.c
index 646f2318..821813bb 100644
--- a/examples/regex_match.c
+++ b/examples/regex_match.c
@@ -1,3 +1,4 @@
+#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
diff --git a/examples/regex_replace.c b/examples/regex_replace.c
index d57dede8..0b6c9a1e 100644
--- a/examples/regex_replace.c
+++ b/examples/regex_replace.c
@@ -1,7 +1,6 @@
#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
-#include <stc/csview.h>
cstr sub_20y(int i, csview m) {
if (i == 1) { // year
@@ -19,26 +18,31 @@ int main()
c_auto (cstr, str)
{
- printf("input: %s\n", input);
- /* European date format */
- cstr_take(&str, cregex_replace_pat(input, pattern, "\\3.\\2.\\1"));
- printf("euros: %s\n", cstr_str(&str));
+ printf("input: %s\n\n", input);
+
+ /* replace with a fixed string, extended all-in-one call: */
+ cstr_take(&str, cregex_replace_p(input, pattern, "YYYY-MM-DD"));
+ printf("fixed: %s\n", cstr_str(&str));
/* US date format, and subtract 20 years: */
- cstr_take(&str, cregex_replace_patx(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0));
+ cstr_take(&str, cregex_replace_pe(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0));
printf("us-20: %s\n", cstr_str(&str));
- /* replace with a fixed string: */
- cstr_take(&str, cregex_replace_pat(input, pattern, "YYYY-MM-DD"));
- printf("fixed: %s\n", cstr_str(&str));
-
/* Wrap first date inside []: */
- cstr_take(&str, cregex_replace_patx(input, pattern, "[\\0]", NULL, 1, 0));
+ cstr_take(&str, cregex_replace_pe(input, pattern, "[\\0]", NULL, 1, 0));
printf("brack: %s\n", cstr_str(&str));
/* Wrap all words in {} */
- cstr_take(&str, cregex_replace_pat("[52] apples and [31] mangoes", "[a-z]+", "{\\0}"));
+ cstr_take(&str, cregex_replace_p("[52] apples and [31] mangoes", "[a-z]+", "{\\0}"));
printf("curly: %s\n", cstr_str(&str));
+
+ /* European date format, compile RE separately */
+ cregex re = cregex_from(pattern, 0);
+ if (cregex_captures(&re) == 0)
+ continue;
+ cstr_take(&str, cregex_replace(input, &re, "\\3.\\2.\\1", NULL, 0));
+ cregex_drop(&re);
+ printf("euros: %s\n", cstr_str(&str));
}
}
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index e806dc06..f93a0c03 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -75,37 +75,37 @@ cregex cregex_init(void) {
return rx;
}
-/* return number of capture groups on success, or (negative) error code on failure. */
+/* return 1 on success, or negative error code on failure. */
int cregex_compile(cregex *self, const char* pattern, int cflags);
static inline
-cregex cregex_from(const char* pattern, int cflags, int* res) {
+cregex cregex_from(const char* pattern, int cflags) {
cregex rx = {0};
- int ret = cregex_compile(&rx, pattern, cflags);
- if (res) *res = ret;
+ cregex_compile(&rx, pattern, cflags);
return rx;
}
-/* number of capture groups in a regex pattern */
+/* number of capture groups in a regex pattern, 0 if regex is invalid */
int cregex_captures(const cregex* self);
/* return 1 on match, 0 on nomatch, and -1 on failure. */
int cregex_match(const char* input, const cregex* re,
csview match[], int mflags);
-int cregex_match_pat(const char* input, const char* pattern,
- csview match[], int cmflags);
+/* match + compile RE pattern */
+int cregex_match_p(const char* input, const char* pattern,
+ csview match[], int cmflags);
/* replace regular expression */
cstr cregex_replace(const char* input, const cregex* re, const char* replace,
cstr (*mfun)(int i, csview match), unsigned count);
-cstr cregex_replace_patx(const char* input, const char* pattern,
- const char* replace, cstr (*mfun)(int i, csview match),
- unsigned count, int cflags);
+/* replace + compile RE pattern and extra arguments */
+cstr cregex_replace_pe(const char* input, const char* pattern, const char* replace,
+ cstr (*mfun)(int i, csview match), unsigned count, int cflags);
static inline
-cstr cregex_replace_pat(const char* input, const char* pattern, const char* replace)
- { return cregex_replace_patx(input, pattern, replace, NULL, 0, 0); }
+cstr cregex_replace_p(const char* input, const char* pattern, const char* replace)
+ { return cregex_replace_pe(input, pattern, replace, NULL, 0, 0); }
/* destroy regex */
void cregex_drop(cregex* self);
diff --git a/src/cregex.c b/src/cregex.c
index 54b13431..8705b335 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -1221,8 +1221,8 @@ cregex_match(const char* input, const cregex* re,
}
}
-int cregex_match_pat(const char* input, const char* pattern,
- csview match[], int cmflags) {
+int cregex_match_p(const char* input, const char* pattern,
+ csview match[], int cmflags) {
cregex re = cregex_init();
int res = cregex_compile(&re, pattern, cmflags);
if (res < 0) return res;
@@ -1254,9 +1254,8 @@ cregex_replace(const char* input, const cregex* re, const char* replace,
}
cstr
-cregex_replace_patx(const char* input, const char* pattern,
- const char* replace, cstr (*mfun)(int i, csview match),
- unsigned count, int cflags) {
+cregex_replace_pe(const char* input, const char* pattern, const char* replace,
+ cstr (*mfun)(int i, csview match), unsigned count, int cflags) {
cregex re = cregex_init();
int res = cregex_compile(&re, pattern, cflags);
if (res < 0)