Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3700424rwb; Sun, 9 Oct 2022 09:29:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Nuk5lHfpoetbR2q0yGuX98P4ijO/wtUsx2q/MEsuPO2AtJIYArheJ4y9AlUAdUBWdLXTO X-Received: by 2002:a05:6402:51d1:b0:45c:1ba1:b7e7 with SMTP id r17-20020a05640251d100b0045c1ba1b7e7mr1145923edd.57.1665332948192; Sun, 09 Oct 2022 09:29:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665332948; cv=none; d=google.com; s=arc-20160816; b=NwycGyi7flKmfsnAkAUJutyqD0SL/ozS9mMDJwjKUAnLoJlMu9jfxeV6ocmqNSR4Bt YU9PYIEZf8ygVy6R/GeRNK+2/vP/6ZX+90HVwSeyMw1nsk7OOSuMAG2l8ErYsUR/o6zb Plw5Oc7awg774eTTruJt5mckuc5d3FsQTg0B5AsP1mdbRpEkogUL3z/Y80BxO9OJBnoN sJNZ1Alo54iZI0HG+NZS1bdPDUSLQbbhobLqB3mpySJBu9ictd6Be1SMesTDU7LO3CAC xL8c/L0PUvxriohGxALdAIzNp4K0yBoV8aHJjw6qRMfmVlu7LsJ9DhQUQ0sVzPEh8m+f OUJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=llJW55rbiZfo1EtwOoSpQSjqRd/+/s2Bb69ydHpO+tA=; b=A/9RAj2/itX5cmLRiS4RqzJ3LDGJrDKZlkN2WHCptCZasXyv6pA4NNc5FVWEhKKWtn vtuTB4OzDK8PNG9Sx7QX+KvZtlqhQedBC6UHpAy4SeTXFX/erhzGCoLVTG/ICITE6TBN XsSUA78a+Urk+objHbPlN+Yd41W/grFHoSY++MEtWBAnbkKYX9Eo0sq/XUzWkV97W5o+ 7daEVJyNc+WXMsYb0L5b/vLIZEcF5vyYyLeNX7JeAQzA4xWZdBAylkuC5J0Dkyk0HV/3 zFYBEUOfbPnwuEH9H1GkQkNztpwLDll2MsI3XzDp4XebVlZumTKf3AuTXUQfTnmyP5Ht Y8pA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=lohu.info Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g9-20020a056402090900b00454412dc7absi9520537edz.259.2022.10.09.09.28.42; Sun, 09 Oct 2022 09:29:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=lohu.info Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229937AbiJIQWm (ORCPT + 99 others); Sun, 9 Oct 2022 12:22:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230037AbiJIQWk (ORCPT ); Sun, 9 Oct 2022 12:22:40 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.221.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D84B6275E8 for ; Sun, 9 Oct 2022 09:22:38 -0700 (PDT) X-QQ-mid: bizesmtp63t1665332548tes0qihh Received: from localhost.localdomain ( [58.247.70.42]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 10 Oct 2022 00:22:27 +0800 (CST) X-QQ-SSF: 01100000002000G0Z000B00A0000000 X-QQ-FEAT: lJ9dImo9Gpf3qGfbbuHdLhphkStRzvkHM9DXOuUIZfz36X3NYtmefNP6oLQqq wNuMooggk9A2IuyJ1zXGo7vuKqhCkZGh7mhUYYEdGlOCGKx28tzZOFFl08XlnnJ1TTzln8p l1dr9FuZFm+uUDWJdUIEfSWodW5dugGvDsM+o6jZgv8n8sfDeKYW5mmyAJMrtk3PX3TB0Xe dVyRoc7/SH6g703CLELjDr9mHT5C2gBSzY3sFlSCFo4PvqrBpv3X74b9ehbOUax28PP6NFD Gwzv0Sao995Mk+n7+UlToHzZHhbDWH9q79ydy7/6E+00IS7xgEJGbf6qoPmyZ2eV2F/QbVX s0t+HX4KIJcpxa5T/KQxUqNhfGZ+T3A5ncbVFuvIqo9H1RK1Cb7Pff0XqrjqA== X-QQ-GoodBg: 0 From: Soha Jin To: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" , Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus , linux-kernel@vger.kernel.org, Soha Jin Subject: [PATCH 1/3] string: add match_string_nocase() for case-insensitive match Date: Mon, 10 Oct 2022 00:21:53 +0800 Message-Id: <20221009162155.1318-2-soha@lohu.info> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221009162155.1318-1-soha@lohu.info> References: <20221009162155.1318-1-soha@lohu.info> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:lohu.info:qybglogicsvr:qybglogicsvr3 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Sometimes we want to make a case-insensitive comparison with strings, like checking compatible devices in fwnode properties, so this commit abstracts match_string to __match_string with a compare function. The original match_string will call __match_string with strcmp, and the new match_string_nocase will call it with strcasecmp. Signed-off-by: Soha Jin --- include/linux/string.h | 31 ++++++++++++++++++++++++++++++- lib/string_helpers.c | 10 ++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index cf7607b32102..fcfa67f598f5 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -183,9 +183,38 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp); extern void argv_free(char **argv); extern bool sysfs_streq(const char *s1, const char *s2); -int match_string(const char * const *array, size_t n, const char *string); +int __match_string(const char * const *array, size_t n, const char *string, + int (*cmp)(const char *, const char *)); int __sysfs_match_string(const char * const *array, size_t n, const char *s); +/** + * match_string - matches given string in an array + * @_a: array of strings + * @_n: number of strings in the array + * @_s: string to match with + * + * Helper for __match_string(). Look for a string in an array of strings up to + * the n-th element in the array with a case-sensitive compare. + * + * Return: + * index of a @string in the @array if matches, or %-EINVAL otherwise. + */ +#define match_string(_a, _n, _s) __match_string(_a, _n, _s, strcmp) + +/** + * match_string_nocase - matches given string in an array + * @_a: array of strings + * @_n: number of strings in the array + * @_s: string to match with + * + * Helper for __match_string(). Look for a string in an array of strings up to + * the n-th element in the array with a case-insensitive compare. + * + * Return: + * index of a @string in the @array if matches, or %-EINVAL otherwise. + */ +#define match_string_nocase(_a, _n, _s) __match_string(_a, _n, _s, strcasecmp) + /** * sysfs_match_string - matches given string in an array * @_a: array of strings diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 230020a2e076..52949adfdfe4 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -910,10 +910,11 @@ bool sysfs_streq(const char *s1, const char *s2) EXPORT_SYMBOL(sysfs_streq); /** - * match_string - matches given string in an array + * __match_string - matches given string in an array * @array: array of strings * @n: number of strings in the array or -1 for NULL terminated arrays * @string: string to match with + * @cmp: compare function * * This routine will look for a string in an array of strings up to the * n-th element in the array or until the first NULL element. @@ -926,7 +927,8 @@ EXPORT_SYMBOL(sysfs_streq); * Return: * index of a @string in the @array if matches, or %-EINVAL otherwise. */ -int match_string(const char * const *array, size_t n, const char *string) +int __match_string(const char * const *array, size_t n, const char *string, + int (*cmp)(const char *, const char *)) { int index; const char *item; @@ -935,13 +937,13 @@ int match_string(const char * const *array, size_t n, const char *string) item = array[index]; if (!item) break; - if (!strcmp(item, string)) + if (!cmp(item, string)) return index; } return -EINVAL; } -EXPORT_SYMBOL(match_string); +EXPORT_SYMBOL(__match_string); /** * __sysfs_match_string - matches given string in an array -- 2.30.2