diff options
| author | Tyge Løvset <[email protected]> | 2022-01-09 23:43:58 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-01-09 23:43:58 +0100 |
| commit | b1c62797f6344eaea6cb55959c9f438481359c75 (patch) | |
| tree | 672e2c270bc53d025ce20dfebf072d3423487e74 | |
| parent | 4925e503aa2234a3ad804256713276199d58dd0d (diff) | |
| download | STC-modified-b1c62797f6344eaea6cb55959c9f438481359c75.tar.gz STC-modified-b1c62797f6344eaea6cb55959c9f438481359c75.zip | |
Small cregex API change. Added 2 examples.
| -rw-r--r-- | examples/regex1.c | 26 | ||||
| -rw-r--r-- | examples/regex2.c | 26 | ||||
| -rw-r--r-- | include/stc/cregex.h | 12 | ||||
| -rw-r--r-- | tests/cregex_test.c | 4 |
4 files changed, 60 insertions, 8 deletions
diff --git a/examples/regex1.c b/examples/regex1.c new file mode 100644 index 00000000..9b450453 --- /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_is_match(&float_expr, input.str))
+ printf("Input is a float\n");
+ else
+ printf("Invalid input : Not a float\n");
+ }
+ }
+}
\ No newline at end of file diff --git a/examples/regex2.c b/examples/regex2.c new file mode 100644 index 00000000..73abee45 --- /dev/null +++ b/examples/regex2.c @@ -0,0 +1,26 @@ +#include <stc/cregex.h>
+#include <stc/csview.h>
+#include <stc/cstr.h>
+
+int main()
+{
+ const char* fnames[] = {"foofile.txt", "barfile.txt", "bazboy.dat", "zoidberg"};
+ c_auto (cregex, re)
+ {
+ re = cregex_new("([a-z]+)\\.([a-z]+)");
+
+ c_forrange (i, c_arraylen(fnames))
+ {
+ printf("%s\n", fnames[i]);
+ if (cregex_is_match(&re, fnames[i]))
+ {
+ c_forrange (j, cregex_capture_size(re))
+ {
+ csview cap; cregex_capture_v(&re, j, &cap);
+ printf(" submatch %d: " c_PRIsv "\n", j, c_ARGsv(cap));
+ }
+ puts("");
+ }
+ }
+ }
+}
diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 82e9a60c..84799d75 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -84,13 +84,13 @@ STC_INLINE bool cregex_find_v(cregex *rx, const char *s, csview *sv) { } /* get captured slice from capture group number index */ -STC_API bool cregex_capture(cregex rx, size_t index, cregex_match *m); +STC_API bool cregex_capture(cregex *rx, size_t index, cregex_match *m); /* get captured slice from capture group number index as a csview */ -STC_INLINE bool cregex_capture_v(cregex rx, size_t index, csview *sv) { +STC_INLINE bool cregex_capture_v(cregex *rx, size_t index, csview *sv) { cregex_match m; bool ret = cregex_capture(rx, index, &m); - *sv = c_make(csview){rx.input + m.start, m.end - m.start}; + *sv = c_make(csview){rx->input + m.start, m.end - m.start}; return ret; } @@ -901,7 +901,7 @@ STC_DEF bool cregex_find_next(cregex *rx, const char *s, cregex_match *m) STC_API bool cregex_find_next_v(cregex *rx, const char *s, csview *sv) { - cregex_match m = { (size_t)(sv->str - s), m.start + sv->size }; + cregex_match m = {(size_t)(sv->str - s), m.start + sv->size}; bool res = cregex_find_next(rx, s, &m); *sv = c_make(csview){s + m.start, m.end - m.start}; @@ -966,9 +966,9 @@ static cregex_node *_rx_find_capture_node(cregex_node *node, size_t index) } } -STC_DEF bool cregex_capture(cregex rx, size_t index, cregex_match *m) +STC_DEF bool cregex_capture(cregex *rx, size_t index, cregex_match *m) { - _rx_CapNode *cap = (_rx_CapNode *)_rx_find_capture_node(rx.nodes, index); + _rx_CapNode *cap = (_rx_CapNode *)_rx_find_capture_node(rx->nodes, index); if (!cap) return false; diff --git a/tests/cregex_test.c b/tests/cregex_test.c index 54973ee2..a2691152 100644 --- a/tests/cregex_test.c +++ b/tests/cregex_test.c @@ -296,8 +296,8 @@ START_TEST(captures_cap) ck_assert(cregex_find(&re, "xxabcdcde", &match)); cregex_match cap0, cap1; - cregex_capture(re, 0, &cap0); - cregex_capture(re, 1, &cap1); + cregex_capture(&re, 0, &cap0); + cregex_capture(&re, 1, &cap1); ck_assert_uint_eq(cap0.start, 2); ck_assert_uint_eq(cap0.end, 4); |
