Using Strings with a Switch
Posted
by Swikar Sugandhi
on August 8th, 2002
Environment:
The following macros help to achieve the switch-case on a string.
////////////////////////////////////////////////////////////// #define STR_SWITCH(str) {\ TCHAR* __ps = (TCHAR*)((const TCHAR*)str);\ while(1) { #define STR_SWITCH_END() break; } } #define STR_CASE(str) if(0 == _tcsicmp(__ps,((const TCHAR*)str))) #define STR_CASE_EXACT(str) if( 0 == _tcscmp( __ps, ((const TCHAR*)str) ) ) #define DEFAULT_CASE() //////////////////////////////////////////////////////////////
How do you use these macros? Go through the following piece of code.
Myclass::Function()
{
CString sSwitch("bbbb");
CString sCase("AAAA");
STR_SWITCH(sSwitch) //Start of switch
{ //Opening and closing braces
//NOT MANDATORY for switch.
STR_CASE(sCase)
{ //Opening and closing braces
//MANDATORY for case.
AfxMessageBox("In first case statement");
break; //break has to in braces of case
} //Opening and closing braces
//MANDATORY for case.
break; //Illegal break -- The code works,
//but the output....?????
sCase = _T("bbbb"); //Statements allowed here ;)
STR_CASE_EXACT(sCase)
{
AfxMessageBox("In second case statement");
break;
}
DEFAULT_CASE()
{
//Default handling if any
break;
}
} //Opening and closing braces
//NOT MANDATORY for switch
STR_SWITCH_END() //MANDATORY statement
}
STR_CASE() -> Perform a lowercase comparison of strings.
STR_CASE_EXACT() -> Compares strings, matches the
case of the characters.
DEFAULT_CASE() -> Default case for the switch.
Note the following requirements:
- The "break" statement has to be inside the scope for the STR_CASE, STR_CASE_EXACT, or DEFAULT_CASE.
- Fall through for case statements is not allowed with this switch case.
- Statements between two cases are allowed. Be careful while modifying the string on which the switch is performed.

Comments
more elegant (IMHO) solution
Posted by Legacy on 09/07/2003 12:00amOriginally posted by: Old Wolf
Replynot recommendable
Posted by Legacy on 05/03/2003 12:00amOriginally posted by: chen3feng
It's not a good idea for either perfanmance or mentainment, I think.
ReplyMACRO, macro is a dangerous thing. try your best to avoid using it.
String switch
Posted by Legacy on 01/10/2003 12:00amOriginally posted by: Peter Briody
At the risk of being hunted down as a c++ poluter, I submit the following routine for string switching which I have been using for years ( mainly because I am too dumb to know any better ).
Here it is as an example:
CString StrExt = „Path.zip“;
LPCSTR lpExt[] = {".zip",".lzh"};
switch( StrMatchPos(strExt,lpExt,sizeof(lpExt)/sizeof(LPCSTR) ))
{
case 0://.zip
AfxMessageBox("zip");
break;
case 1://.lzh
AfxMessageBox("lzh");
break;
default:
AfxMessageBox("other");
break;
}
The initialisation array provides a good view of what you are doing and the routine is only one line longer than usual.
Put th�s following routine somewhere central – in your own utilities-dll maybe
UINT StrMatchPos(LPCSTR lpExp, LPCSTR *lpArray,DWORD dwCount)
{
try
{
UINT n;
CString strTmp,strExp = lpExp;
for ( n = 0; n < dwCount;n++)
{
strTmp = lpArray[n];
if ( strTmp.CompareNoCase(strExp) == 0 )
return n; // match
}// for n
return -1; // no match
}
catch(...){return -1;}// Error}
}
If you want to make life tough for yourself and try to force c++ to behave in ways for whuch it was not designed then don't let me spoil your fun - and dont bother to write write and tell me !
ReplyPerformance note
Posted by Legacy on 10/05/2002 12:00amOriginally posted by: Stepan Orlov
1. I've read carefully nearly all of comments and have found that no one discussed how fast would a code using "string switch" work. The matter is that the a good c/c++ compiler produces code for the switch statement which performs BINARY search to find a matching case. What Swikar Sugandhi offers us works good for a small number of CASEs, but much worse than real switch if there's a lot of CASEs - using these macros will cause a LINEAR search.
2. One should keep in mind that "string switch" has semantics different from the SWITCH operator - I mean the situation when we don't use the BREAK operator after each CASE.
Stepan Orlov
ReplyPlz be cool
Posted by Legacy on 10/02/2002 12:00amOriginally posted by: Buyung
ReplyOptimistis
Posted by Legacy on 08/12/2002 12:00amOriginally posted by: Kannan
A moon without black marks will not looks better. We (this group) should have 1 or 2 members like Eric.
ReplyNot perfect, but it works
Posted by Legacy on 08/11/2002 12:00amOriginally posted by: Alex F
I think CodeGuru is not a proper site to write a negative comments like these.
ReplySwikar: implementing Fall Trough may be a good reply to Eric and Jimmy.
It's just bad..
Posted by Legacy on 08/10/2002 12:00amOriginally posted by: Jimmy
Well first, don't do stuff with macros like that. Those who wanted a reference why, try the king, Stroustrup: http://www.research.att.com/~bs/bs_faq2.html#macro
ReplyDoing "switch" isn't THAT bad, but macros are, and having to switch on a string too. Check your design, maybe that you should be using a map or a hash_map instead..
Why is switch/case not OOP?
Posted by Legacy on 08/09/2002 12:00amOriginally posted by: John Wellbelove
Since starting to program in C++ I've read several C++ books and attended Advance C++ programming courses and this is the first time I've ever heard about 'switch/case' being non-OOP!
If anyone has a URL of an article which gives a reason why, I would be most interested.
ReplyREALLY REALLY BAD THINGS!!!!!
Posted by Legacy on 08/08/2002 12:00amOriginally posted by: Eric
Please people!!! please don't do that please!! :o(
Reply