summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-07-20 23:13:11 +0200
committerTyge Lovset <[email protected]>2022-07-20 23:13:11 +0200
commit3f89c290700618eae78eaa289bdb88d1cfb3514d (patch)
tree32dd44f8ca210aee69039f8779be611f7885cd5b /examples
parent78cb61301df13fee995d3afd1bd1d8d63310d819 (diff)
downloadSTC-modified-3f89c290700618eae78eaa289bdb88d1cfb3514d.tar.gz
STC-modified-3f89c290700618eae78eaa289bdb88d1cfb3514d.zip
Added cregex_replace*() [implemented in utf8code.c]. Added examples/regex_replace.c. Docs not ready, i.e. API not fixed. Some other refactoring and minor fixes/improvements. cstr_assign_sv() now returns char* like the other cstr_assign*().
Diffstat (limited to 'examples')
-rw-r--r--examples/regex2.c2
-rw-r--r--examples/regex_match.c6
-rw-r--r--examples/regex_replace.c35
3 files changed, 39 insertions, 4 deletions
diff --git a/examples/regex2.c b/examples/regex2.c
index 82247da5..1f3163f7 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -22,7 +22,7 @@ int main()
printf("input: %s\n", inputs[i]);
if (cregex_match(&re, inputs[i], 20, m, 0) > 0)
{
- c_forrange (j, cregex_captures(re))
+ c_forrange (j, cregex_captures(&re))
{
printf(" submatch %" PRIuMAX ": %" c_PRIsv "\n", j, c_ARGsv(m[j]));
}
diff --git a/examples/regex_match.c b/examples/regex_match.c
index 2b135bb7..5680b55e 100644
--- a/examples/regex_match.c
+++ b/examples/regex_match.c
@@ -13,14 +13,14 @@ int main()
{
int res = cregex_compile(&re, "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?", 0);
printf("%d\n", res);
- cregmatch m[10];
+ csview m[10];
if (cregex_match(&re, s, 10, m, 0) > 0) {
printf("Found digits at position %" PRIuMAX "-%" PRIuMAX "\n", m[0].str - s, m[0].str - s + m[0].size);
} else {
printf("Could not find any digits\n");
}
- while (cregex_match(&re, s, 10, m, creg_next) > 0) {
+ while (cregex_match(&re, s, 10, m, cregex_NEXT) > 0) {
printf("%" c_PRIsv " ; ", c_ARGsv(m[0]));
}
puts("");
@@ -28,7 +28,7 @@ int main()
res = cregex_compile(&re, "(.+)\\b(.+)", 0);
printf("groups: %d\n", res);
if ((res = cregex_match(&re, "hello@wørld", 10, m, 0)) > 0) {
- c_forrange (i, res)
+ c_forrange (i, res)
printf("match: [%" c_PRIsv "]\n", c_ARGsv(m[i]));
} else
printf("err: %d\n", res);
diff --git a/examples/regex_replace.c b/examples/regex_replace.c
new file mode 100644
index 00000000..1216701f
--- /dev/null
+++ b/examples/regex_replace.c
@@ -0,0 +1,35 @@
+#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
+ int year;
+ sscanf(m.str, "%4d", &year);
+ return cstr_from_fmt("%04d", year - 20);
+ }
+ return cstr_from_sv(m);
+}
+
+int main()
+{
+ const char* pattern = "\\b(\\d\\d\\d\\d)-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])\\b";
+ const char* input = "start date: 2015-12-31, end date: 2022-02-28";
+
+ c_auto (cregex, re)
+ c_auto (cstr, str1, str2)
+ {
+ printf("input: %s\n", input);
+ /* European date format */
+ str1 = cregex_replace(input, pattern, "\\3.\\2.\\1");
+ printf("euros: %s\n", cstr_str(&str1));
+
+ /* US date format, and subtract 20 years: */
+ str2 = cregex_replace_fn(input, pattern, "\\1/\\3/\\2", sub_20y, 0, 0);
+ printf("us-20: %s\n", cstr_str(&str2));
+ }
+}
+
+#include "../src/cregex.c"
+#include "../src/utf8code.c"