diff options
| author | Tyge Lovset <[email protected]> | 2023-06-21 00:31:59 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2023-06-21 00:31:59 +0200 |
| commit | 3aa8a04f4fe38d828006b4713beb390ef526cea7 (patch) | |
| tree | d5bbd50969a4ace404b72cdc9a633b2e6fb2962b | |
| parent | d1a1ed75ee08ed1100748bdbdc6fb1c3136c4c6b (diff) | |
| download | STC-modified-3aa8a04f4fe38d828006b4713beb390ef526cea7.tar.gz STC-modified-3aa8a04f4fe38d828006b4713beb390ef526cea7.zip | |
Added cco_switch(x) {}, cco_case(val) {}, and cco_default {} to use inside coroutines.
| -rw-r--r-- | include/stc/algo/coroutine.h | 5 | ||||
| -rw-r--r-- | misc/examples/regex2.c | 2 |
2 files changed, 6 insertions, 1 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index b92507b8..727ce2a8 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -66,6 +66,11 @@ enum { #define cco_initial(co) ((co)->cco_state == 0) #define cco_done(co) ((co)->cco_state == cco_state_done) +/* Always use { } after cco_switch(x), cco_case(val), and cco_default. */ +#define cco_switch(x) for (intmax_t _sw = (intmax_t)(x), _once=1; _once; _once=0) +#define cco_case(val) if (_sw == (intmax_t)(val)) +#define cco_default else + #define cco_routine(co) \ for (int *_state = &(co)->cco_state; *_state != cco_state_done; *_state = cco_state_done) \ _resume: switch (*_state) case 0: // thanks, @liigo! diff --git a/misc/examples/regex2.c b/misc/examples/regex2.c index 5718a1d8..734190cb 100644 --- a/misc/examples/regex2.c +++ b/misc/examples/regex2.c @@ -26,7 +26,7 @@ int main() printf("\ninput: %s\n", s[i].input); c_formatch (j, &re, s[i].input) { - c_forrange (k, cregex_captures(&re)) + c_forrange (k, cregex_captures(&re) + 1) printf(" submatch %lld: %.*s\n", k, c_SV(j.match[k])); } } |
