From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754356AbcAHIpD (ORCPT ); Fri, 8 Jan 2016 03:45:03 -0500 Received: from mga04.intel.com ([192.55.52.120]:46711 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751787AbcAHIpA (ORCPT ); Fri, 8 Jan 2016 03:45:00 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,537,1444719600"; d="scan'208";a="886363609" Message-ID: <1452242596.30729.425.camel@linux.intel.com> Subject: Re: [PATCH v1 1/8] lib/string: introduce match_string() helper From: Andy Shevchenko To: Sergey Senozhatsky Cc: Tejun Heo , Linus Walleij , Dmitry Eremin-Solenikov , linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, "David S. Miller" , David Airlie , Andrew Morton , Rasmus Villemoes Date: Fri, 08 Jan 2016 10:43:16 +0200 In-Reply-To: <20160108000915.GA2551@swordfish> References: <1452168368-75630-1-git-send-email-andriy.shevchenko@linux.intel.com> <20160108000915.GA2551@swordfish> Organization: Intel Finland Oy Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.18.2-1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2016-01-08 at 09:13 +0900, Sergey Senozhatsky wrote: > On (01/07/16 14:06), Andy Shevchenko wrote: > > > > From time to time we have to match a string in an array. Make a > > simple helper > > for that purpose. > > > > Hello, > > strncmp() case seems to be quite common. Like I answered to Rasmus, please, provide real examples. > > > +int match_string(const char * const *array, size_t len, const char > > *string) >                                                   ^^^^^^^ > a nitpick, [to me] `len' looks a bit confusing, usually it's array > 'size'. Agreed. I would change it to plain 'n'. > > > +{ > > + int index = 0; > > + const char *item; > > + > > + do { > > + item = array[index]; > > + if (!item) > > + break; > > + if (!strcmp(item, string)) > > + return index; > > + } while (++index < len || !len); > > + > > + return -ENODATA; > > +} > > > do you want to do something like this: > > /* >  * hm, how to name this thing... nmatch_string() or > match_nstring()... >  * nmatch_string() _probably_ better, match_nstring() is totally > cryptic. >  */ > 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. -- Andy Shevchenko Intel Finland Oy