Skip to content

Commit

Permalink
string pattern: simplify routine
Browse files Browse the repository at this point in the history
  • Loading branch information
ejv2 committed Jul 24, 2022
1 parent 8d845fc commit 6667e30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
34 changes: 16 additions & 18 deletions patt.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,39 @@ string_pattern_match(const wchar_t rune)
}

void
string_pattern_run(struct string_pattern_state *state, union sample_space samples, int count)
string_pattern_run(union sample_space samples, int count)
{
int i;
long suffix;
size_t ind;
const char *cend;
char *endptr, *buf;
const char *walk;

if (!state->common_end && !state->common_check) {
for (walk = samples.ordered.last; *walk; walk++) {
ind = walk-samples.ordered.last;
if (!samples.ordered.middle[ind])
break;
else if (samples.ordered.middle[ind] != *walk) {
break;
}

state->common_end = walk;
for (walk = samples.ordered.last; *walk; walk++) {
ind = walk-samples.ordered.last;
if (!samples.ordered.middle[ind])
break;
else if (samples.ordered.middle[ind] != *walk) {
break;
}
state->common_check = 1;

cend = walk;
}

if (state->common_end && *state->common_end) {
state->common_end++;
suffix = strtol(state->common_end, &endptr, 10);
buf = calloc(sizeof(char), state->common_end - samples.ordered.last + 1);
if (cend && *cend) {
cend++;
suffix = strtol(cend, &endptr, 10);
buf = calloc(sizeof(char), cend - samples.ordered.last + 1);
if (!buf) {
perror("buf allocation");
return;
}
strncpy(buf, samples.ordered.last, state->common_end - samples.ordered.last);
strncpy(buf, samples.ordered.last, cend - samples.ordered.last);

for (int i = 0; i <= count; i++) {
if (suffix) {
printf("%s%ld%s ", buf, ++suffix, endptr);
printf("%s%ld%s ", buf, suffix++, endptr);
} else {
printf("%s%s ", buf, endptr);
}
Expand Down
8 changes: 1 addition & 7 deletions xlseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ struct matcher_state {
struct buffered_matcher_state days;
struct buffered_matcher_state months;
};
struct pattern_state {
struct string_pattern_state string;
};

char *argv0;

Expand Down Expand Up @@ -119,13 +116,10 @@ type_detect(const char *first, const char *second)
int
run_pattern(PatternType pat, int count, union sample_space samples)
{
struct pattern_state state;

memset(&state, 0, sizeof(state));
switch (pat) {
case StringPattern:
MUST_BOUNDED(count);
string_pattern_run(&state.string, samples, count);
string_pattern_run(samples, count);
break;
case NumberPattern:
MUST_BOUNDED(count);
Expand Down
2 changes: 1 addition & 1 deletion xlseq.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct string_pattern_state {
const char *common_end; /* NULL if no common section discovered */
};
int string_pattern_match(const wchar_t rune);
void string_pattern_run(struct string_pattern_state *state, union sample_space samples, int count);
void string_pattern_run(union sample_space samples, int count);

/* number pattern */
int number_pattern_match(const wchar_t rune);
Expand Down

0 comments on commit 6667e30

Please sign in to comment.