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 23:47

>>11
I'd like to see you write a more compact and elegant regex parser in C, using no external libraries.

Name: Anonymous 2008-10-03 0:13

>>15
In my experience, ^, $, . and * is enough to cover 99% of use cases. Besides, the purpose wasn't to demonstrate anything usable, just something which is clear, concise and elegant. If you can't see how a recursive regex parser in 21 C statements doesn't meet this criteria, then just sage and begone.
Otherwise, I eagerly await your contribution to this thread, if you already haven't done so.

Name: Anonymous 2008-10-03 0:27

>>17
No, that makes you a defensive coward.
You seem like the type of person who is reluctant to participate in anything, due to an irrational fear of being hurt. What are you afraid of, what are you running away from?
Yourself.

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