C is not Python
I am porting pyspf to C (long story, and I am stupid for trying). But of course, C is not python.
So you don't have anything nearly as nice as re.compile("whatever").split("somestring")
.
What is that good for, you may ask? Well, to do things like splitting email addresses while validating them, or in this specific case, to validate SPF mechanisms (nevermind what those are).
But hey, you can always do this (excuse me while I weep a little):
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) { break; } 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); ptr=ptr+pmatch[0].rm_eo; } regfree(&re); bcatblk (tmp,ptr,strlen(string)-(ptr-string)); struct bstrList *l= bsplit(tmp,0); return l; }
And that is probably wrong for some cases (and it doesn't split the exact same way as Python, but that's what unit testing is for).
I must be missing something that makes regcomp & friends nicer to use. Right? Right?
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
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.
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.