diff mbox

[v2,1/7] lib: string: add functions to case-convert strings

Message ID 1467751631-22878-2-git-send-email-mmayer@broadcom.com (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Markus Mayer July 5, 2016, 8:47 p.m. UTC
Add a collection of generic functions to convert strings to lowercase
or uppercase.

Changing the case of a string (with or without copying it first) seems
to be a recurring requirement in the kernel that is currently being
solved by several duplicated implementations doing the same thing. This
change aims at reducing this code duplication.

The new functions are
    char *strncpytoupper(char *dst, const char *src, size_t len);
    char *strncpytolower(char *dst, const char *src, size_t len);
    char *strcpytoupper(char *dst, const char *src);
    char *strcpytolower(char *dst, const char *src);
    char *strtoupper(char *s);
    char *strtolower(char *s);

The "str[n]cpyto*" versions of the function take a destination string
and a source string as arguments. The "strncpyto*" versions
additionally take a length argument like strncpy() itself. Lastly, the
strto* functions take a single string argument and modify the passed-in
string.

All functions return a pointer to the terminating '\0' character in the
modified string ("dst" or "s", respectively).

Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
 include/linux/string.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/string.c           | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+)

Comments

Eric Engestrom July 7, 2016, 11:04 a.m. UTC | #1
On Tue, Jul 05, 2016 at 01:47:05PM -0700, Markus Mayer wrote:
> All functions return a pointer to the terminating '\0' character in the
> modified string ("dst" or "s", respectively).

I think this is going to be confusing. From the man:

	The strcpy() and strncpy() functions return a pointer to the
	destination string dest.

I think it would be better to keep the same behaviour, especially since
you used the same name for your functions (which I think is sensible),
not to mention you don't use this return value in any of your calls.
(IMHO strcpy() shouldn't have had a return value and neither should your
functions, but since it does, yours should match.)

Cheers,
  Eric
--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rasmus Villemoes July 8, 2016, 12:19 a.m. UTC | #2
On Tue, Jul 05 2016, Markus Mayer <mmayer@broadcom.com> wrote:

> Add a collection of generic functions to convert strings to lowercase
> or uppercase.
>
> Changing the case of a string (with or without copying it first) seems
> to be a recurring requirement in the kernel that is currently being
> solved by several duplicated implementations doing the same thing. This
> change aims at reducing this code duplication.
>
> +/**
> + * strncpytoupper - Copy a length-limited string and convert to uppercase.
> + * @dst: The buffer to store the result.
> + * @src: The string to convert to uppercase.
> + * @len: Maximum string length. May be 0 to set no limit.
> + *
> + * Returns pointer to terminating '\0' in @dst.
> + */
> +char *strncpytoupper(char *dst, const char *src, size_t len)
> +{
> +	size_t i;
> +
> +	for (i = 0; src[i] != '\0' && (i < len || !len); i++)
> +		dst[i] = toupper(src[i]);
> +	if (i < len || !len)
> +		dst[i] = '\0';
> +
> +	return dst + i;
> +}

Hm, this seems to copy the insane semantics from strncpy of not
guaranteeing '\0'-termination.

Why use 0 as a sentinel, when (size_t)-1 == SIZE_MAX would work just as
well and require a little less code (no || !len)?

I regret suggesting this return semantics and now agree that void would
be better, especially since there doesn't seem to be anyone who can
use this (or any other) return value. How about

if (!len)
   return;

for (i = 0; i < len && src[i]; ++i)
  dst[i] = toupper(src[i]);
dst[i < len ? i : i-1] = '\0';

(I think you must do i < len before testing src[i], since the len
parameter should be an upper bound on the number of bytes to access in
both src and dst).

Rasmus
--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Markus Mayer July 8, 2016, 6:04 p.m. UTC | #3
On 7 July 2016 at 17:19, Rasmus Villemoes <linux@rasmusvillemoes.dk> wrote:
> On Tue, Jul 05 2016, Markus Mayer <mmayer@broadcom.com> wrote:
>
>> +/**
>> + * strncpytoupper - Copy a length-limited string and convert to uppercase.
>> + * @dst: The buffer to store the result.
>> + * @src: The string to convert to uppercase.
>> + * @len: Maximum string length. May be 0 to set no limit.
>> + *
>> + * Returns pointer to terminating '\0' in @dst.
>> + */
>> +char *strncpytoupper(char *dst, const char *src, size_t len)
>> +{
>> +     size_t i;
>> +
>> +     for (i = 0; src[i] != '\0' && (i < len || !len); i++)
>> +             dst[i] = toupper(src[i]);
>> +     if (i < len || !len)
>> +             dst[i] = '\0';
>> +
>> +     return dst + i;
>> +}
>
> Hm, this seems to copy the insane semantics from strncpy of not
> guaranteeing '\0'-termination.

Yeah. I've been tossing that one around a bit. The reason I did it
this way in the end is due to the use cases I found. strncpy() is
being used there and I was a little wary to make too many changes all
at once.

But I understand your point. I'll look at it again and see what
changes might be required in the code that used strncpy() before.

> Why use 0 as a sentinel, when (size_t)-1 == SIZE_MAX would work just as
> well and require a little less code (no || !len)?

I'll change that.

> I regret suggesting this return semantics and now agree that void would
> be better, especially since there doesn't seem to be anyone who can
> use this (or any other) return value. How about
>
> if (!len)
>    return;
>
> for (i = 0; i < len && src[i]; ++i)
>   dst[i] = toupper(src[i]);
> dst[i < len ? i : i-1] = '\0';

This makes sense.

> (I think you must do i < len before testing src[i], since the len
> parameter should be an upper bound on the number of bytes to access in
> both src and dst).
>
> Rasmus

Thanks,
-Markus
--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..c58d510 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,8 @@  extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+char *strncpytoupper(char *dst, const char *src, size_t len);
+char *strncpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,50 @@  static inline const char *kbasename(const char *path)
 	return tail ? tail + 1 : path;
 }
 
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+static inline char *strcpytoupper(char *dst, const char *src)
+{
+	return strncpytoupper(dst, src, 0);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+static inline char *strcpytolower(char *dst, const char *src)
+{
+	return strncpytolower(dst, src, 0);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ *
+ * Returns pointer to terminating '\0' in @s.
+ */
+static inline char *strtoupper(char *s)
+{
+	return strncpytoupper(s, s, 0);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ *
+ * Returns pointer to terminating '\0' in @s.
+ */
+static inline char *strtolower(char *s)
+{
+	return strncpytolower(s, s, 0);
+}
+
 #endif /* _LINUX_STRING_H_ */
diff --git a/lib/string.c b/lib/string.c
index ed83562..900f357 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,45 @@  char *strreplace(char *s, char old, char new)
 	return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strncpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be 0 to set no limit.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+char *strncpytoupper(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	for (i = 0; src[i] != '\0' && (i < len || !len); i++)
+		dst[i] = toupper(src[i]);
+	if (i < len || !len)
+		dst[i] = '\0';
+
+	return dst + i;
+}
+EXPORT_SYMBOL(strncpytoupper);
+
+/**
+ * strncpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be 0 to set no limit.
+ *
+ * Returns pointer to terminating '\0' in @dst.
+ */
+char *strncpytolower(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	for (i = 0; src[i] != '\0' && (i < len || !len); i++)
+		dst[i] = tolower(src[i]);
+	if (i < len || !len)
+		dst[i] = '\0';
+
+	return dst + i;
+}
+EXPORT_SYMBOL(strncpytolower);