Markus Mayer
2016-Jul-05 20:47 UTC
[Nouveau] [PATCH v2 0/7] lib: string: add functions to case-convert strings
This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality. Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories: - copying a string and converting the case while specifying a maximum length to mimic strncpy() - copying a string and converting the case without specifying a length to mimic strcpy() - converting the case of a string in-place (i.e. modifying the string that was passed in) Consequently, I am proposing these new functions: 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); They all return a pointer to the terminating '\0' in the destination string (for strtoupper() and strtolower() that is "s"). Several drivers are being modified to make use of the functions above. Another driver that also makes use of this functionality will be submitted upstream shortly, which prompted this whole exercise. The changes made here have been compile-tested, but not tried out, due to lack of required hardware. Changes since v1: - expanded strtolower() into a family of functions that cover use cases when a length argument is or isn't required and that support copying the string into a new buffer or changing it in-place - changed the function semantics to return a pointer to the terminating '\0' character of the modified string - added strtoupper() functionality mirroring the above - dropped the ACPICA patch, since that code is OS independent and can't rely on a Linux library function (see [2]) - Added two new patches replacing strtoupper() implementations [1] https://lkml.org/lkml/2016/6/30/727 [2] https://lkml.org/lkml/2016/7/1/9 Markus Mayer (7): lib: string: add functions str[n]cpytolower()/str[n]cpytoupper() drm/nouveau/core: make use of new strncpytolower() function ACPI / device_sysfs: make use of new strtolower() function staging: speakup: replace spk_strlwr() with strncpytolower() iscsi-target: replace iscsi_initiatorname_tolower() with strcpytolower() drm/nouveau/fifo/gk104: make use of new strcpytoupper() function power_supply: make use of new strcpytoupper() function drivers/acpi/device_sysfs.c | 4 +- drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 9 +---- drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c | 5 +-- drivers/power/power_supply_sysfs.c | 13 +++---- drivers/staging/speakup/kobjects.c | 3 +- drivers/staging/speakup/main.c | 3 +- drivers/staging/speakup/speakup.h | 1 - drivers/staging/speakup/varhandlers.c | 12 ------ drivers/target/iscsi/iscsi_target_nego.c | 17 +-------- include/linux/string.h | 48 ++++++++++++++++++++++++ lib/string.c | 40 ++++++++++++++++++++ 11 files changed, 100 insertions(+), 55 deletions(-) -- 2.7.4
Markus Mayer
2016-Jul-05 20:47 UTC
[Nouveau] [PATCH v2 1/7] lib: string: add functions to case-convert strings
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 at broadcom.com> --- include/linux/string.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/string.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) 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); -- 2.7.4
Markus Mayer
2016-Jul-05 20:47 UTC
[Nouveau] [PATCH v2 2/7] drm/nouveau/core: make use of new strncpytolower() function
Call strncpytolower() rather than copying the string explicitly and then walking it to convert it to lowercase. Signed-off-by: Markus Mayer <mmayer at broadcom.com> --- drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c index 34ecd4a..6f3314f 100644 --- a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c +++ b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c @@ -36,16 +36,10 @@ nvkm_firmware_get(struct nvkm_device *device, const char *fwname, { char f[64]; char cname[16]; - int i; /* Convert device name to lowercase */ - strncpy(cname, device->chip->name, sizeof(cname)); + strncpytolower(cname, device->chip->name, sizeof(cname)); cname[sizeof(cname) - 1] = '\0'; - i = strlen(cname); - while (i) { - --i; - cname[i] = tolower(cname[i]); - } snprintf(f, sizeof(f), "nvidia/%s/%s.bin", cname, fwname); return request_firmware(fw, f, device->dev); -- 2.7.4
Markus Mayer
2016-Jul-05 20:47 UTC
[Nouveau] [PATCH v2 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
Call strcpytoupper() rather than copying the string explicitly and then walking it to convert it to uppercase. Signed-off-by: Markus Mayer <mmayer at broadcom.com> --- drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c index 743f3a1..8d01032 100644 --- a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c +++ b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c @@ -332,10 +332,7 @@ gk104_fifo_intr_fault(struct gk104_fifo *fifo, int unit) enum nvkm_devidx engidx = nvkm_top_fault(device->top, unit); if (engidx < NVKM_SUBDEV_NR) { const char *src = nvkm_subdev_name[engidx]; - char *dst = en; - do { - *dst++ = toupper(*src++); - } while(*src); + strcpytoupper(en, src); engine = nvkm_device_engine(device, engidx); } } else { -- 2.7.4
Joe Perches
2016-Jul-05 22:14 UTC
[Nouveau] [PATCH v2 0/7] lib: string: add functions to case-convert strings
On Tue, 2016-07-05 at 13:47 -0700, Markus Mayer wrote:> This series introduces a family of generic string case conversion > functions. This kind of functionality is needed in several places in > the kernel. Right now, everybody seems to be implementing their own > copy of this functionality. > > Based on the discussion of the previous version of this series[1] and > the use cases found in the kernel, it does look like having several > flavours of case conversion functions is beneficial. The use cases fall > into three categories: > - copying a string and converting the case while specifying a > maximum length to mimic strncpy() > - copying a string and converting the case without specifying a > length to mimic strcpy() > - converting the case of a string in-place (i.e. modifying the > string that was passed in) > > Consequently, I am proposing these new functions: > 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);I think there isn't much value in anything other than strto<upper|lower>. Using str[n]cpy followed by strto<upper|lower> is pretty obvious and rarely used anyway.
Markus Mayer
2016-Jul-05 22:36 UTC
[Nouveau] [PATCH v2 0/7] lib: string: add functions to case-convert strings
On 5 July 2016 at 15:14, Joe Perches <joe at perches.com> wrote:> On Tue, 2016-07-05 at 13:47 -0700, Markus Mayer wrote: >> This series introduces a family of generic string case conversion >> functions. This kind of functionality is needed in several places in >> the kernel. Right now, everybody seems to be implementing their own >> copy of this functionality. >> >> Based on the discussion of the previous version of this series[1] and >> the use cases found in the kernel, it does look like having several >> flavours of case conversion functions is beneficial. The use cases fall >> into three categories: >> - copying a string and converting the case while specifying a >> maximum length to mimic strncpy() >> - copying a string and converting the case without specifying a >> length to mimic strcpy() >> - converting the case of a string in-place (i.e. modifying the >> string that was passed in) >> >> Consequently, I am proposing these new functions: >> 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); > > I think there isn't much value in anything other > than strto<upper|lower>. > > Using str[n]cpy followed by strto<upper|lower> is > pretty obvious and rarely used anyway.First time around, folks were proposing the "copy" variants when I submitted just strtolower() by itself[1]. They just asked for source and destination parameters to strtolower(), but looking at the use cases that wouldn't have worked so well. Hence it evolved into these 6 functions. Here's a breakdown of how the functions are being used (patches 2-7), see also [2]: Patch 2: strncpytolower() Patch 3: strtolower() Patch 4: strncpytolower() and strtolower() Patch 5: strtolower() Patch 6: strcpytoupper() Patch 7: strcpytoupper() So it does look like the copy + change case variant is more frequently used than just strto<upper|lower>. Regards, -Markus [1] https://lkml.org/lkml/2016/7/1/652 [2] https://lkml.org/lkml/2016/7/5/542
Eric Engestrom
2016-Jul-07 11:04 UTC
[Nouveau] [PATCH v2 1/7] lib: string: add functions to case-convert strings
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
Rasmus Villemoes
2016-Jul-08 00:19 UTC
[Nouveau] [PATCH v2 1/7] lib: string: add functions to case-convert strings
On Tue, Jul 05 2016, Markus Mayer <mmayer at 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
Possibly Parallel Threads
- [PATCH v2 0/7] lib: string: add functions to case-convert strings
- [PATCH 1/6] lib: string: add function strtolower()
- [PATCH v2 1/7] lib: string: add functions to case-convert strings
- [PATCH v3 1/7] lib: string: add functions to case-convert strings
- [PATCH v3 0/7] lib: string: add functions to case-convert strings