lkml.org 
[lkml]   [2019]   [Apr]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH 1/6] lib/string: add strnchrnul()
From
Date
On 28/04/2019 05.29, Yury Norov wrote:
> New function works like strchrnul() with a length limited strings.
>
> Signed-off-by: Yury Norov <ynorov@marvell.com>
> ---
> include/linux/string.h | 3 +++
> lib/string.c | 20 ++++++++++++++++++++
> 2 files changed, 23 insertions(+)
>
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 4deb11f7976b..69e8df51b630 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -62,6 +62,9 @@ extern char * strchr(const char *,int);
> #ifndef __HAVE_ARCH_STRCHRNUL
> extern char * strchrnul(const char *,int);
> #endif
> +#ifndef __HAVE_ARCH_STRNCHRNUL

I find it unlikely this would ever be performance-critical, so an arch
would want to implement its own. Please let's avoid adding redundant
ifdeffery (and yes, there's probably quite a few of the existing ones
that should go away).

>
> +#ifndef __HAVE_ARCH_STRNCHRNUL
> +/**
> + * 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 first occurrence of 'c' in s. If c is not found,
> + * then return a pointer to the null byte at the end of s.

Pet peeve: nul, not null. Especially important when this is in contrast
to returning a NULL pointer. Also, the description is inaccurate; the
whole point of the n-variant is that there is not necessarily any nul
terminator.

> + */
> +char *strnchrnul(const char *s, int count, int c)
> +{

Yes, make count size_t so it's easier to distinguish from the c, and
keep this parameter ordering (that matches the str n chr name).

> + while (count-- && *s && *s != (char)c)
> + s++;
> + return (char *)s;
> +}
> +EXPORT_SYMBOL(strnchrnul);

This can be added if a modular user shows up. No reason to add that
bloat to the kernel image immediately.

Rasmus

\
 
 \ /
  Last update: 2019-04-28 20:59    [W:0.105 / U:0.008 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site