Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Beautiful Code

Name: Anonymous 2008-10-02 19:21

Sup /prog/-riders
In this thread we post beautiful code. They can be from your job, school, whatever.

Here's a recursive regex parser with support for character classes, $ and & anchors, and the . and * metachars.


int matchhere(char* regexp, char* text);
int matchstar(int c, char* regexp, char* text)
{
    do {
        if (matchhere(regexp, text))
            return 1;
    } while (*text != '\0' && (*text++ == c || c == '.'));
    return 0;
}

int matchhere(char* regexp, char* text)
{
    if (regexp[0] == '\0')
        return 1;
    if (regexp[1] == '*')
        return matchstar(regexp[0], regexp+2, text);
    if (regexp[0] == '$' && regexp[1] == '\0')
        return *text == '\0';
    if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
        return matchhere(regexp+1, text+1);
    return 0;
}

int match(char* regexp, char* text)
{
    if (regexp[0] == '^')
        return matchhere(regexp+1, text);
    do {
        if (matchhere(regexp, text))
            return 1;
    } while (*text++ != '\0');
    return 0;
}

Name: Anonymous 2008-10-02 20:38

>>1
Doesn't support char classes, or at least I don't see a ']' or '[' anywhere.

Core of comm:

  res=memcmp(l1.data,l2.data,(l1.used < l2.used)?l1.used+1:l2.used+1);
  if(!res) { /* line in both files */
   if(cols&4 && l1.used) {
    if(cols&2) putchar('\t');
    if(cols&1) putchar('\t');
    fwrite(l1.data,1,l1.used,stdout);
   }
   need=3;
  } else if(res<0) { /* line only in file1 */
   if(cols&1)
    fwrite(l1.data,1,l1.used,stdout);
   need=1;
  } else { /* line only in file2 */
   if(cols&2) {
    if(cols&1) putchar('\t');
    fwrite(l2.data,1,l2.used,stdout);
   }
   need=2;
  }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List