On Wed, 20 Apr 2016, John Baldwin wrote:

On Wednesday, April 20, 2016 01:06:38 PM Bruce Evans wrote:
On Wed, 20 Apr 2016, Marcelo Araujo wrote:

2016-04-20 0:16 GMT+08:00 John Baldwin <j...@freebsd.org>:

On Tuesday, April 19, 2016 04:46:13 AM Marcelo Araujo wrote:
Author: araujo
Date: Tue Apr 19 04:46:13 2016
New Revision: 298247
URL: https://svnweb.freebsd.org/changeset/base/298247

Log:
  Remove redundant parenthesis.

  Submitted by:       pfg
  MFC after:  2 weeks.

I don't realling like churnging to the nonstandard nitems().  Use
of the nonstandard <sys/param.h> is bad enough.

I think it's not that bad from a readability standpoint.  Other languages
have fairly concise syntax for 'for-each' loops and this provides a
closer variant of that for statically sized arrays.  TAILQ_FOREACH() is
still nicer of course.  One could imagine doing some sort of
ARRAY_FOREACH() that was:

Ugh, I really realling (sic) don't like the FOREACH macros.  The FOREACH
macros add syntactic salt.  The queue macros were bad enough before they
had FOREACH.  Arrays don't start with such nastiness.

#define ARRAY_FOREACH(p, array)    \
        for (size_t __i = 0, (p) = &(array)[0]; __i < nitems((array)); __i++, 
(p)++)

This only works in the not very usual case of a simple loop from the start
to the end.  Even the name EACH becomes wrong if the range is anything else.
For full nastiness, add a few hundred FORFOO macros to handle multi-
dimensional arrays with different access methods, array slices, sentinels
and other terminating conditions, etc.

Perhaps better is this:

#define ARRAY_FOREACH(p, array) \
        for ((p) = &(array)[0]; (p) < &(array)[nitems((array))]; (p)++)

(No need for __i)

But hiding the indexes forces the old C programming idiom of using
pointers for everything.  A mere few hundred FORFOO macros won't do.
For just 1-dimensional FOREACH, you need 3 versions to expose p, i or
both p and i.

Churnging too much (also remove excessive parentheses and braces) gives:

        size_t i;               /* XXX: I don't like unsigned types, but... */

        for (i = 0; i < nitems(part_types); i++)
                if (part_types[i].type == type & 0x7f)
                        return (part_types[i].name);
        return ("unknown");

This would work for me (unless folks actually like the ARRAY_FOREACH()
idea).

Once a loop is simple enough, it is easy to see that it really is simple
without an ARRAY_FOREACH() macros that hides its details.  Actually, it
is not so simple since it has a special terminating condition.  It only
handles EACH (every) element in unusual cases.

Bruce
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to