[v2,1/7] string.h: add str_has_suffix() helper for test string ends with specify string
Message ID | 20240122074015.4042575-2-yebin10@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-32434-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2416611dyb; Sun, 21 Jan 2024 23:38:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IG1JoEuzvZ4O2aIWLpijb4p1UcRDTA8JgGhLitELC8Bu9Pr4D0x4EZlwqlWFKdO+ERIM71E X-Received: by 2002:a05:6402:28f:b0:559:9da5:19f6 with SMTP id l15-20020a056402028f00b005599da519f6mr1847249edv.19.1705909092574; Sun, 21 Jan 2024 23:38:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705909092; cv=pass; d=google.com; s=arc-20160816; b=lROdEzhkEO7rif1Ip7fAsJr/Lh8iASHM+zslS00rUi3vzqEjkD4RoZHjvv4gLXLlaJ faBWlLgj9fvCOh+St4ssOL7EK/lq+VyW+UtH2p9V+jGCkPTVlbz8rv+FiLcgy4zSKUEw VM6TOeDo3/++y7Q10Gk00/qVhe9CWLlCrv9hDqIxH0W2rDKUY5kggFv0AHPQaoggTMJt yzgggVyVoAH3bf86ipFnQjpSeBrlzx7PmM8aT/zy76bLpuGVqlLy/FDT3Nn+HQRKNLgw t39n2O9meAHpdJFYVGFW8BgmF6xbtCigrY8DmpFV/mlSnoECbqFd4hyaekW3gmYo3fRu wFMQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=xOlOUqAMj8ivc8bsmDQh2fWmsKd9HFqcyqEb2t7uYL8=; fh=Us/DORbzHj5XmgnBeJW+VlCBdo2pSXKHAejHonylCvY=; b=O2AhKA0b6mf89az28WjB4PWTn62LK7NA4q86W4MNfFYvwandipaEJQlZSXDc0wQYrG c8RwVB34KKYwnj4Uema+c8If9pUv4Csidg6qdowcBOfL5Bh5fxXSzJQadJ3IoEXXD6Ac 1zUNV4J5fTaImcIzhTPpPqK+4U6/AiI6VabbEeEeQx/3JEm0Ep4OInOD2rpAoyEXX6ZW dwY7L54Zu1+m0X8ymtywC/IskmZNTs0EKZjK2zLYfoZQwqWaS9cKAWnK+CCv2o+QJavV tNCTiZFDYe9ti1/X/qUA+8ibKhgrFgkARs2wfp7xETq3rAnmg94rFbdfqt/yB8JIhdHz vCdQ== 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-32434-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32434-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q35-20020a05640224a300b0055400f1f188si11187003eda.41.2024.01.21.23.38.12 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 23:38:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32434-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; 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-32434-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32434-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 331571F228AE for <ouuuleilei@gmail.com>; Mon, 22 Jan 2024 07:38:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C2DC8179BA; Mon, 22 Jan 2024 07:37:53 +0000 (UTC) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (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 2319D16416; Mon, 22 Jan 2024 07:37:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705909072; cv=none; b=tCJpB3ZWssMxwMwmSiSpq/4HokrxhUzH4LgVxw6N2X8Fqutv/2JY5GIb4wMrU7b8HlGHBi3u2BwjTgUFFMQEqqJHolhp/QA/eOEH6gUWiSo9TkP/GgjkPHchobEiBKZjZgcQAfytMnblyLOEOOJXOwwsg4QPgxWQG6mA4yJTevo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705909072; c=relaxed/simple; bh=sBwc0e0LX68ujIGPPtdpEdCtFeOhchnf4ztfTmcCrW4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PqdnJLFmR2y4yr6IFpsaa/yKHdTSXPzt3OZqQYX9CUSh8sw5QdTNl2T7fct/lxsUj66XLtH47+7utJiXYORZNx5BSTyOXtgMNRSOsgD1/hmDTl5dJpU+sn70ozaFgIDgu9NG9Z1uqGmlHcxpiZgS1pnXZ3tTfsim5bhHxPrf+jk= 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.188 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.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4TJMVC181kzVjFn; Mon, 22 Jan 2024 15:36:35 +0800 (CST) Received: from canpemm500010.china.huawei.com (unknown [7.192.105.118]) by mail.maildlp.com (Postfix) with ESMTPS id 49E9D1400E4; Mon, 22 Jan 2024 15:37:44 +0800 (CST) Received: from huawei.com (10.175.127.227) 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; Mon, 22 Jan 2024 15:37:39 +0800 From: Ye Bin <yebin10@huawei.com> To: <rostedt@goodmis.org>, <mhiramat@kernel.org>, <mathieu.desnoyers@efficios.com>, <linux-trace-kernel@vger.kernel.org> CC: <linux-kernel@vger.kernel.org>, <yebin10@huawei.com> Subject: [PATCH v2 1/7] string.h: add str_has_suffix() helper for test string ends with specify string Date: Mon, 22 Jan 2024 15:40:09 +0800 Message-ID: <20240122074015.4042575-2-yebin10@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240122074015.4042575-1-yebin10@huawei.com> References: <20240122074015.4042575-1-yebin10@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500010.china.huawei.com (7.192.105.118) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788775332512687409 X-GMAIL-MSGID: 1788775332512687409 |
Series |
support '%pd' and '%pD' for print file name
|
|
Commit Message
Ye Bin
Jan. 22, 2024, 7:40 a.m. UTC
str_has_suffix() is test string if ends with specify string.
Signed-off-by: Ye Bin <yebin10@huawei.com>
---
include/linux/string.h | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
Comments
On Mon, 22 Jan 2024 15:40:09 +0800 Ye Bin <yebin10@huawei.com> wrote: > str_has_suffix() is test string if ends with specify string. > > Signed-off-by: Ye Bin <yebin10@huawei.com> > --- > 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 > + > #endif /* _LINUX_STRING_H_ */
On 2024/1/23 0:07, Steven Rostedt wrote: > On Mon, 22 Jan 2024 15:40:09 +0800 > Ye Bin <yebin10@huawei.com> wrote: > >> str_has_suffix() is test string if ends with specify string. >> >> Signed-off-by: Ye Bin <yebin10@huawei.com> >> --- >> 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_ */ > . >
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; +} + #endif /* _LINUX_STRING_H_ */