On 02/04/2009 11:18 PM, Daniel-Constantin Mierla wrote:
On 02/04/2009 10:58 PM, Andrei Pelinescu-Onciul wrote:
On Feb 04, 2009 at 22:29, Daniel-Constantin Mierla miconda@gmail.com wrote:
Hi Andrei,
does the switch support only integers?
Apart of return code from functions, most of the switches I have seen are for strings (e.g., matching dialed number/address, user IDs, etc...).
Yes, it's only for integers.
For strings, ifs should be used, since the switch wouldn't bring any advantage.
There are couple of advantages:
- config clarity - instead of a long
if (a==... || a==...) you have
switch(a) { case ...: case ...: }
- it can shorten the script and save some ifs:
switch(a) { case v1: something; case v2: something else; break; }
This is equivalent of: if(a==v1) { something; } if(a==v1 || a==v2){ something; something else; }
or
if(a==v1) { something; something else; } if(a==v2){ something else; }
- although not implemented yet in kamailio/openser (but planned),
inside 'break' can make code cleaner switch(a) { case v1: ... if(...) { break; } ... break; }
- traditionally, as I could learn, pstn switching/old telco school guys
feel more confortable with line oriented routing rules, where they had something like:
extension match "1234": call emergency;
It could be a sswitch (although I would prefer single name to avoid confusions) if breaks logic for integer optimizations.
and btw, as the case values are constant, we probably optimize by creating a hash value at start up from it, so there can be some jumpers and linkers of the the actions crossing cases without breaks.
Cheers, Daniel