summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-12-11 10:52:32 +0100
committerTyge Løvset <[email protected]>2022-12-11 10:52:32 +0100
commit0db528ed0062920e9bd5b2c7fcdc7506bd41abad (patch)
tree217b0c6b36964b84bac423bfc6a6c3333c0cd83d
parentf500cd5301433015e7860ea4446e81db29c9369e (diff)
parent3b616b5260f2f477563508b6eec5370506565da9 (diff)
downloadSTC-modified-0db528ed0062920e9bd5b2c7fcdc7506bd41abad.tar.gz
STC-modified-0db528ed0062920e9bd5b2c7fcdc7506bd41abad.zip
Merge branch 'master' of github.com:tylov/STC
-rw-r--r--docs/cregex_api.md10
-rw-r--r--examples/regex_replace.c18
-rw-r--r--include/stc/cregex.h12
-rw-r--r--src/cregex.c6
-rw-r--r--src/libstc.c8
5 files changed, 30 insertions, 24 deletions
diff --git a/docs/cregex_api.md b/docs/cregex_api.md
index f1fd7461..865e569e 100644
--- a/docs/cregex_api.md
+++ b/docs/cregex_api.md
@@ -38,11 +38,13 @@ int cregex_find_pattern(const char* pattern, const char* input, csview m
bool cregex_is_match(const cregex* re, const char* input);
-cstr cregex_replace(const cregex* re, const char* input, const char* replace, unsigned count);
+cstr cregex_replace(const cregex* re, const char* input, const char* replace);
cstr cregex_replace_sv(const cregex* re, csview 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, 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);
+cstr cregex_replace_pattern_n(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
```
@@ -104,7 +106,7 @@ int main() {
printf("Could not find any date\n");
// Lets change all dates into US date format MM/DD/YYYY:
- cstr us_input = cregex_replace(&re, input, "$2/$3/$1", 0);
+ cstr us_input = cregex_replace(&re, input, "$2/$3/$1");
printf("US input: %s\n", cstr_str(&us_input));
// Free allocated data
diff --git a/examples/regex_replace.c b/examples/regex_replace.c
index e88c559f..808ac086 100644
--- a/examples/regex_replace.c
+++ b/examples/regex_replace.c
@@ -2,11 +2,11 @@
#include <stc/cregex.h>
#include <stc/csview.h>
-bool add_10_years(int i, csview m, cstr* mstr) {
+bool add_10_years(int i, csview match, cstr* out) {
if (i == 1) { // group 1 matches year
int year;
- sscanf(m.str, "%4d", &year);
- cstr_printf(mstr, "%04d", year + 10);
+ sscanf(match.str, "%4d", &year); // scan 4 chars only
+ cstr_printf(out, "%04d", year + 10);
return true;
}
return false;
@@ -22,23 +22,23 @@ int main()
printf("INPUT: %s\n", input);
/* replace with a fixed string, extended all-in-one call: */
- cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD", 0, NULL, cre_default));
+ cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD"));
printf("fixed: %s\n", cstr_str(&str));
/* US date format, and add 10 years to dates: */
- cstr_take(&str, cregex_replace_pattern(pattern, input, "$1/$3/$2", 0, add_10_years, cre_default));
+ cstr_take(&str, cregex_replace_pattern_n(pattern, input, "$1/$3/$2", 0, add_10_years, cre_default));
printf("us+10: %s\n", cstr_str(&str));
/* Wrap first date inside []: */
- cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]", 1, NULL, cre_default));
+ cstr_take(&str, cregex_replace_pattern_n(pattern, input, "[$0]", 1, NULL, cre_default));
printf("brack: %s\n", cstr_str(&str));
/* Shows how to compile RE separately */
c_with (cregex re = cregex_from(pattern, cre_default), cregex_drop(&re)) {
if (cregex_captures(&re) == 0)
- continue; // break c_with
+ continue; /* break c_with */
/* European date format. */
- cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1", 0));
+ cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1"));
printf("euros: %s\n", cstr_str(&str));
/* Strip out everything but the matches */
@@ -47,7 +47,7 @@ int main()
}
/* Wrap all words in ${} */
- cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}", 0, NULL, cre_default));
+ cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}"));
printf("curly: %s\n", cstr_str(&str));
}
}
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index 877f4052..57157b47 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -124,14 +124,18 @@ cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsi
bool (*mfun)(int i, csview match, cstr* mstr), int rflags);
static inline
-cstr cregex_replace(const cregex* re, const char* input, const char* replace, unsigned count) {
+cstr cregex_replace(const cregex* re, const char* input, const char* replace) {
csview sv = {input, strlen(input)};
- return cregex_replace_sv(re, sv, replace, count, NULL, cre_default);
+ return cregex_replace_sv(re, sv, replace, 0, NULL, cre_default);
}
/* replace + compile RE pattern, and extra arguments */
-cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, unsigned count,
- bool (*mfun)(int i, csview match, cstr* mstr), int crflags);
+cstr cregex_replace_pattern_n(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_n(pattern, input, replace, 0, NULL, cre_default); }
+
/* destroy regex */
void cregex_drop(cregex* self);
diff --git a/src/cregex.c b/src/cregex.c
index 444843bf..41c3aaf8 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -1254,11 +1254,11 @@ cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned
}
cstr
-cregex_replace_pattern(const char* pattern, const char* input, const char* replace, unsigned count,
- bool (*mfun)(int, csview, cstr*), int crflags) {
+cregex_replace_pattern_n(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) != cre_success)
- return cstr_new("[[error: invalid regex pattern]]");
+ assert(0);
csview sv = {input, strlen(input)};
cstr out = cregex_replace_sv(&re, sv, replace, count, mfun, crflags);
cregex_drop(&re);
diff --git a/src/libstc.c b/src/libstc.c
index 30c610c6..e8980a7a 100644
--- a/src/libstc.c
+++ b/src/libstc.c
@@ -1,11 +1,11 @@
-#define STC_EXTERN // implement common extern, non-templated functions, e.g. _clist_mergesort().
-#define STC_HEADER // don't implement clist_int itself, just dummy declare it.
+#define STC_EXTERN // implement common extern, non-templated functions and dependencies.
#define i_val int
+#define i_header // don't implement clist_int itself, just dummy declare it.
#include "../include/stc/clist.h"
#define STC_IMPLEMENT // implement the following.
-#include "../include/stc/cstr.h"
+#include "../include/stc/cregex.h"
#include "../include/stc/csview.h"
-#include "../include/stc/crandom.h"
+//#include "../include/stc/crandom.h"