I am nothing, no one, nobody, no more

Get out of life alive le blog de SnippyHolloW.

Bits of code

int function(void) {
    static int i, state = 0;
    switch (state) {
        case 0: goto LABEL0;
        case 1: goto LABEL1;
    }
    LABEL0: /* start of function */
    for (i = 0; i < 10; i++) {
        state = 1; /* so we will come back to LABEL1 */
        return i;
        LABEL1:; /* resume control straight after the return */
    }
}

(coroutine explanation)

/* match: search for regexp anywhere in text */
int match(char *regexp, char *text)
{
    if (regexp[0] == ‘^’)
        return matchhere(regexp+1, text);
    do {    /* must look even if string is empty */
        if (matchhere(regexp, text))
            return 1;
    } while (*text++ != ”);
    return 0;
}

/* matchhere: search for regexp at beginning of text */
int matchhere(char *regexp, char *text)
{
    if (regexp[0] == ”)
        return 1;
    if (regexp[1] == ‘*’)
        return matchstar(regexp[0], regexp+2, text);

    if (regexp[0] == ‘$’ && regexp[1] == ”)
        return *text == ”;
    if (*text!=” && (regexp[0]==‘.’ || regexp[0]==*text))
        return matchhere(regexp+1, text+1);
    return 0;
}

/* matchstar: search for c*regexp at beginning of text */
int matchstar(int c, char *regexp, char *text)
{
    do {   /* a * matches zero or more instances */
        if (matchhere(regexp, text))
            return 1;
    } while (*text != ” && (*text++ == c || c == ‘.’));
    return 0;
}

Brian Kernighan’s first chapter of Beautiful Code