| 1 |
|
| # The walk from Perl: next, previous, between, first, iterate, contains. |
| 2 |
|
| |
| 3 |
|
| MODULE = Time::Keeper::Recurrence PACKAGE = Time::Keeper::Recurrence |
| 4 |
|
| |
| 5 |
|
| SV * |
| 6 |
|
| next(self, ...) |
| 7 |
|
| SV *self |
| 8 |
|
| ALIAS: |
| 9 |
|
| previous = 1 |
| 10 |
|
| PREINIT: |
| 11 |
|
| static const char *const whats[] = { "Time::Keeper::Recurrence->next", "Time::Keeper::Recurrence->previous" }; |
| 12 |
|
| const tkr_rule *r; |
| 13 |
7697 |
| int64_t from, got, detail = 0; |
| 14 |
|
| int rc; |
| 15 |
|
| CODE: |
| 16 |
7697 |
| r = tkr_rule_of(aTHX_ self, whats[ix]); |
| 17 |
7697 |
▶ | from = items > 1 ? tkr_instant_arg(aTHX_ ST(1), whats[ix]) : tkr_now_sec(aTHX); |
Branch: 1 branch
| Branch | True | False | Total |
| from = items > 1 ? tkr_instant_arg(aTHX_ ST(1), whats[ix]) : tkr_now_sec(aTHX); |
7696 |
1 |
7697 |
|
| 18 |
7696 |
▶ | if (r->flags & TKR_F_EVERY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->flags & TKR_F_EVERY) { |
5 |
7691 |
7696 |
|
| 19 |
5 |
▶ | got = ix ? from - r->every_sec : from + r->every_sec; |
Branch: 1 branch
| Branch | True | False | Total |
| got = ix ? from - r->every_sec : from + r->every_sec; |
1 |
4 |
5 |
|
| 20 |
5 |
| rc = TKR_W_GOT; |
| 21 |
|
| } |
| 22 |
|
| else |
| 23 |
7691 |
▶ | rc = ix ? tkr_previous_before(aTHX_ r, from, &got, &detail) : tkr_next_after(aTHX_ r, from, 0, &got, &detail); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = ix ? tkr_previous_before(aTHX_ r, from, &got, &detail) : tkr_next_after(aTHX_ r, from, 0, &got, &detail); |
2836 |
4855 |
7691 |
|
| 24 |
7696 |
| tkr_rc_croak(aTHX_ rc, r, detail, whats[ix]); |
| 25 |
7692 |
▶ | RETVAL = rc == TKR_W_GOT ? tkr_occurrence_sv(aTHX_ r, got, whats[ix]) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = rc == TKR_W_GOT ? tkr_occurrence_sv(aTHX_ r, got, whats[ix]) : &PL_sv_undef; |
7690 |
2 |
7692 |
|
| 26 |
|
| OUTPUT: |
| 27 |
|
| RETVAL |
| 28 |
|
| |
| 29 |
|
| void |
| 30 |
|
| between(self, a, b) |
| 31 |
|
| SV *self |
| 32 |
|
| SV *a |
| 33 |
|
| SV *b |
| 34 |
|
| PREINIT: |
| 35 |
372 |
| const char *what = "Time::Keeper::Recurrence->between"; |
| 36 |
|
| const tkr_rule *r; |
| 37 |
372 |
| int64_t from, to, got, n = 0, i; |
| 38 |
|
| SV *held; |
| 39 |
|
| tkr_walk w; |
| 40 |
|
| int rc; |
| 41 |
|
| PPCODE: |
| 42 |
372 |
| r = tkr_rule_of(aTHX_ self, what); |
| 43 |
372 |
| from = tkr_instant_arg(aTHX_ a, what); |
| 44 |
372 |
| to = tkr_instant_arg(aTHX_ b, what); |
| 45 |
372 |
▶ | if (r->flags & TKR_F_EVERY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->flags & TKR_F_EVERY) |
0 |
372 |
372 |
|
| 46 |
0 |
| croak("%s: an @every cron has no occurrences of its own, only the next after a moment", what); |
| 47 |
372 |
| held = sv_2mortal(newSV(64 * sizeof(int64_t))); |
| 48 |
372 |
| tkr_walk_init(aTHX_ &w, r, tkr_wall_of(aTHX_ r, from, NULL) - tkr_lookback(aTHX_ r, from), 0); |
| 49 |
10003128 |
▶ | while ((rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT) { |
10003098 |
30 |
10003128 |
|
| 50 |
10003098 |
▶ | if (got < from) |
Branch: 1 branch
| Branch | True | False | Total |
| if (got < from) |
369 |
10002729 |
10003098 |
|
| 51 |
369 |
| continue; |
| 52 |
10002729 |
▶ | if (got >= to) |
Branch: 1 branch
| Branch | True | False | Total |
| if (got >= to) |
341 |
10002388 |
10002729 |
|
| 53 |
341 |
| break; |
| 54 |
10002388 |
▶ | if (n >= TKR_BETWEEN_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n >= TKR_BETWEEN_MAX) { |
1 |
10002387 |
10002388 |
|
| 55 |
1 |
| tkr_walk_free(&w); |
| 56 |
1 |
| croak("%s: more than %d occurrences; use iterate", what, TKR_BETWEEN_MAX); |
| 57 |
|
| } |
| 58 |
10002387 |
▶ | if ((STRLEN)(n + 1) * sizeof(int64_t) > SvLEN(held)) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STRLEN)(n + 1) * sizeof(int64_t) > SvLEN(held)) |
22 |
10002365 |
10002387 |
|
| 59 |
22 |
▶ | SvGROW(held, SvLEN(held) * 2); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(held, SvLEN(held) * 2); |
22 |
0 |
22 |
| SvGROW(held, SvLEN(held) * 2); |
22 |
0 |
22 |
|
| 60 |
10002387 |
| ((int64_t *)SvPVX(held))[n++] = got; |
| 61 |
|
| } |
| 62 |
371 |
| tkr_rc_croak(aTHX_ rc, r, w.fail_wall, what); |
| 63 |
371 |
| tkr_walk_free(&w); |
| 64 |
371 |
▶ | EXTEND(SP, n); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, n); |
371 |
0 |
371 |
| EXTEND(SP, n); |
0 |
371 |
371 |
|
| 65 |
2758 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
2387 |
371 |
2758 |
|
| 66 |
2387 |
| PUSHs(sv_2mortal(tkr_occurrence_sv(aTHX_ r, ((int64_t *)SvPVX(held))[i], what))); |
| 67 |
|
| |
| 68 |
|
| void |
| 69 |
|
| first(self, n) |
| 70 |
|
| SV *self |
| 71 |
|
| IV n |
| 72 |
|
| PREINIT: |
| 73 |
1526 |
| const char *what = "Time::Keeper::Recurrence->first"; |
| 74 |
|
| const tkr_rule *r; |
| 75 |
|
| int64_t got, from; |
| 76 |
|
| tkr_walk w; |
| 77 |
|
| int rc; |
| 78 |
1526 |
| IV i = 0; |
| 79 |
|
| PPCODE: |
| 80 |
1526 |
| r = tkr_rule_of(aTHX_ self, what); |
| 81 |
1526 |
▶ | if (n < 1 || n > TKR_BETWEEN_MAX) |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 1 || n > TKR_BETWEEN_MAX) |
1525 |
1 |
1526 |
| if (n < 1 || n > TKR_BETWEEN_MAX) |
0 |
1525 |
1525 |
|
| 82 |
1 |
| croak("%s: how many? %" IVdf " is not 1 to %d", what, n, TKR_BETWEEN_MAX); |
| 83 |
1525 |
▶ | if (r->flags & TKR_F_EVERY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->flags & TKR_F_EVERY) |
1 |
1524 |
1525 |
|
| 84 |
1 |
| croak("%s: an @every cron has no occurrences of its own, only the next after a moment", what); |
| 85 |
1524 |
▶ | from = (r->flags & TKR_F_HAS_START) ? r->start.sec : tkr_now_sec(aTHX); |
Branch: 1 branch
| Branch | True | False | Total |
| from = (r->flags & TKR_F_HAS_START) ? r->start.sec : tkr_now_sec(aTHX); |
1524 |
0 |
1524 |
|
| 86 |
1524 |
| tkr_walk_init(aTHX_ &w, r, tkr_wall_of(aTHX_ r, from, NULL) - tkr_lookback(aTHX_ r, from), (r->flags & TKR_F_HAS_START) != 0); |
| 87 |
35002 |
▶ | while (i < n && (rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < n && (rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT) { |
33552 |
1450 |
35002 |
| while (i < n && (rc = tkr_walk_next(aTHX_ &w, &got)) == TKR_W_GOT) { |
33478 |
73 |
33551 |
|
| 88 |
33478 |
▶ | if ((r->flags & TKR_F_CRON) && got <= from) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((r->flags & TKR_F_CRON) && got <= from) |
2 |
33476 |
33478 |
| if ((r->flags & TKR_F_CRON) && got <= from) |
0 |
2 |
2 |
|
| 89 |
0 |
| continue; |
| 90 |
33478 |
▶ | XPUSHs(sv_2mortal(tkr_occurrence_sv(aTHX_ r, got, what))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(tkr_occurrence_sv(aTHX_ r, got, what))); |
0 |
33478 |
33478 |
|
| 91 |
33478 |
| i++; |
| 92 |
|
| } |
| 93 |
1523 |
▶ | if (i < n) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < n) |
73 |
1450 |
1523 |
|
| 94 |
73 |
| tkr_rc_croak(aTHX_ rc, r, w.fail_wall, what); |
| 95 |
1505 |
| tkr_walk_free(&w); |
| 96 |
|
| |
| 97 |
|
| IV |
| 98 |
|
| contains(self, t) |
| 99 |
|
| SV *self |
| 100 |
|
| SV *t |
| 101 |
|
| PREINIT: |
| 102 |
2009 |
| const char *what = "Time::Keeper::Recurrence->contains"; |
| 103 |
|
| const tkr_rule *r; |
| 104 |
|
| time_keeper_instant in; |
| 105 |
2009 |
▶ | int64_t got, detail = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int64_t got, detail = 0; |
2009 |
0 |
2009 |
|
| 106 |
|
| int rc; |
| 107 |
|
| CODE: |
| 108 |
2009 |
| r = tkr_rule_of(aTHX_ self, what); |
| 109 |
2009 |
▶ | if ((tkr_tk(aTHX)->from_sv)(aTHX_ t, &in, NULL, NULL) != TIME_KEEPER_OK) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((tkr_tk(aTHX)->from_sv)(aTHX_ t, &in, NULL, NULL) != TIME_KEEPER_OK) |
0 |
2009 |
2009 |
|
| 110 |
0 |
| croak("%s: a Time::Keeper value is wanted", what); |
| 111 |
2009 |
▶ | if (r->flags & TKR_F_EVERY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->flags & TKR_F_EVERY) |
0 |
2009 |
2009 |
|
| 112 |
0 |
| croak("%s: an @every cron has no occurrences of its own, only the next after a moment", what); |
| 113 |
2009 |
| rc = tkr_next_after(aTHX_ r, in.sec, 1, &got, &detail); |
| 114 |
2009 |
| tkr_rc_croak(aTHX_ rc, r, detail, what); |
| 115 |
2009 |
▶ | RETVAL = rc == TKR_W_GOT && got == in.sec && in.nsec == tkr_occurrence_nsec(r); |
Branch: 4 branches
| Branch | True | False | Total |
| RETVAL = rc == TKR_W_GOT && got == in.sec && in.nsec == tkr_occurrence_nsec(r); |
2008 |
1 |
2009 |
| RETVAL = rc == TKR_W_GOT && got == in.sec && in.nsec == tkr_occurrence_nsec(r); |
2005 |
3 |
2008 |
| RETVAL = rc == TKR_W_GOT && got == in.sec && in.nsec == tkr_occurrence_nsec(r); |
2003 |
2 |
2005 |
| RETVAL = rc == TKR_W_GOT && got == in.sec && in.nsec == tkr_occurrence_nsec(r); |
1996 |
13 |
2009 |
|
| 116 |
|
| OUTPUT: |
| 117 |
|
| RETVAL |
| 118 |
|
| |
| 119 |
|
| void |
| 120 |
|
| _iterate_step(...) |
| 121 |
|
| PREINIT: |
| 122 |
|
| tkr_iter *it; |
| 123 |
|
| int64_t got; |
| 124 |
|
| int rc; |
| 125 |
|
| PPCODE: |
| 126 |
|
| PERL_UNUSED_VAR(items); |
| 127 |
2002 |
| it = tkr_iter_of(aTHX_ cv, "Time::Keeper::Recurrence->iterate"); |
| 128 |
2002 |
| it->w.r = &it->w.rule; |
| 129 |
|
| for (;;) { |
| 130 |
2003 |
| rc = tkr_walk_next(aTHX_ &it->w, &got); |
| 131 |
2003 |
▶ | if (rc != TKR_W_GOT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != TKR_W_GOT) |
0 |
2003 |
2003 |
|
| 132 |
0 |
| break; |
| 133 |
2003 |
▶ | if (got > it->from || (it->inclusive && got == it->from)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (got > it->from || (it->inclusive && got == it->from)) |
334 |
1669 |
2003 |
| if (got > it->from || (it->inclusive && got == it->from)) |
333 |
1 |
334 |
| if (got > it->from || (it->inclusive && got == it->from)) |
0 |
333 |
333 |
|
| 134 |
|
| break; |
| 135 |
|
| } |
| 136 |
2002 |
| tkr_rc_croak(aTHX_ rc, &it->w.rule, it->w.fail_wall, "Time::Keeper::Recurrence->iterate"); |
| 137 |
2002 |
▶ | if (rc == TKR_W_GOT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == TKR_W_GOT) |
2002 |
0 |
2002 |
|
| 138 |
2002 |
▶ | XPUSHs(sv_2mortal(tkr_occurrence_sv(aTHX_ &it->w.rule, got, "Time::Keeper::Recurrence->iterate"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(tkr_occurrence_sv(aTHX_ &it->w.rule, got, "Time::Keeper::Recurrence->iterate"))); |
0 |
2002 |
2002 |
|
| 139 |
|
| else |
| 140 |
0 |
▶ | XPUSHs(&PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(&PL_sv_undef); |
0 |
0 |
0 |
|
| 141 |
|
| |
| 142 |
|
| SV * |
| 143 |
|
| iterate(self, ...) |
| 144 |
|
| SV *self |
| 145 |
|
| PREINIT: |
| 146 |
335 |
| const char *what = "Time::Keeper::Recurrence->iterate"; |
| 147 |
|
| const tkr_rule *r; |
| 148 |
|
| tkr_iter it; |
| 149 |
|
| CV *cv; |
| 150 |
|
| SV *state; |
| 151 |
|
| CODE: |
| 152 |
335 |
| r = tkr_rule_of(aTHX_ self, what); |
| 153 |
335 |
▶ | if (r->flags & TKR_F_EVERY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->flags & TKR_F_EVERY) |
0 |
335 |
335 |
|
| 154 |
0 |
| croak("%s: an @every cron has no occurrences of its own, only the next after a moment", what); |
| 155 |
335 |
| memset(&it, 0, sizeof it); |
| 156 |
335 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
1 |
334 |
335 |
|
| 157 |
1 |
| it.from = tkr_instant_arg(aTHX_ ST(1), what); |
| 158 |
1 |
| it.inclusive = 0; |
| 159 |
|
| } |
| 160 |
334 |
▶ | else if (r->flags & TKR_F_HAS_START) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (r->flags & TKR_F_HAS_START) { |
334 |
0 |
334 |
|
| 161 |
334 |
| it.from = r->start.sec; |
| 162 |
334 |
| it.inclusive = !(r->flags & TKR_F_CRON); |
| 163 |
|
| } |
| 164 |
|
| else { |
| 165 |
0 |
| it.from = tkr_now_sec(aTHX); |
| 166 |
0 |
| it.inclusive = 0; |
| 167 |
|
| } |
| 168 |
335 |
▶ | tkr_walk_init(aTHX_ &it.w, r, tkr_wall_of(aTHX_ r, it.from, NULL) - tkr_lookback(aTHX_ r, it.from), items == 1 && (r->flags & TKR_F_HAS_START)); |
Branch: 2 branches
| Branch | True | False | Total |
| tkr_walk_init(aTHX_ &it.w, r, tkr_wall_of(aTHX_ r, it.from, NULL) - tkr_lookback(aTHX_ r, it.from), items == 1 && (r->flags & TKR_F_HAS_START)); |
334 |
1 |
335 |
| tkr_walk_init(aTHX_ &it.w, r, tkr_wall_of(aTHX_ r, it.from, NULL) - tkr_lookback(aTHX_ r, it.from), items == 1 && (r->flags & TKR_F_HAS_START)); |
334 |
0 |
334 |
|
| 169 |
335 |
| cv = newXS(NULL, XS_Time__Keeper__Recurrence__iterate_step, __FILE__); |
| 170 |
335 |
| state = newSVpvn((const char *)&it, sizeof it); |
| 171 |
335 |
| sv_magicext((SV *)cv, state, PERL_MAGIC_ext, &tkr_iter_vtbl, NULL, 0); |
| 172 |
335 |
| SvREFCNT_dec(state); |
| 173 |
335 |
| RETVAL = newRV_noinc((SV *)cv); |
| 174 |
|
| OUTPUT: |
| 175 |
|
| RETVAL |