Re: [PATCH v1 1/8] lib/string: introduce match_string() helper
From: Andy Shevchenko
Date: Mon Jan 11 2016 - 09:59:41 EST
On Sat, 2016-01-09 at 13:57 +0200, Andy Shevchenko wrote:
> On Sat, Jan 9, 2016 at 3:12 AM, Sergey Senozhatsky
> <sergey.senozhatsky@xxxxxxxxx> wrote:
> > Andy Shevchenko wrote:
> > [..]
> > > >
> > > > strncmp() case seems to be quite common.
> > >
> > > Like I answered to Rasmus, please, provide real examples.
> >
> > [..]
> > > > int nmatch_string(array, array_size, string, string_len)
> > > > {
> > > > ÂÂÂÂÂÂdo {
> > > > ÂÂÂÂÂÂÂÂÂÂÂÂÂÂstrncmp();
> > > > ÂÂÂÂÂÂ} while ();
> > > > }
> > > >
> > > > int match_string(array, array_size, string)
> > > > {
> > > > ÂÂÂÂÂÂreturn nmatch_string(array, array_size, string,
> > > > strlen(string));
> > > > }
> > >
> > > See above.
> >
> > after some quick and inaccurate grepping, well, probably you're
> > right - not worth it.
>
> Good grep anyway, it clearly shows that there is hard to generalize
> which limit to use: a) length of a first argument / item from a list,
> b) length of a second argument or a constant.
>
> > arch/mips/bcm63xx/boards/board_bcm963xx.cÂÂvoid __init
> > board_prom_init(void)
> > net/irda/irnet/irnet_irda.cÂÂÂirnet_dname_to_daddr()
> > arch/powerpc/sysdev/ppc4xx_cpm.c static ssize_t cpm_idle_store()
> > arch/x86/ras/mce_amd_inj.c static int __set_inj
> > drivers/hwtracing/intel_th/msu.c mode_store
> > drivers/pci/pcie/aer/ecrc.c void pcie_ecrc_get_policy
> > drivers/pci/pcie/aspm.c pcie_aspm_set_policy
> > drivers/scsi/aic7xxx/aic7xxx_osm.c aic7xxx_setup
> > drivers/scsi/aic7xxx/aic79xx_osm.c aic79xx_setup
> > drivers/scsi/scsi_transport_fc.c static int get_fc_##title##_match
> > drivers/staging/android/ion/hisilicon/hi6220_ion.c get_type_by_name
> > drivers/staging/lustre/lustre/lmv/lproc_lmv.c placement_name2policy
> > drivers/xen/sys-hypervisor.c pmu_mode_store
Thought more about those cases.
If you would like you may introduce something like
int nmatch_string(array, array_size, string, int len)
{
 if (len < 0)
  return match_string();
 for (â) {
  size_t itemlen = (len > 0) ? len : strlen(array[index]);
â
  if (!strncmp(array[index], string, itemlen))
   return index;
 }
 return -EINVAL;
}
And convert existing users where it makes sense.
--
Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Intel Finland Oy