linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandru Ardelean <alexandru.ardelean@analog.com>
To: <linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <gregkh@linuxfoundation.org>, <andriy.shevchenko@linux.intel.com>,
	<namhyung@kernel.org>, <mingo@kernel.org>,
	Alexandru Ardelean <alexandru.ardelean@analog.com>
Subject: [PATCH 1/2][V2] lib: add __sysfs_match_string_with_gaps() helper
Date: Mon, 22 Apr 2019 17:02:51 +0300	[thread overview]
Message-ID: <20190422140251.8960-1-alexandru.ardelean@analog.com> (raw)
In-Reply-To: <20190422083257.21805-1-alexandru.ardelean@analog.com>

This helper is similar to __sysfs_match_string() with the exception that it
ignores NULL elements within the array.
It takes an extra parameter (called `gaps`) which when true will ignore
the NULL elements. When false, this function behaves exactly like
`__sysfs_match_string()`.

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 include/linux/string.h |  2 ++
 lib/string.c           | 48 ++++++++++++++++++++++++++++++++----------
 2 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index 7927b875f80c..08e4a33b5f29 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -190,6 +190,8 @@ static inline int strtobool(const char *s, bool *res)
 
 int match_string(const char * const *array, size_t n, const char *string);
 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
+int __sysfs_match_string_with_gaps(const char * const *array, size_t n,
+				   const char *str);
 
 /**
  * sysfs_match_string - matches given string in an array
diff --git a/lib/string.c b/lib/string.c
index 38e4ca08e757..2ae3b7e5ff3d 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -658,32 +658,58 @@ int match_string(const char * const *array, size_t n, const char *string)
 }
 EXPORT_SYMBOL(match_string);
 
-/**
- * __sysfs_match_string - matches given string in an array
- * @array: array of strings
- * @n: number of strings in the array or -1 for NULL terminated arrays
- * @str: string to match with
- *
- * Returns index of @str in the @array or -EINVAL, just like match_string().
- * Uses sysfs_streq instead of strcmp for matching.
- */
-int __sysfs_match_string(const char * const *array, size_t n, const char *str)
+static int __sysfs_match_string_common(const char * const *array, size_t n,
+				       const char *str, bool gaps)
 {
 	const char *item;
 	int index;
 
 	for (index = 0; index < n; index++) {
 		item = array[index];
-		if (!item)
+		if (!item) {
+			if (gaps)
+				continue;
 			break;
+		}
 		if (sysfs_streq(item, str))
 			return index;
 	}
 
 	return -EINVAL;
 }
+
+/**
+ * __sysfs_match_string - matches given string in an array
+ * @array: array of strings
+ * @n: number of strings in the array or -1 for NULL terminated arrays
+ * @str: string to match with
+ *
+ * Returns index of @str in the @array or -EINVAL, just like match_string().
+ * Uses sysfs_streq instead of strcmp for matching.
+ */
+int __sysfs_match_string(const char * const *array, size_t n, const char *str)
+{
+	return __sysfs_match_string_common(array, n, str, false);
+}
 EXPORT_SYMBOL(__sysfs_match_string);
 
+/**
+ * __sysfs_match_string_with_gaps - matches string in array ignoring NULLs
+ * @array: array of strings
+ * @n: number of strings in the array or -1 for NULL terminated arrays
+ * @str: string to match with
+ *
+ * Returns index of @str in the @array or -EINVAL, just like match_string().
+ * Uses sysfs_streq instead of strcmp for matching.
+ * Ignores NULL entries within the @array.
+ */
+int __sysfs_match_string_with_gaps(const char * const *array, size_t n,
+				   const char *str)
+{
+	return __sysfs_match_string_common(array, n, str, true);
+}
+EXPORT_SYMBOL(__sysfs_match_string_with_gaps);
+
 #ifndef __HAVE_ARCH_MEMSET
 /**
  * memset - Fill a region of memory with the given value
-- 
2.17.1


  parent reply	other threads:[~2019-04-22 14:03 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-22  8:32 [PATCH 1/2] lib: add __sysfs_match_string_with_gaps() helper Alexandru Ardelean
2019-04-22  8:32 ` [PATCH 2/2] iio: Handle enumerated properties with gaps Alexandru Ardelean
2019-04-22 10:38   ` Jonathan Cameron
2019-04-22 14:03   ` [PATCH 2/2][V2] " Alexandru Ardelean
2019-04-22 10:37 ` [PATCH 1/2] lib: add __sysfs_match_string_with_gaps() helper Jonathan Cameron
2019-04-22 11:16   ` Ardelean, Alexandru
2019-04-22 14:02 ` Alexandru Ardelean [this message]
2019-04-22 21:06 ` Greg KH
2019-04-23  6:38   ` Ardelean, Alexandru
2019-04-24 12:34     ` Jonathan Cameron
2019-04-25 19:37       ` gregkh
2019-04-26  9:29         ` Alexandru Ardelean
2019-04-26 14:27           ` andriy.shevchenko
2019-05-06 13:45             ` Alexandru Ardelean
2019-05-06 14:46               ` andriy.shevchenko
2019-05-08 11:34                 ` Ardelean, Alexandru

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=20190422140251.8960-1-alexandru.ardelean@analog.com \
    --to=alexandru.ardelean@analog.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@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).