summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-01-15 13:29:49 +0100
committerTyge Løvset <[email protected]>2022-01-15 13:29:49 +0100
commit12fc6070378f5f78b07f7ac1efdadfdb8534ff27 (patch)
tree7d27c7073f9bb23226060d6aa0dfdd6bf21f877a /examples
parent349ba35358f10923a62947aaee8056d14f1be74a (diff)
downloadSTC-modified-12fc6070378f5f78b07f7ac1efdadfdb8534ff27.tar.gz
STC-modified-12fc6070378f5f78b07f7ac1efdadfdb8534ff27.zip
Added back cregex with examples. Found the bug in his code.
Diffstat (limited to 'examples')
-rw-r--r--examples/regex1.c26
-rw-r--r--examples/regex2.c31
-rw-r--r--examples/regex_match.c35
-rw-r--r--examples/stack.c1
4 files changed, 92 insertions, 1 deletions
diff --git a/examples/regex1.c b/examples/regex1.c
new file mode 100644
index 00000000..7385d69e
--- /dev/null
+++ b/examples/regex1.c
@@ -0,0 +1,26 @@
+#include <stc/cstr.h>
+#include <stc/cregex.h>
+
+int main()
+{
+ c_auto (cstr, input)
+ c_auto (cregex, float_expr)
+ {
+ float_expr = cregex_new("[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)");
+ // Until "q" is given, ask for another number
+ while (true)
+ {
+ printf("Enter float number (q for quit): ");
+ cstr_getline(&input, stdin);
+
+ // Exit when the user inputs q
+ if (cstr_equals(input, "q"))
+ break;
+
+ if (cregex_matches(&float_expr, input.str))
+ printf("Input is a float\n");
+ else
+ printf("Invalid input : Not a float\n");
+ }
+ }
+}
diff --git a/examples/regex2.c b/examples/regex2.c
new file mode 100644
index 00000000..5509baa5
--- /dev/null
+++ b/examples/regex2.c
@@ -0,0 +1,31 @@
+#include <stc/cregex.h>
+#include <stc/csview.h>
+#include <stc/cstr.h>
+
+int main()
+{
+ const char* inputs[] = {"birthday: 1964-12-05", "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-\\.@:%_+~#=\\?&]*)?",
+ "!((abc|123)+)!",
+ };
+ c_forrange (i, c_arraylen(inputs))
+ {
+ c_auto (cregex, re)
+ {
+ re = cregex_new(patterns[i]);
+ csview m;
+ if (cregex_find_sv(&re, inputs[i], &m))
+ {
+ printf("input: %s\n", inputs[i]);
+ c_forrange (j, cregex_capture_size(re))
+ {
+ csview cap;
+ bool res = cregex_capture_sv(&re, j, &cap);
+ if (res) printf(" submatch %zu: " c_PRIsv "\n", j, c_ARGsv(cap));
+ }
+ puts("");
+ }
+ }
+ }
+}
diff --git a/examples/regex_match.c b/examples/regex_match.c
new file mode 100644
index 00000000..17e3355f
--- /dev/null
+++ b/examples/regex_match.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stc/csview.h>
+#include <stc/cregex.h>
+#include <stc/crandom.h>
+#define i_val double
+#define i_type Vecu64
+#include <stc/cstack.h>
+#include <time.h>
+
+
+int main()
+{
+ // Lets find the first sequence of digits in a string
+ const char *s = "Hello numeric world, there are 24 hours in a day, 3600 seconds in an hour."
+ " Around 365.25 days a year, and 52 weeks in a year."
+ " Boltzmann const: 1.38064852E-23, is very small."
+ " Bohrradius is 5.29177210903e-11, and Avogadros number is 6.02214076e23.";
+
+ 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);
+ } 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));
+ }
+ puts("");
+ }
+}
diff --git a/examples/stack.c b/examples/stack.c
index f392ecd1..29b39aef 100644
--- a/examples/stack.c
+++ b/examples/stack.c
@@ -1,6 +1,5 @@
#include <stdio.h>
-#include <stc/cstr.h>
#define i_tag i
#define i_val int