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 <roret...@linux.microsoft.com> > > > Sent: Monday, April 3, 2023 7:59 PM > > > To: dev@dpdk.org > > > Cc: ciara.po...@intel.com; bruce.richard...@intel.com; > > > david.march...@redhat.com; tho...@monjalon.net; Tyler Retzlaff > > > <roret...@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 <roret...@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. /Bruce