Skip to content

Commit

Permalink
buffered pattern: implement generic handler
Browse files Browse the repository at this point in the history
Implemented our two buffered patterns so far (days and months).
Because of the generic design of the code, I can add more by just
creating new arrays in xlseq.h, which should be fun.
  • Loading branch information
ejv2 committed Jul 31, 2022
1 parent 765b303 commit ae1f421
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
47 changes: 47 additions & 0 deletions patt.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,50 @@ buffered_pattern_match(const wchar_t rune, struct buffered_matcher_state *state,

return 0;
}

void
buffered_pattern_run(union sample_space samples, int count,
const struct long_short *dataset, size_t datalen)
{
int i;
const wchar_t *out;
const char *work = samples.ordered.last;
const struct long_short *cur;
int uselong;
int rlen, rind = 0;
unsigned int dind = 0;
int arglen = strlen(samples.ordered.last);
wchar_t decode[arglen];

memset(decode, 0, sizeof(wchar_t) * arglen);
do {
rlen = mbtowc(decode + rind, work, arglen);
if (rlen < 0) {
fprintf(stderr, "xlseq: invalid text encoding\n");
return;
}
work += rlen;
rind++;
} while (*work);
decode[rind] = 0;

for (i = 0; i < datalen; i++) {
cur = &dataset[i];
if (wcscmp(cur->l, decode) == 0) {
uselong = 1;
break;
} else if (wcscmp(cur->s, decode) == 0) {
uselong = 0;
break;
}
}
dind = i;
if (count <= 0)
count = datalen - 1 - (cur - dataset);

for (i = 1; i <= count; i++) {
out = (uselong) ? dataset[(dind + i) % datalen].l :
dataset[(dind + i) % datalen].s;
printf("%ls ", out);
}
}
4 changes: 4 additions & 0 deletions xlseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ run_pattern(PatternType pat, int count, int startind, union sample_space samples
number_pattern_run(samples, count, startind);
break;
case DaysPattern:
buffered_pattern_run(samples, count, days, LENGTH(days));
break;
case MonthsPattern:
buffered_pattern_run(samples, count, months, LENGTH(months));
break;
case UnrecognisedPattern:
break;
default:
Expand Down
3 changes: 3 additions & 0 deletions xlseq.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ int buffered_pattern_match(const wchar_t rune,
struct buffered_matcher_state *state,
const struct long_short *dataset,
size_t datalen);
void buffered_pattern_run(union sample_space samples, int count,
const struct long_short *dataset,
size_t datalen);

/* string pattern */
struct string_pattern_state {
Expand Down

0 comments on commit ae1f421

Please sign in to comment.