@@ -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
@@ -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
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(-)