Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdlib.h>
- void dispose_matchdata(char **matchdata) {
- if (!matchdata) return;
- for (char **phrase = matchdata; *phrase; ++phrase)
- free(*phrase);
- free(matchdata);
- }
- char **match(char const *instance, char const *pattern) {
- char **retval;
- char **currdata;
- int astcount = 0;
- char const *lastast = 0;
- int cdatsize = 0;
- int cdatcap = 0;
- for (char const *astchk = pattern; *astchk; ++astchk)
- if (*astchk == '*') ++astcount;
- retval = calloc(astcount + 1, sizeof(char *));
- currdata = retval - 1;
- while (isspace(*instance)) ++instance;
- while (isspace(*pattern)) ++pattern;
- while (*instance && *pattern) {
- if (lastast != pattern && *pattern == '*') {
- lastast = pattern;
- ++currdata;
- cdatsize = 0;
- cdatcap = 16;
- *currdata = malloc(cdatcap);
- }
- if (isspace(*instance) && isspace(*pattern)) {
- do { ++instance; } while (isspace(*instance));
- do { ++pattern; } while (isspace(*pattern));
- } else if (isspace(*instance) && *pattern == '*') {
- do { ++instance; } while (isspace(*instance));
- } else if (isspace(*instance) || isspace(*pattern)) {
- dispose_matchdata(retval);
- return 0;
- } else if (pattern == lastast) {
- int done = 0;
- char const *next_nonws = pattern;
- do { ++next_nonws; }
- while (isspace(*next_nonws));
- while (!done) {
- int break_outer = 0;
- do {
- if (*next_nonws && !*instance) {
- dispose_matchdata(retval);
- return 0;
- }
- if (isspace(*instance)
- && *next_nonws == '*') {
- break_outer = 1;
- break;
- }
- (*currdata)[cdatsize] = *instance;
- ++cdatsize;
- if (cdatsize >= cdatcap) {
- cdatcap *= 2;
- *currdata = realloc(*currdata,
- cdatcap);
- }
- do { ++instance; }
- while (isspace(*instance)
- && isspace(instance[1]));
- } while (*instance != *next_nonws);
- if (break_outer) break;
- done = 1;
- char const *ichk = instance;
- char const *pchk = pattern + 1;
- while (isspace(*ichk)) ++ichk;
- while (isspace(*pchk)) ++pchk;
- while (*ichk && *pchk && *pchk != '*') {
- if (isspace(*ichk) && isspace(*pchk)) {
- do { ++ichk; } while (isspace(*ichk));
- do { ++pchk; } while (isspace(*pchk));
- } else if (*ichk != *pchk) {
- done = 0;
- break;
- } else {
- ++ichk;
- ++pchk;
- }
- }
- if (done) {
- while (isspace(*ichk)) ++ichk;
- while (isspace(*pchk)) ++pchk;
- if (!*ichk != !*pchk) {
- done = 0;
- }
- }
- }
- (*currdata)[cdatsize] = 0;
- int i = cdatsize - 1;
- while (isspace((*currdata)[i]) && i > 0) --i;
- cdatcap = i + 2;
- *currdata = realloc(*currdata, cdatcap);
- (*currdata)[i + 1] = 0;
- do { ++pattern; } while (isspace(*pattern));
- } else if (*instance != *pattern) {
- dispose_matchdata(retval);
- return 0;
- } else {
- ++instance;
- ++pattern;
- }
- }
- while (isspace(*instance)) ++instance;
- while (isspace(*pattern)) ++pattern;
- if (*instance || *pattern) {
- dispose_matchdata(retval);
- return 0;
- } else {
- return retval;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement