Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

C is not Python

I am port­ing pyspf to C (long sto­ry, and I am stupid for try­ing). But of course, C is not python.

So you don't have anything nearly as nice as re.­com­pile("what­ev­er").s­plit("­somestring").

What is that good for, you may ask? Well, to do things like split­ting email ad­dress­es while val­i­dat­ing them, or in this spe­cif­ic case, to val­i­date SPF mech­a­nisms (n­ev­er­mind what those are).

But hey, you can al­ways do this (ex­cuse me while I weep a lit­tle):

struct bstrList *re_split(const char *string, const char *pattern)
    int status;
    regex_t re;
    regmatch_t pmatch[20];

    if (regcomp(&re, pattern, REG_ICASE|REG_EXTENDED) != 0)
        return(0);      /* Report error. */

    bstring tmp=bfromcstr("");
    char *ptr=(char *)string;

    for (;;)
        status = regexec(&re, ptr, (size_t)20, pmatch, 0);
        if (status==REG_NOMATCH)
        bcatblk (tmp,ptr,pmatch[0].rm_so);
        bconchar (tmp,0);
        bcatblk (tmp,ptr+pmatch[0].rm_so,pmatch[0].rm_eo-pmatch[0].rm_so);
        bconchar (tmp,0);

    bcatblk (tmp,ptr,strlen(string)-(ptr-string));
    struct bstrList *l= bsplit(tmp,0);
    return l;

And that is prob­a­bly wrong for some cas­es (and it does­n't split the ex­act same way as Python, but that's what unit test­ing is for).

I must be miss­ing some­thing that makes reg­comp & friends nicer to use. Right? Right?

Justin Haygood / 2007-02-15 17:40:

Have you considered using PCRE for regular expressions? It's fairly small, and should be bundled with KDE anyway, since kjs uses it to power JavaScript's regular expressions

Roberto Alsina / 2007-02-15 20:26:

It's a possibility.

However, it adds another dependency, and it is possible I can do without. But yeah, I am keeping it in mind.

Henry S. / 2007-02-16 01:15:

Well, you have to view C as a low level language...not as a set of usefull functions or libraries because there really aren't any. If you are programming in straight C, you might as well do it in Assembly as far as I'm concerned. :-)

Aas a professional Java programmer, I have always hated programming in C and C++. But programming with Qt is as easy for me as Java because it has all of the usefull stuff that Java has....and I'm not talking about GUI stuff, but things like Map, List, Calendar, and things like that.

I believe QT4 actually seperates the usefull API stuff from the Gui Toolkit, so you could actually write Qt4 commandline applications. That will be so sweet.

Contents © 2000-2020 Roberto Alsina