Re: [PATCH v4 1/9] lib/string: introduce match_string() helper
From: Andy Shevchenko
Date: Tue Feb 02 2016 - 02:59:52 EST
On Mon, 2016-02-01 at 22:05 -0800, Andrew Morton wrote:
> On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko
> @linux.intel.com> wrote:
> > + * @array: array of strings
> > + * @n: number of strings in the array or -1 for
> > NULL terminated arrays
> > + * @string: string to match with
> > + *
> > + * Return:
> > + * index of a @string in the @array if matches, or %-ENODATA
> > otherwise.
> > + */
> > +int match_string(const char * const *array, size_t n, const char
> > *string)
> > +{
> > + int index;
> > + const char *item;
> > +
> > + for (index = 0; index < n; index++) {
>
> So we're taking an int and comparing that with (size_t)-1, relying
> upon
> the compiler promoting the int to an unsigned type because size_t is
> unsigned.ÂÂIt works, but it isn't pretty - there wasn't really much
> point in making size have type size_t.
It was Rasmus' idea [1], before that I used int and tristate branch. I
agreed that one of that branches wasn't a good idea, but the rest was
exactly about differentiating size to compare and infinite (till
terminator) loop.
[1]Âhttp://www.spinics.net/lists/kernel/msg2156925.html
>
>
> > + item = array[index];
> > + if (!item)
> > + break;
> > + if (!strcmp(item, string))
> > + return index;
> > + }
> > +
> > + return -ENODATA;
> > +}
> > +EXPORT_SYMBOL(match_string);
>
--
Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Intel Finland Oy