From patchwork Tue Jul 5 20:47:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 9215221 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 81B7060752 for ; Tue, 5 Jul 2016 20:50:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 739F828409 for ; Tue, 5 Jul 2016 20:50:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 688F92842A; Tue, 5 Jul 2016 20:50:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F007C28409 for ; Tue, 5 Jul 2016 20:50:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754766AbcGEUsX (ORCPT ); Tue, 5 Jul 2016 16:48:23 -0400 Received: from mail-qk0-f173.google.com ([209.85.220.173]:36083 "EHLO mail-qk0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755365AbcGEUsU (ORCPT ); Tue, 5 Jul 2016 16:48:20 -0400 Received: by mail-qk0-f173.google.com with SMTP id u68so23026489qkc.3 for ; Tue, 05 Jul 2016 13:48:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=HfvU7rDpB0/isIxUabkn0cwiwV4fTTE7/UYRrzNdgW4=; b=XcfBw49nbA6LYy/zA0LudY07xjpSri1TEpGaby0toJqM1k0f85QyrhTvrB2Rqw8qtL Ard8/pw7FU/9HVShI9eEiLNs20zrcGN/DJwMMCij49KCQ9BBq/LGsA6oNinFyFMd/+Eb U0JzWVJT4RUlUf/fDBTszXMDW/DzmcrRuCBs4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=HfvU7rDpB0/isIxUabkn0cwiwV4fTTE7/UYRrzNdgW4=; b=R0iaWCEFK6JWrvxyrmWMFyGxB6mnw0amIsf7RMdiBE5m11nAXENScpqMS5o2S25Xdr /MDEJ4fuIBoZrYFVRbwnXWUmKctmLa2ue7TyNI5KTbbuCzxPJAuoIAus3qkzePelg6hC YIMaz6WMM/C9pGvT1SFVfjM+queyUBU5kbEllizLx5km2YtD02XHsomPXixNdY9R9T2t 5J/2cA/mdKEySXMOaGN981Z6ZdtUUrvmbW7uq6aMZuIwmP/JtklFc8qyoiPpcnH8C+x+ Y+WxPd0JNLd/CXt5eJ+1k+XyEibyfC+cDbg9gM2vYLApeFAF5hfR2Ccv5eEwWsYLi9AP 6DGw== X-Gm-Message-State: ALyK8tLwLkxwy58FBsk9uPyySE/JjBcnUlXHYAnw4ILnkaWEwpHifMcX7SWLHw8yzoeUn9AY X-Received: by 10.55.198.208 with SMTP id s77mr26119357qkl.61.1467751698526; Tue, 05 Jul 2016 13:48:18 -0700 (PDT) Received: from lbrmn-mmayer.lab.lan ([216.31.219.19]) by smtp.gmail.com with ESMTPSA id q187sm1323287qke.42.2016.07.05.13.48.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 05 Jul 2016 13:48:17 -0700 (PDT) From: Markus Mayer To: Andrew Morton , Al Viro , Rasmus Villemoes , Chris Metcalf , Kees Cook Cc: Markus Mayer , dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-acpi@vger.kernel.org, speakup@linux-speakup.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/7] lib: string: add functions to case-convert strings Date: Tue, 5 Jul 2016 13:47:05 -0700 Message-Id: <1467751631-22878-2-git-send-email-mmayer@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1467751631-22878-1-git-send-email-mmayer@broadcom.com> References: <1467751631-22878-1-git-send-email-mmayer@broadcom.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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);