Didier Kryn <[email protected]> writes:
> Le 25/01/2016 13:23, Rainer Weikusat a écrit :
>>      while (*r) if (*r++ == '/') n = r;
>
>     Does it mean
>
>     while (*r)
>       {
>         if (*r == '/')
>       {
>            n = r;
>            r++;
>         }
>       }
>
> or
>
>     while (*r)
>       {
>         if (*r == '/')
>       {
>            r++;
>            n = r;
>         }
>       }
>
>
>     I think the second answer is the good one. It is more readable and
> less error-prone than your example and

... doesn't work. r (for 'running pointer') needs to be incremented on
every iteration until it hits the end of the string. In case it
currently pointed to a '/', 'n' ('pointer to [start of] name') needs to
be set to the char behind the slash. As soons as *r == 0 aka !*r, n will
point to the char after the last slash in the original string, ie, to
the program name part of a program pathname.

This is even already 'optimized for simplicity' as gcc will (usually)
issue code to reload the char r points and thus, if this was supposed
'optimized', it really ought to be something like (all untested)

char const *r, *n;
int c;

n = r = arg0;
while (c = *r++) if (c == '/') n = r;

A multi-line version could look like this:

while (c = *r) {
        ++r;
        if (c == '/') n = r;
}

Or, for people who think everything ought to be expressed as for-loop
because everything can be expressed as for-loop,

char const *r, *n;
int c0, c1;

for (n = r = arg0, c1 = 0; c0 = *r; r++) {
        if (c1 == '/') n = r;
        c1 = c0;
}

This is a nice progression from '[maybe unusal but] straight-forward' to
'conventional [& contorted]'.
_______________________________________________
Dng mailing list
[email protected]
https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng

Reply via email to