Considering we want to code something like below:
char ch[20];
scanf("%s", ch);
switch(string){
case "hello":
printf("Hi!\n");
char name[20];
scanf("%s", name);
switch(name){
case "Kevin":
printf("Nice to meet you!\n");
default:
printf("But my name is \"Kevin\"!!!");
}
default:
printf("Not Working\n");
case "bye":
printf("The end\n");
break;
case "err":
printf("Error");
goto default;
case "sth":
printf("What?");
}... we are sure to find out it's impossible to work - things after case should be a constant, while a string is actually a address.
The most common way is to do it like this:
if(strcmp(ch, "hello") == 0){
//...
}else if(strcmp(ch, "bye")){
//...
}else if(strcmp(ch, "err")){
//...
}else{
//...
}However, obviously, a lot of if-else and repeated strcmp are indeed stressful. Also, reusing codes in blocks won't be easy, either. Thus, here comes the match-like block! It looks like below:
match(ch)
like("hello")
printf("Hi\n");
char name[20];
scanf("%s", name);
match(name)
like("Kevin")
printf("Nice to meet you!\n");
break;
other
printf("But my name is Kevin!\n");
endMatch
thenGo("bye");
other
printf("Not working\n");
like("bye")
printf("The end\n");
break;
like("err")
printf("Error\n");
thenGoOther;
like("sth")
printf("What?\n");
endMatchThis demo can be found in autoKeepGoing/demo.c.
- Code reusing (
thenGo()andthenGoOther) - Code reducing (needless to use
strcmp) - Scope safety (codes after each
like()orotheris actually contained by a{ })
The first step is to #include this library.
Just put autoKeepGoing/matchLikeMacro.h under the same directory as main.c or something.
Then, copy & paste this line into it:
#include "matchLikeMacro.h"A match-like block looks like this:
match(switchString)
like("cmd1")
like("cmd2")
other
endMatchIt starts with match(switchString) and ends up with endMatch.
Each case started with like(identifierString) or other. A : must not be provided.
A case will be run when the switchString matchs the identifierString, or after the cases preceding is done.
To escape the block, use break; . To force another case to be run, use thenGo(identifierString); or thenGoOther; .
For those who found a bunch of break; s are useless.
Given a code like this:
char ch[20];
scanf("%s", ch);
switch(ch){
case "hello":
printf("Hi\n");
break;
case "bye":
printf("TheEnd!\n");
break;
case "err":
printf("Weird.\n");
goto default;
break;
default:
printf("notWorking!\n");
goto case "bye";
break;
}... on top of the impossibility of compiling and running, it is tiring to remember to use break;.
Thus, here comes the meet-as block!
meet(ch)
as("hello")
printf("Hi\n");
as("bye")
printf("TheEnd!\n");
as("err")
printf("Weird.\n");
forceMeetRest;
rest
printf("notWorking!\n");
forceMeet("bye");
endMeetHa! Much cleaner, right?
- Code reusing (
forceMeet()andforceMeetRest) - Code reducing (needless to use
strcmpandbreak;) - Scope safety (codes after each
as()orrestis actually contained by a{ })
The first step is to #include this library.
Just put autoBreak/meetAs.h under the same directory as main.c or something.
Then, copy & paste this line into it:
#include "meetAs.h"A meet-as block looks like this:
meet(switchString)
as("cmd1")
as("cmd2")
other
endMeetIt starts with meet(switchString) and ends up with endMeet.
Each case started with as(identifierString) or rest. A : must not be provided.
The rest block must always be at the end, right before endMeet.
A case will be run when the switchString matchs the identifierString, or after the cases preceding is done.
To escape the block, use break; . To force another case to be run, use forceMeet(identifierString); or forceMeetOther; .