but...but...but..this way I can strcmp the buffer with a finite set of strings, determined at compile time, with the efficiency of a single strcmp!
for example, if the strings were: hi me how are you
I could do:
#define NOTHING -1
#define HI 2
#define ME 4
#define HOW 7
#define ARE 11
#define YOU 16
char *start = buffer;
char *position;
int state = 0;
int token = NOTHING;
for(;;) {
switch(*position) {
case 'h': switch(state) { case 0: state = 1; break;
default: state = 0; break;
}
case 'i': switch(state) { case 1: state = 2; break;
default: state = 0; break;
}
case 'm': switch(state) { case 0: state = 3; break;
default: state = 0; break;
}
case 'e': switch(state) { case 3: state = 4; break;
default: state = 0; break;
}
case 'h': switch(state) { case 0: state = 5; break;
default: state = 0; break;
}
case 'o': switch(state) { case 5: state = 6; break;
default: state = 0; break;
}
case 'w': switch(state) { case 6: state = 7; break;
default: state = 0; break;
}
case 'a': switch(state) { case 8: state = 9; break;
default: state = 0; break;
}
case 'r': switch(state) { case 9: state = 10; break;
default: state = 0; break;
}
case 'e': switch(state) { case 10: state = 11; break;
default: state = 0; break;
}
case 'y': switch(state) { case 0: state = 12; break;
default: state = 0; break;
}
case 'o': switch(state) { case 13: state = 14; break;
default: state = 0; break;
}
case 'u': switch(state) { case 15: state = 16; break;
default: state = 0; break;
}
case '\0': state = NOTHING; break;
}
switch(state) {
case NOTHING: return NULL; break;
case HI:
case ME:
case HOW:
case ARE:
case YOU:
return state;
break;
}
}
And this then reduces to:
#define NOTHING -1
#define HI 2
#define ME 4
#define HOW 7
#define ARE 11
#define YOU 16
char *start = buffer;
char *position;
int state = 0;
int token = NOTHING;
for(;;) {
switch(*position) {
case 'h': switch(state) { case 0: state = 1; break;
default: state = 0; break;
}
case 'i': switch(state) { case 1: state = 2; break;
default: state = 0; break;
}
case 'm': switch(state) { case 0: state = 3; break;
default: state = 0; break;
}
case 'e': switch(state) { case 3: state = 4; break;
case 10: state = 11; break;
default: state = 0; break;
}
case 'o': switch(state) { case 1: state = 6; break;
case 13: state = 14; break;
default: state = 0; break;
}
case 'w': switch(state) { case 6: state = 7; break;
default: state = 0; break;
}
case 'a': switch(state) { case 8: state = 9; break;
default: state = 0; break;
}
case 'r': switch(state) { case 9: state = 10; break;
default: state = 0; break;
}
case 'y': switch(state) { case 0: state = 12; break;
default: state = 0; break;
}
case 'u': switch(state) { case 15: state = 16; break;
default: state = 0; break;
}
case '\0': state = NOTHING; break;
}
switch(state) {
case NOTHING: return NULL; break;
case HI:
case ME:
case HOW:
case ARE:
case YOU:
return state;
break;
}
}
Name:
Anonymous2011-11-06 15:34
>>19
Using strcmp() would make it into linear time you fucking idiot.
>>19 is a fucking stupid shit. Most, if not all C compilers can optimize a switch/case statement to a jump table. This in turn makes it constant time. However using strcmp() will result in linear time.
Name:
Anonymous2011-11-06 15:46
>>20 Nigger doesn't realize >>17-san was quoting >>8
also you just gotta love how jewboy number >>20-23 is desperately trying to make it look as if several people were hating on >>19-san
good god the autism in this thread is hilarious
Name:
Anonymous2011-11-06 15:49
>>24
I'm quoting the ANSI/ISO C standards you fucking uneducated bitch.
Name:
Anonymous2011-11-06 15:55
>>25 look at me i'm trying to cope with my autistic rage by using strong words! fear me!
>>24
Autism is a very serious disorder. It most certainly is not under any circumstances `hilarious'! Many of these people are most likely unable to live a normal life outside of /prog/ due to their tragic condition.
Name:
Anonymous2011-11-06 16:01
>>21
this switches by an unitialized *position in an infite loop,
srsly is that what it was supposed to do?
my C99 is rusty btw >>25
yup. which great job, these 2 clock cycles you gained day-um,not really worth all the typing, linear ain't that bad, OP doesn't even have moar than 10 elements anywheere
Name:
Anonymous2011-11-06 17:34
i fucking love \prog\
Name:
Anonymous2011-11-06 18:20
the /prog/ theorem
every normal iteration can be replaced by a cluster fuck of pointless switch case statements
Name:
Anonymous2011-11-06 18:59
>>26
Go scrub another toilet you fucking bitch. That is all will ever amount to you mental midget you.
yup. which great job, these 2 clock cycles you gained day-um,not really worth all the typing, linear ain't that bad, OP doesn't even have moar than 10 elements anywheere
It's more than that you idiot. If you take the disjoint union of the case statements over some f: X -> T... never mind. I don't think you have the mental capacity understand anything beyond googleable C.
Name:
Anonymous2011-11-06 19:42
>>32
ever considered why noone uses relativity theory on those basic Problems like:
...how much time will it take lolcat A to cross the road at velocity V...
None should ever be encouraged to use this rocket science math just to compare 2 strings lol,(and btw the compiler can prolly optimize it)
>>33
Oh aren't you cool. You can lol on the internet. With that....
You aren't comparing strings you fucking jew. You're comparing each character in an array of characters. This is because a string in C is an array of characters that terminates with '\0'.
So if you compare something like "xle" in C, you would be doing a minimum of 4 comparisions. Now depending on how the if/else in structured, the number of comparisions could actually increase exponentially.
And just for the record you little fucking annoying stupid shit jew, the compiler can optimize it. However, you will still be stuck with 4 comparisons. Can you tell us why? I bet not.
Now go scrub another toilet you fucking mental midget.
the code is incomplete, and I forgot to increment le pointer. Outside of the loop, there would be some line buffered reading, or maybe just fixed sized buffering, and then the dfa would loop over the buffer, returning tokens that have sent it into an accept state. Normal people never hand code their dfas, and instead use tools like lex/flex to generate the code from a list of regular expressions to match. It isn't difficult to algorithmically generate such a dfa using the fewest amount of possible states, so there is no advantage to hand coding a dfa, but there is autistic enjoyment to be had, much like playing with legos.