Date: Thu, 21 Apr 2016 16:01:17 +1000 (EST) From: Bruce Evans <brde@optusnet.com.au> To: John Baldwin <jhb@freebsd.org> Cc: Bruce Evans <brde@optusnet.com.au>, araujo@freebsd.org, "src-committers@freebsd.org" <src-committers@freebsd.org>, "svn-src-all@freebsd.org" <svn-src-all@freebsd.org>, "svn-src-head@freebsd.org" <svn-src-head@freebsd.org> Subject: Re: svn commit: r298247 - head/sbin/fdisk_pc98 Message-ID: <20160421150811.K1013@besplex.bde.org> In-Reply-To: <17068472.zUDTmEYeVg@ralph.baldwin.cx> References: <201604190446.u3J4kD9G050780@repo.freebsd.org> <CAOfEmZhofgYySTtMKAXo-Qkm8BRaOP8kzEfchdtPTH3T-0W-qQ@mail.gmail.com> <20160420115844.Y967@besplex.bde.org> <17068472.zUDTmEYeVg@ralph.baldwin.cx>
next in thread | previous in thread | raw e-mail | index | archive | help
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 <jhb@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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20160421150811.K1013>