[SR-Dev] git:andrei/switch: script parsing: C style switch() & case support

Daniel-Constantin Mierla miconda at gmail.com
Wed Feb 4 22:30:31 CET 2009



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 at 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:
> 1)  config clarity - instead of a long
> if (a==... || a==...)
>  you have
>
> switch(a) {
>  case ...:
>  case ...:
> }
>
> 2) 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;
> }
>
> 3) although not implemented yet in kamailio/openser (but planned), 
> inside 'break' can make code cleaner
> switch(a) {
> case v1:
>       ...
>       if(...) {
>           break;
>       }
>       ...
> break;
> }
>
> 4) 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

-- 
Daniel-Constantin Mierla
http://www.asipto.com




More information about the sr-dev mailing list