lkml.org 
[lkml]   [2016]   [Feb]   [2]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH v4 1/9] lib/string: introduce match_string() helper
From
Date
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@linux.intel.com>
Intel Finland Oy

\
 
 \ /
  Last update: 2016-02-02 09:21    [W:0.101 / U:0.588 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site