summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-02-05 23:21:51 +0100
committerTyge Løvset <[email protected]>2022-02-05 23:21:51 +0100
commit29409b257d9144010bd608afc19f66ee2fbaa337 (patch)
tree229c17af426a76d6fc7d773a9ac8c98ef7352405 /examples
parent53de8174dd788db62c219477e9cca0ee3ce757cb (diff)
downloadSTC-modified-29409b257d9144010bd608afc19f66ee2fbaa337.tar.gz
STC-modified-29409b257d9144010bd608afc19f66ee2fbaa337.zip
Switched to heavily modified version of Rob Pike's plan9 regexp9. -> now renamed to cregex, with new API.
Diffstat (limited to 'examples')
-rw-r--r--examples/regex1.c6
-rw-r--r--examples/regex2.c21
-rw-r--r--examples/regex_match.c18
3 files changed, 23 insertions, 22 deletions
diff --git a/examples/regex1.c b/examples/regex1.c
index 02d0f5f4..894fe2b1 100644
--- a/examples/regex1.c
+++ b/examples/regex1.c
@@ -10,7 +10,7 @@ int main(int argc, char* argv[])
c_auto (cstr, input)
c_auto (cregex, float_expr)
{
- float_expr = cregex_new("[+-]?[0-9]+(\\.[0-9]*)?|\\.[0-9]+");
+ float_expr = cregex_new("^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$", 0);
// Until "q" is given, ask for another number
while (true)
{
@@ -21,10 +21,12 @@ int main(int argc, char* argv[])
if (cstr_equals(input, "q"))
break;
- if (cregex_matches(&float_expr, input.str))
+ if (cregex_find(&float_expr, input.str, 0, NULL, 0) > 0)
printf("Input is a float\n");
else
printf("Invalid input : Not a float\n");
}
}
}
+
+#include "../src/cregex.c" \ No newline at end of file
diff --git a/examples/regex2.c b/examples/regex2.c
index 512f7e58..45de9aba 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -5,29 +5,28 @@
int main()
{
const char* inputs[] = {"date: 2024-02-29 leapyear day", "https://en.cppreference.com/w/cpp/regex/regex_search", "!123abcabc!"};
- const char* patterns[] = {"([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])",
- "(https?:\\/\\/|ftp:\\/\\/|www\\.)([0-9A-Za-z-@:%_+~#=]+\\.)+([a-z]{2,3})(\\/[\\/0-9A-Za-z-\\.@:%_+~#=\\?&]*)?",
+ const char* patterns[] = {"(\\d\\d\\d\\d)-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])",
+ "(https?://|ftp://|www\\.)([0-9A-Za-z@:%_+~#=-]+\\.)+([a-z][a-z][a-z]?)(/[/0-9A-Za-z\\.@:%_+~#=\\?&-]*)?",
"!((abc|123)+)!",
};
c_forrange (i, c_arraylen(inputs))
{
c_auto (cregex, re)
{
- re = cregex_new(patterns[i]);
- csview m;
+ re = cregex_new(patterns[i], 0);
+ cregmatch m[20];
printf("input: %s\n", inputs[i]);
- if (cregex_find_sv(&re, inputs[i], &m))
+ if (cregex_find(&re, inputs[i], 20, m, 0) > 0)
{
- c_forrange (j, cregex_capture_size(re))
+ c_forrange (j, cregex_captures(re))
{
- csview cap;
- if (cregex_capture_sv(&re, j, &cap))
- printf(" submatch %zu: " c_PRIsv "\n", j, c_ARGsv(cap));
- else
- printf(" FAILED index %zu\n", j);
+ csview cap = {m[j].str, m[j].len};
+ printf(" submatch %zu: " c_PRIsv "\n", j, c_ARGsv(cap));
}
puts("");
}
}
}
}
+
+#include "../src/cregex.c"
diff --git a/examples/regex_match.c b/examples/regex_match.c
index 17e3355f..be3c0682 100644
--- a/examples/regex_match.c
+++ b/examples/regex_match.c
@@ -3,8 +3,6 @@
#include <stc/cregex.h>
#include <stc/crandom.h>
#define i_val double
-#define i_type Vecu64
-#include <stc/cstack.h>
#include <time.h>
@@ -18,18 +16,20 @@ int main()
c_auto (cregex, re)
{
- re = cregex_new("[+-]?([0-9]*\\.)?[0-9]+([Ee][-+]?[0-9]+)?");
- cregex_match match;
- if (cregex_find(&re, s, &match)) {
- printf("Found digits at position %zu-%zu\n", match.start, match.end);
+ int res = cregex_compile(&re, "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?", 0);
+ printf("%d\n", res);
+ cregmatch m[10];
+ if (cregex_find(&re, s, 10, m, 0) > 0) {
+ printf("Found digits at position %zu-%zu\n", m[0].str - s, m[0].str - s + m[0].len);
} else {
printf("Could not find any digits\n");
}
- csview sv = {0};
- while (cregex_find_next_sv(&re, s, &sv)) {
- printf(c_PRIsv " ; ", c_ARGsv(sv));
+ while (cregex_find(&re, s, 1, m, creg_next) > 0) {
+ printf("%.*s ; ", m[0].len, m[0].str);
}
puts("");
}
}
+
+#include "../src/cregex.c"