Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp227250rdb; Mon, 22 Jan 2024 18:55:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IE5cBAlGz6i/ADhwzZVldsAPQWj+nZcFhChYsXqvNfxzlba9W23cz8tUBx2JWE2WAu4I31R X-Received: by 2002:a17:903:2446:b0:1d5:c77c:1baf with SMTP id l6-20020a170903244600b001d5c77c1bafmr3309973pls.92.1705978507774; Mon, 22 Jan 2024 18:55:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705978507; cv=pass; d=google.com; s=arc-20160816; b=m9DsxaPIbT8YmHuT0D9Ba+ry75MuipMNqT8pRJsM0ECyxtds1ocKuVLbImkthSWPFU 4vJBnIB2omoC3vJaJ9xRN9J2jE4QAU4uGm5dImkNIYlp5UbbzA9dnd3hVmIsinra5GA2 QiWBBSPoXODidhFd7VgrYmQDGimXJdUP8aH2jc4ae4fo24ghWS+WsQgaRiE158wrZNYP wZV/WY3IBozD4frvqUaZmUiZAok0yWaMhAQrnIFJjTRliT7DIYMxuYBAU8ngzat5O2w4 mmBV+qtc+oWcKDg4euk2y99LtfovranOjB2iknotJvd5hZbw5GkZ4+Qme1Lvbd1JFBYQ nxcA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:date:message-id:from :cc:references:to:subject; bh=dXF6IXKz6AQ8R4ZDvYZl7kBZtjSQ9QqZSRAsXS4bSfQ=; fh=ipiWiXIDvPejCHFaYsAzNBcuzWLUm6DkBUTeernJ/6M=; b=sT87FLz3wDr4PKEmWcTC/8W4Cl78NQEd0tzJJgCD7BPdw/q/LwkbZCRk9A5tpzQiys 8STRx4QWZ6ievwBt7mUtc/nZ0Mq1c9nigOghxFGBIBsxTKnIb3JwCBFGJKsSEBTB32LV 3d/BfRBOE4xC1m5upZ4jlrGQxHBiiN5MEc+NEX8oG5iIMoBRJTFM+oAJGV85Ej6tJu3r OPV5XdFafmNZi1lPlPalEO4Y4c1TJMgl3GG/2f1EydN6kXrlTqH10Fm6qdtxUAhJGAiG CKLYFbMQCUwtb2LBBn8IBNzDAB/dbS3hL3u0f3Zq7YVtd7uT2lfM1DCWH5yueIDrKp12 eJwg== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-34643-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34643-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id q1-20020a170902bd8100b001cffca283fasi8955047pls.144.2024.01.22.18.55.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 18:55:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34643-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-34643-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34643-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id ABD73282089 for ; Tue, 23 Jan 2024 02:44:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F060D52F9F; Tue, 23 Jan 2024 02:36:58 +0000 (UTC) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4D1C52F90; Tue, 23 Jan 2024 02:36:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705977418; cv=none; b=V5nCyEp7pq+Td8OOpDR4h7qRbcByC1aXoCoNxy1xHpGoMX0zM9Ba2mxjbOs1yeSREmMAEbiLXzzEwPz33/h+JGT3U/vSYpQgue+SF4Ob5b7GJrnFZHpwtBHrlFlQjuYbrWiCA5ccK9NKgFIRiSbmzf4rgZHYUn5voNYjmKPV7CI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705977418; c=relaxed/simple; bh=ZzHhTy4oVqbqk4rjHHtCAYK6n4KCr+YmeWtTcRgougs=; h=Subject:To:References:CC:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=C2v0OyiB4IM0fxaY9cLIldTYBbCmpsBAOx286RTuy/txxBfaQ2claPQodzE8cHyQcspBDDBGUbA2KOpRDzxoH+nLolIGCNAnedj6v2z+s8OS86/rEqE45A1k/xGyA/QAB3rKxA3idy9piH/LW59M3PKB2E0pdRMla+EZ0Feospc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4TJrmx75ZRzNlNk; Tue, 23 Jan 2024 10:36:01 +0800 (CST) Received: from canpemm500010.china.huawei.com (unknown [7.192.105.118]) by mail.maildlp.com (Postfix) with ESMTPS id 6CBBE18005E; Tue, 23 Jan 2024 10:36:53 +0800 (CST) Received: from [10.174.178.185] (10.174.178.185) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 23 Jan 2024 10:36:37 +0800 Subject: Re: [PATCH v2 1/7] string.h: add str_has_suffix() helper for test string ends with specify string To: Steven Rostedt References: <20240122074015.4042575-1-yebin10@huawei.com> <20240122074015.4042575-2-yebin10@huawei.com> <20240122110759.29c50a19@gandalf.local.home> CC: , , , From: "yebin (H)" Message-ID: <65AF2634.2090705@huawei.com> Date: Tue, 23 Jan 2024 10:36:36 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <20240122110759.29c50a19@gandalf.local.home> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500010.china.huawei.com (7.192.105.118) On 2024/1/23 0:07, Steven Rostedt wrote: > On Mon, 22 Jan 2024 15:40:09 +0800 > Ye Bin wrote: > >> str_has_suffix() is test string if ends with specify string. >> >> Signed-off-by: Ye Bin >> --- >> include/linux/string.h | 20 ++++++++++++++++++++ >> 1 file changed, 20 insertions(+) >> >> diff --git a/include/linux/string.h b/include/linux/string.h >> index 433c207a01da..e47e9597af27 100644 >> --- a/include/linux/string.h >> +++ b/include/linux/string.h >> @@ -405,4 +405,24 @@ static __always_inline size_t str_has_prefix(const char *str, const char *prefix >> return strncmp(str, prefix, len) == 0 ? len : 0; >> } >> >> +/** >> + * str_has_suffix - Test if a string has a given suffix >> + * @str: The string to test >> + * @suffix: The string to see if @str ends with >> + * >> + * Returns: >> + * * strlen(@suffix) if @str ends with @suffix >> + * * 0 if @str does not end with @suffix >> + */ >> +static __always_inline size_t str_has_suffix(const char *str, const char *suffix) >> +{ >> + size_t len = strlen(suffix); >> + size_t str_len = strlen(str); >> + >> + if (len > str_len) >> + return 0; >> + >> + return strncmp(str + str_len - len, suffix, len) == 0 ? len : 0; >> +} > I understand why you are returning the length of the suffix, as it matches > str_has_prefix(). But as the user of this will likely need the length of > the "str" too, we should either return the length of the string, or the > index of where the suffix was found. > > /** > [..] > @index: The index into @str of where @suffix is if found (NULL to ignore) > [..] > */ > static __always_inline size_t str_has_suffix(const char *str, const char *suffix, size_t *index) > { > size_t len = strlen(suffix); > size_t str_len = strlen(str); > > if (len > str_len) > return 0; > > if (index) > *index = str_len - len; > > return strncmp(str + str_len - len, suffix, len) == 0 ? len : 0; > } > > -- Steve Thank you for your advice. Return the index of where the suffix was found is useful. I'll modify it according to your suggestion. -- Ye Bin >> + >> #endif /* _LINUX_STRING_H_ */ > . >