DPDK patches and discussions
 help / color / mirror / Atom feed
From: Tyler Retzlaff <roretzla@linux.microsoft.com>
To: Bruce Richardson <bruce.richardson@intel.com>
Cc: Konstantin Ananyev <konstantin.ananyev@huawei.com>,
	"dev@dpdk.org" <dev@dpdk.org>,
	"ciara.power@intel.com" <ciara.power@intel.com>,
	"david.marchand@redhat.com" <david.marchand@redhat.com>,
	"thomas@monjalon.net" <thomas@monjalon.net>
Subject: Re: [PATCH v3] telemetry: use portable syntax to initialize array
Date: Tue, 4 Apr 2023 09:28:53 -0700	[thread overview]
Message-ID: <20230404162853.GC18560@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net> (raw)
In-Reply-To: <ZCxODiYPbQRIdn5x@bricha3-MOBL.ger.corp.intel.com>

On Tue, Apr 04, 2023 at 05:19:26PM +0100, Bruce Richardson wrote:
> On Tue, Apr 04, 2023 at 08:59:06AM -0700, Tyler Retzlaff wrote:
> > On Tue, Apr 04, 2023 at 09:01:50AM +0000, Konstantin Ananyev wrote:
> > > 
> > > 
> > > > -----Original Message-----
> > > > From: Tyler Retzlaff <roretzla@linux.microsoft.com>
> > > > Sent: Monday, April 3, 2023 7:59 PM
> > > > To: dev@dpdk.org
> > > > Cc: ciara.power@intel.com; bruce.richardson@intel.com; david.marchand@redhat.com; thomas@monjalon.net; Tyler Retzlaff
> > > > <roretzla@linux.microsoft.com>
> > > > Subject: [PATCH v3] telemetry: use portable syntax to initialize array
> > > > 
> > > > Use of ranges in designated initialization are a non-standard gcc
> > > > extension. Use loops to initialize permitted characters on first use.
> > > > 
> > > > Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
> > > > ---
> > > >  lib/telemetry/telemetry_data.c | 22 +++++++++++++++-------
> > > >  1 file changed, 15 insertions(+), 7 deletions(-)
> > > > 
> > > > diff --git a/lib/telemetry/telemetry_data.c b/lib/telemetry/telemetry_data.c
> > > > index 2bac2de..562b387 100644
> > > > --- a/lib/telemetry/telemetry_data.c
> > > > +++ b/lib/telemetry/telemetry_data.c
> > > > @@ -152,13 +152,21 @@
> > > >  static bool
> > > >  valid_name(const char *name)
> > > >  {
> > > > -	char allowed[128] = {
> > > > -			['0' ... '9'] = 1,
> > > > -			['A' ... 'Z'] = 1,
> > > > -			['a' ... 'z'] = 1,
> > > > -			['_'] = 1,
> > > > -			['/'] = 1,
> > > > -	};
> > > > +	int index;
> > > > +	static bool initialized;
> > > > +	static char allowed[128];
> > > > +
> > > > +	if (!initialized) {
> > > > +		for (index = '0'; index <= '9'; index++)
> > > > +			allowed[index] = 1;
> > > > +		for (index = 'A'; index <= 'Z'; index++)
> > > > +			allowed[index] = 1;
> > > > +		for (index = 'a'; index <= 'z'; index++)
> > > > +			allowed[index] = 1;
> > > > +		allowed[(int)'_'] = allowed[(int)'/'] = 1;
> > > > +		initialized = true;
> > > > +	}
> > > > +
> > > >  	while (*name != '\0') {
> > > >  		if ((size_t)*name >= RTE_DIM(allowed) || allowed[(int)*name] == 0)
> > > >  			return false;
> > > 
> > > Wonder isn't the same as:
> > > while (*name != 0)
> > > 	if (!isascii(name[0] || (!isalnum(name[0])  && name[0] != '_' && name[0] != '/'))
> > > 		return false; 
> > > 
> > > Or MSVC doesn't support these  macros?
> > 
> > it's standard C msvc supports it. Bruce acceptable to take Konstantin's
> > suggestion here?
> > 
> Yep, I'm ok with it.
> 
> If I may confuse things a little, how about combining the two? Use an array
> with individual initializers for the "special characters", but using
> isalnum() for the rest. That way we get a clear list of allowed chars at
> the top of the array, without having a massive list of individual letters
> and numbers. For example: [completely untested and not compiled!].
> 
> {
> 	/* non-alpha-numeric characters allowed in names */
> 	static const char allowed[128] = { ['_'] = 1, ['/'] = 1 };
> 
> 	for ( ; *name != '\0'; name++) {
> 		if (isalnum(*name))
> 			continue;
> 		if ((size_t)*name >= RTE_DIM(allowed) || allowed[(int)*name] == 0)
> 			return false;
> 	}
> 	return true;
> }
> 
> I think something like this achieves a good balance between clarity and
> brevity.
> 1.  We don't have a massive array definition
> 2.  We do have a static constant array 
> 3.  We don't have the non-alnum character list hidden inside the middle of
>     an "if" statement.

at first glance i'm not sure if that initializer list syntax is portable
but i guess i'll find out.

i'm fine with being confused more so long as you give me the answer and
i can just cut and paste it ;)

> 
> /Bruce

  reply	other threads:[~2023-04-04 16:28 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-03 16:30 [PATCH 0/2] improve code portability Tyler Retzlaff
2023-04-03 16:30 ` [PATCH 1/2] telemetry: use malloc instead of variable length array Tyler Retzlaff
2023-04-03 17:17   ` Tyler Retzlaff
2023-04-03 20:19   ` Stephen Hemminger
2023-04-03 20:40     ` Tyler Retzlaff
2023-04-04  8:47     ` Bruce Richardson
2023-04-04 16:24       ` Tyler Retzlaff
2023-04-04 16:28         ` Bruce Richardson
2023-04-04 16:44           ` Tyler Retzlaff
2023-04-04 17:25             ` Bruce Richardson
2023-04-04 17:34               ` Tyler Retzlaff
2023-04-05  1:20                 ` Stephen Hemminger
2023-04-05  8:53                   ` Bruce Richardson
2023-04-05  1:04       ` Stephen Hemminger
2023-04-05  8:54         ` Bruce Richardson
2023-04-05 15:25           ` Tyler Retzlaff
2023-04-05 15:30             ` Dmitry Kozlyuk
2023-04-05 15:37               ` Stephen Hemminger
2023-04-05 15:47             ` Bruce Richardson
2023-04-03 16:30 ` [PATCH 2/2] telemetry: use portable syntax to initialize array Tyler Retzlaff
2023-04-03 17:04 ` [PATCH 0/2] improve code portability Bruce Richardson
2023-04-03 17:35   ` Tyler Retzlaff
2023-04-03 18:47 ` [PATCH v2] " Tyler Retzlaff
2023-04-03 18:47   ` [PATCH v2] telemetry: use portable syntax to initialize array Tyler Retzlaff
2023-04-03 18:59 ` [PATCH v3] improve code portability Tyler Retzlaff
2023-04-03 18:59   ` [PATCH v3] telemetry: use portable syntax to initialize array Tyler Retzlaff
2023-04-04  8:51     ` Bruce Richardson
2023-04-04 15:54       ` Tyler Retzlaff
2023-04-04 16:08         ` Bruce Richardson
2023-04-04  9:01     ` Konstantin Ananyev
2023-04-04 15:59       ` Tyler Retzlaff
2023-04-04 16:19         ` Bruce Richardson
2023-04-04 16:28           ` Tyler Retzlaff [this message]
2023-04-04 18:09 ` [PATCH v4] improve code portability Tyler Retzlaff
2023-04-04 18:09   ` [PATCH v4] telemetry: remove non-portable array initialization syntax Tyler Retzlaff
2023-04-05  8:56     ` Bruce Richardson
2023-04-05 15:27       ` Tyler Retzlaff
2023-04-05 18:52 ` [PATCH v5] improve code portability Tyler Retzlaff
2023-04-05 18:52   ` [PATCH v5] telemetry: remove non-portable array initialization syntax Tyler Retzlaff
2023-05-24 20:54     ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230404162853.GC18560@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net \
    --to=roretzla@linux.microsoft.com \
    --cc=bruce.richardson@intel.com \
    --cc=ciara.power@intel.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=konstantin.ananyev@huawei.com \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).