|
|
|
|
Posted 2007-07-25, 11:01 PM
in reply to WetWired's post starting "Or, you could simply put the loops in..."
|
|
|
|
WetWired said:
Or, you could simply put the loops in an inline function and return from it.
All this structure, of course, breaks down to gotos at the machine code level, anyway.
Of course, you can write spaghetti code without gotos, too:
Code:
unsigned uState=0;
while(1){
switch(uState){
case 0:
//stuff
uState=2;
break;
case 1:
//other stuff
break;
case 2:
//land here
uState=1;
break;
}
} |
This is actually a construct commonly used in my line of work 0_0
|
I think a "break 2;" is more elegant than an inline function but eh.
Actually, I've used that structure before too...here's a shitty html parser I wrote some time ago:
Code:
void HTMLParser::Parse(std::string code)
{
std::string::iterator oldp, curp;
int stage;
Element elem;
oldp = curp = code.begin();
stage = 0;
while(curp != code.end()) {
switch(stage) {
case 0:
if(*curp == '<') {
elem.type = Element::TEXT;
elem.data.assign(oldp, curp);
elements.push_back(elem);
oldp = curp + 1;
stage = 1;
}
break;
case 1:
if(*curp == '/') {
elem.type = Element::CLOSINGTAG;
oldp++;
stage = 2;
break;
} else
elem.type = Element::TAG;
// Fall through
case 2:
if(!IsAlphanumeric(*curp)) {
elem.data.assign(oldp, curp);
elem.data = ToLower(elem.data);
curp = BuildParamArray(code, curp, elem.params);
elements.push_back(elem);
stage = 3;
continue;
}
break;
case 3:
if(*curp == '>') {
oldp = curp + 1;
stage = 0;
}
break;
}
curp++;
}
if(oldp < curp) {
elem.type = Element::TEXT;
elem.data.assign(oldp, curp);
elements.push_back(elem);
}
} |
|
|
|
|
|
|
|
|
|
|
|