I though it would be cool to implement a few cool perl functions in c. I've tired this a few times before and was unsuccessful. Any way, today I have split, join, chop, chomp done. They are minimal so they might not do all that their perl counter parts do. My next one I want to do is subsitute or =~ s/ in perl but my brain is failing me on ways to replace the strings. If anyone has any ideas post em up. In the main time here is what I have started, corrections and contributions of course are appreciated:
char *string = "This is the originial string";
char test1[] = "A|B|C|D|E|F|G", *test2, **list;
char test3[] = "This is the string to be chopped";
char test4[] = "This string has a newline at the end ";
test4[strlen(test4)-1] = '\n';
puts(test1);
list = split("|", test1);
test2 = join(":", list);
puts(test2);
Btw, duplicating the s/// operator is going to be a real challenge.. I've given it extensive thought, and am afraid to even begin such a thing.. (^=
[ 08 September 2001: Message edited by: TheLinuxDuck ]
debiandude
09-09-2001, 12:08 AM
Well the implementation dosn't have to be copmlete, and personally I don't think I proabably would be able to make it complete because they're some option of s/// that I don't even know how to use. I just wanted to do a few basic ones because their have been quite a few times when I have said to myself, geeze it wish I could do =~ s/(\w+)/<$1>/g; and maybe if I get around do all that stuff I won't have to dream.. And don't tell me to use the perl regular expression for c library, how would I learn if i just did that :-)
[ 09 September 2001: Message edited by: debiandude ]
TheLinuxDuck
09-09-2001, 12:18 AM
Originally posted by debiandude:
<STRONG>Well the implementation dosn't have to be copmlete, and personally I don't think I proabably would be able to make it complete because they're some option of s/// that I don't even know how to use. I just wanted to do a few basic ones because their have been quite a few times when I have said to myself, geeze it wish I could do =~ s/(\w+)/<$1>/g; and maybe if I get around do all that stuff I won't have to dream.. </STRONG>
I'm totally with you.. I'd love to be able to do some of the s/// stuff in C, with some ease.. I don't know diddly about the existing regexp stuff.. all I know is it's not easy to use.. I've not been able to find any examples of using it, let alone being able to use it.. (^=
Maybe you and I could put our heads together and build up a C version that compares to the perl one.. I don't know how close we could get, but man that would be awesome to try! (^=
<STRONG>
And don't tell me to use the perl regular expression for c library, how would I learn if i just did that :-)
</STRONG>
I completely understand. It's things like this that help us to learn tricks and techniques to improve our abilities....
debiandude
09-09-2001, 12:35 AM
Wow, absolutly, I would be honored for you to help me with this. I guess we could set up something with sourceforge, I havn't done anything with them yet but they have so many projects their it can't be that difficult.
Anyway, I guess we should start by figuring what we want to start with. I was going to do the \w, \d, \s and +, next. Here is the subsitute code so far. I am going to need to get rid of using the split function in the sub function also. :-)
char *subsitute(char *src, char *pattern) {
Okay last nite I sat for a very long time and decided I was I was going to need to do.
The first step would be to create a string parses, that account for all the different possibilities is the s/// function.
Also we need to take in account precedence, which goes parentheses, multipliers, seqences and anchors, and last alternation.
So we create the string parser, and then we have to create each of the individual subs which account for the operators. Now each function needs to have a pointer to the intatance of the operator in the array, and with some of the operators, namely parens, we need to have a find end parsers to, which will then process the contents of the parens (prolly just a loop).
I applied for a prject on sourceforge called cregex and I am currently waiting for approval. If anyone else is interested post here, and maybe we could get some stuff started. Thanks for you time!
debiandude
09-09-2001, 03:16 PM
Okay here is a sample expression parser, which dosn't really do anything I just want to check to see if I am on the right track:
eval_seq_anch();
while((op = *token) == '|') {
get_token();
eval_seq_anch(tmp);
if(op == '|') {
/* do what needs to be done ;-) */
}
}
}
void eval_seq_anch() {
register char op;
char tmp[80];
eval_multipliers();
while((op = *token) == '^' || op == '$') {
get_token();
eval_multiplers(tmp);
switch(op) {
/* Case for each of the operators */
}
}
}
void eval_multipliers() {
char tmp[80];
register int t;
eval_parenthese();
if(op = *token) == '?' || op == '+' || op == '*) {
get_token();
eval_parenthese(tmp);
switch(op) {
/* Case for each of the operators */
}
}
}