Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #55214

Re: switch case critique

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: switch case critique
Date 2014-12-21 17:10 +0000
Organization A noiseless patient Spider
Message-ID <87y4q1nd2d.fsf@bsb.me.uk> (permalink)
References (3 earlier) <h_elw.195150$7A5.71758@fx17.am4> <cdeb25bd-791c-4342-b39d-2c49957c206a@googlegroups.com> <TBylw.160444$O%4.13446@fx39.am4> <87ioh5p19a.fsf@bsb.me.uk> <quBlw.156007$W43.17677@fx04.am4>

Show all headers | View raw


BartC <bc@freeuk.com> writes:

> On 21/12/2014 13:42, Ben Bacarisse wrote:
>> BartC <bc@freeuk.com> writes:
>
>>> http://pastebin.com/EgGFgA0B
>>
>> In at least some cases you use switch instead of if so, yes, you'll use
>> a lot of them.  Is that from habit or some design preference?
>>
>> The example I came across (extractpath) brought up some other issues.
>> One reason that code build systems are complex is because of the need
>> for platform specific versions of functions like this.  Since you seem
>> to be writing Windows-only code it's bound to be a simpler problem.  The
>> other issue is possible undefined behaviour in the function if it
>> decrements t past the first byte of an object.
>
> This is the code:
>
> uchar* extractpath(uchar* s){
> static uchar str[260];
> uchar* t;
> int n;
>
> t=s+strlen(s)-1;	
>
> while (t>=s) {
>  switch (*t) {
>  break; case '\\':case '/':case ':': 	
>   n=t-s+1;		
>   memcpy(str,s,n);
>   str[n]=0;
>   return str;
>  }
>  --t;
> }
> return "";		
> }
>
> Here, the same value *t is being tested against three different
> values; using a switch is the natural way to express that (and easily
> extends to testing other characters). It's just a common pattern. (A
> better construct might have been '*t in ['\\', '/', ':']' but we don't
> have that.)
>
> The code is intended to run under Linux too (and has done, although
> I'm not sure how well this function was exercised; I would expect the
> checks for '\' and ':' to be harmless). But maybe it should be moved
> into the OS-specific module.
>
> t itself will eventually point to the char before s; that's how it
> breaks out of the loop. s is unlikely to ever have the value 0.
>
> If C might have problems in comparing (not dereferencing) two pointers
> where one might point just before an object, then I'll worry about
> that when it's necessary! And then put in a fix (if (t==s) break; at
> the end of loop body, for example).

<snip>

I'm glad you happy with the code.  Sorry I mentioned it.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

switch case critique fir <profesor.fir@gmail.com> - 2014-12-20 03:48 -0800
  Re: switch case critique Paul N <gw7rib@aol.com> - 2014-12-20 04:45 -0800
    Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-20 04:54 -0800
      Re: switch case critique BartC <bc@freeuk.com> - 2014-12-20 13:39 +0000
        Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-20 06:39 -0800
          Re: switch case critique BartC <bc@freeuk.com> - 2014-12-20 15:48 +0000
            Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-20 08:02 -0800
        Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-21 01:25 -0800
          Re: switch case critique BartC <bc@freeuk.com> - 2014-12-21 11:58 +0000
            Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-21 05:04 -0800
              Re: switch case critique BartC <bc@freeuk.com> - 2014-12-21 13:22 +0000
                Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-21 05:55 -0800
                Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-21 06:07 -0800
                Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-21 06:16 -0800
                Re: switch case critique BartC <bc@freeuk.com> - 2014-12-21 14:45 +0000
            Re: switch case critique Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 13:42 +0000
              Re: switch case critique BartC <bc@freeuk.com> - 2014-12-21 15:15 +0000
                Re: switch case critique Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 17:10 +0000
                Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-21 09:20 -0800
                Re: switch case critique BartC <bc@freeuk.com> - 2014-12-21 20:06 +0000
                Re: switch case critique Keith Thompson <kst-u@mib.org> - 2014-12-21 12:12 -0800
                Re: switch case critique BartC <bc@freeuk.com> - 2014-12-21 21:14 +0000
                Re: switch case critique Keith Thompson <kst-u@mib.org> - 2014-12-21 20:06 -0800
                Re: switch case critique Richard Damon <Richard@Damon-Family.org> - 2014-12-21 17:28 -0500
      Re: switch case critique Reinhardt Behm <rbehm@hushmail.com> - 2014-12-22 11:04 +0800
  Re: switch case critique fir <profesor.fir@gmail.com> - 2014-12-20 05:00 -0800
    Re: switch case critique BartC <bc@freeuk.com> - 2014-12-20 13:46 +0000
    Re: switch case critique fir <profesor.fir@gmail.com> - 2015-01-20 06:47 -0800

csiph-web