linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Alexandru Ardelean <alexandru.ardelean@analog.com>
Cc: linux-iio <linux-iio@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH v6 1/2] lib/string.c: add __sysfs_match_string_with_gaps() helper
Date: Tue, 22 Dec 2020 15:42:52 +0200	[thread overview]
Message-ID: <CAHp75Ve06gYFE+U_XNZfTFceMEzmADR6wd0OYr+tVMQ6hW3MLQ@mail.gmail.com> (raw)
In-Reply-To: <20201222131312.64957-1-alexandru.ardelean@analog.com>

On Tue, Dec 22, 2020 at 3:09 PM Alexandru Ardelean
<alexandru.ardelean@analog.com> wrote:
>
> The original docstring of the __sysfs_match_string() and match_string()
> helper, implied that -1 could be used to search through NULL terminated
> arrays, and positive 'n' could be used to go through arrays that may have
> NULL elements in the middle of the array.
>
> This isn't true. Regardless of the value of 'n', the first NULL element in
> the array will stop the search, even if the element may be after a NULL
> element.
>
> To allow for a behavior where we can use the __sysfs_match_string() to
> search over arrays with NULL elements in the middle, the
> __sysfs_match_string_with_gaps() helper is added.
> If n > 0, the search will continue until the element is found or n is
> reached.
> If n < 0, the search will continue until the element is found or a NULL
> character is found.

I'm wondering if we can leave __sysfs_match_string() alone (w/o adding
unnecessary branch).

int __sysfs_match_string_with_gaps(const char * const *array, size_t
n, const char *str)
{
       const char *item;
       int index;

       for (index = 0; index < n; index++) {
               item = array[index];
               if (!item)
                       continue;
               if (sysfs_streq(item, str))
                       return index;
       }
       return -EINVAL;
}

Note, the check n>0 seems redundant for this particular function.

> +static int __sysfs_match_string_common(const char * const *array, ssize_t n,
> +                                      const char *str, bool gaps)
> +{
> +       const char *item;
> +       int index;
> +
> +       for (index = 0; index < n; index++) {
> +               item = array[index];
> +               if (!item) {
> +                       if (gaps && n > 0)
> +                               continue;
> +                       break;
> +               }
> +               if (sysfs_streq(item, str))
> +                       return index;
> +       }
> +
> +       return -EINVAL;
> +}
> +
>  /**
>   * __sysfs_match_string - matches given string in an array
>   * @array: array of strings
> @@ -770,21 +790,32 @@ EXPORT_SYMBOL(match_string);
>   */
>  int __sysfs_match_string(const char * const *array, size_t n, const char *str)
>  {
> -       const char *item;
> -       int index;
> -
> -       for (index = 0; index < n; index++) {
> -               item = array[index];
> -               if (!item)
> -                       break;
> -               if (sysfs_streq(item, str))
> -                       return index;
> -       }
> -
> -       return -EINVAL;
> +       return __sysfs_match_string_common(array, n, str, false);
>  }

-- 
With Best Regards,
Andy Shevchenko

  parent reply	other threads:[~2020-12-22 13:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-22 13:13 [PATCH v6 1/2] lib/string.c: add __sysfs_match_string_with_gaps() helper Alexandru Ardelean
2020-12-22 13:13 ` [PATCH v6 2/2] iio: Handle enumerated properties with gaps Alexandru Ardelean
2020-12-22 13:42 ` Andy Shevchenko [this message]
2020-12-23  7:31   ` [PATCH v6 1/2] lib/string.c: add __sysfs_match_string_with_gaps() helper Alexandru Ardelean

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHp75Ve06gYFE+U_XNZfTFceMEzmADR6wd0OYr+tVMQ6hW3MLQ@mail.gmail.com \
    --to=andy.shevchenko@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexandru.ardelean@analog.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).