aboutsummaryrefslogtreecommitdiffstats
path: root/lib/string.c
diff options
context:
space:
mode:
authorYury Norov <[email protected]>2020-02-04 01:37:20 +0000
committerLinus Torvalds <[email protected]>2020-02-04 03:05:26 +0000
commit0bee0cece2a6a71ccc347fdc1d46cf638cd5fd1c (patch)
treeca63b8bff0d7d5047076f5331eef5692c023f8e3 /lib/string.c
parentproc: convert everything to "struct proc_ops" (diff)
downloadkernel-0bee0cece2a6a71ccc347fdc1d46cf638cd5fd1c.tar.gz
kernel-0bee0cece2a6a71ccc347fdc1d46cf638cd5fd1c.zip
lib/string: add strnchrnul()
Patch series "lib: rework bitmap_parse", v5. Similarl to the recently revisited bitmap_parselist(), bitmap_parse() is ineffective and overcomplicated. This series reworks it, aligns its interface with bitmap_parselist() and makes it simpler to use. The series also adds a test for the function and fixes usage of it in cpumask_parse() according to the new design - drops the calculating of length of an input string. bitmap_parse() takes the array of numbers to be put into the map in the BE order which is reversed to the natural LE order for bitmaps. For example, to construct bitmap containing a bit on the position 42, we have to put a line '400,0'. Current implementation reads chunk one by one from the beginning ('400' before '0') and makes bitmap shift after each successful parse. It makes the complexity of the whole process as O(n^2). We can do it in reverse direction ('0' before '400') and avoid shifting, but it requires reverse parsing helpers. This patch (of 7): New function works like strchrnul() with a length limited string. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Yury Norov <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Cc: Rasmus Villemoes <[email protected]> Cc: Amritha Nambiar <[email protected]> Cc: Willem de Bruijn <[email protected]> Cc: Kees Cook <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: "Tobin C . Harding" <[email protected]> Cc: Will Deacon <[email protected]> Cc: Miklos Szeredi <[email protected]> Cc: Vineet Gupta <[email protected]> Cc: Chris Wilson <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Steffen Klassert <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
Diffstat (limited to 'lib/string.c')
-rw-r--r--lib/string.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c
index 08ec58cc673b..f607b967d978 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -434,6 +434,23 @@ char *strchrnul(const char *s, int c)
EXPORT_SYMBOL(strchrnul);
#endif
+/**
+ * strnchrnul - Find and return a character in a length limited string,
+ * or end of string
+ * @s: The string to be searched
+ * @count: The number of characters to be searched
+ * @c: The character to search for
+ *
+ * Returns pointer to the first occurrence of 'c' in s. If c is not found,
+ * then return a pointer to the last character of the string.
+ */
+char *strnchrnul(const char *s, size_t count, int c)
+{
+ while (count-- && *s && *s != (char)c)
+ s++;
+ return (char *)s;
+}
+
#ifndef __HAVE_ARCH_STRRCHR
/**
* strrchr - Find the last occurrence of a character in a string