[dev] [sic] tok and ctok functions

From: anonymous <aim0shei_AT_lavabit.com>
Date: Thu, 15 Apr 2010 02:57:59 +0400

I have written the following function:

static char*
skip(char *s, char c) {
        while(*s != c && *s != '\0')
                s++;
        if (*s != '\0')
                *s++ = '\0';
        return s;
}

Then I can rewrite `ctok` like this:

static char*
ctok(char **s, int c) {
        char *p;
 
        p = *s;
        *s = skip(p, c);
        return p;
}

This means every line that looks like

    p = ctok(&s, c);

can be rewritten as

    p = s;
    s = skip(p, c);

Then we can rewrite these lines (117-119 in sic.c):

    txt = ctok(&msg, '\r');
    msg = ctok(&txt, ':');

as

    txt = msg;
    msg = skip(txt, '\r');
    msg = txt;
    txt = skip(msg, ':');

and then as

    skip(msg, '\r');
    txt = skip(msg, ':');

We can't remove skip(msg, '\r') because it has side effect of
replacing '\r' with '\0'.

First of all, maybe I did something wrong, but I did this with my copy
and can see no regression. If everything I did was correct then it
means ctok and tok functions are really confusing so even their
authors can't understand it and use properly (or do you think that
using parsing function for replacing last character is proper use?).
I will try to rewrite sic using my `skip` function. It don't operate
with pointers to pointers and do less operations so I think it is
easier to understand what it is doing.

Also sometimes sic.c uses tok(&s) function instead of ctok(&s, ' ')
even if RFC says there should be only one space character.
Received on Wed Apr 14 2010 - 22:57:59 UTC

This archive was generated by hypermail 2.2.0 : Wed Apr 14 2010 - 23:36:01 UTC