Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp460361rdb; Mon, 22 Jan 2024 09:20:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IG3bNZMYPeicye7x9Ev23UyKuEjuIiVp8410AUOfIoB1fpYpvAUF2FFzoHS5mNWvD7zg9rz X-Received: by 2002:a17:902:bf43:b0:1d6:f240:91eb with SMTP id u3-20020a170902bf4300b001d6f24091ebmr4167425pls.105.1705944035509; Mon, 22 Jan 2024 09:20:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705944035; cv=pass; d=google.com; s=arc-20160816; b=jZfGKv3ueeMOFdSVb8/iL6Yz7BOp78sTKf8VxOJzpril2wNvMvjCfBSpOLzLlphqPC 3VYapceAVIS46e9/XmdGX/IRwrXcOG9hnUWScc7O3caMUU3dN18NWjVBCu+QDsvogW52 bFzW4pB3a5GElJxqKV3n5Lfa5QWZo0aSj/rs4OAOb2YkvnJnHi9isA4m+B8TxGGLv93z IpsS1ROcXeI5pyqiwJp5dic80PQam6Vyj+jm4bfqQnKeYIplwTYPgTms0glkN3OP2ayi lLtESd49UY0Fg8dcXUFrGjS5SDZdYIE5DNliNNJy1RBjw348Cq0ce9dilOpfbdtosFz9 P+1g== 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 :subject:cc:to:from:date; bh=tTNZNj87HcD1yy/UMj2iDTMIpxCB2Y+qNJf7fjq5KVk=; fh=FddkWE8y9gfCaHrvhKVjcZeFb5K9uBw6d3r/INVh6IE=; b=ToLAV6rVyUC7GT/gUEbb1dzQk67XWHBa3KbJicOdIIMHcpPimOQl4M+Wsj2AHVIboZ 9zjvpH8VG41t4NnfyehpQ560Vq+kAtcoNdmUd4y+CczIpGSxe4IcHrgIvt2f5Vz72EKs ysxDacm1KdVgCLjmQlSYt8upoQ09cxhKG01qPt49XbPrnYQ94WoIbEb5Q1KksxndZX6i G9yiXGUWHyRz+bbVsOzg+XgLkHf9D8gjCGuvpWfjG7viQooKmWu3wHk5Kn5SRJsFBd+Q nNeP9LHOi0GAeDI8Wxocc9qrR7roXXgy90/GXFT6lAsT9bEecbjcDLghRFwiXgcRxofm b6Cg== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-33656-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33656-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s15-20020a170902ea0f00b001d6f67afa3bsi5519539plg.113.2024.01.22.09.20.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 09:20:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33656-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-33656-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33656-linux.lists.archive=gmail.com@vger.kernel.org" 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 25BDE285DFB for ; Mon, 22 Jan 2024 17:17:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1F771651A4; Mon, 22 Jan 2024 16:06:33 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9D7913EA92; Mon, 22 Jan 2024 16:06:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705939592; cv=none; b=ruuOrI2bMpWcCmy3wV/5g/qSqZBJAuO+mKhB1m+wTneEMyNvvFSlD+xhcBFR3XgliYt3JZLNiLD2McQD41Uh1ZXeIQt+O1ll2aJ6mpPQ2mr5HPNIDg9NJKBb2XXl13UMZYYwcv+z9vMYUgqGNaEvsezojoF6IclCOatjGvygiU8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705939592; c=relaxed/simple; bh=/L2YrNxPOfocGWcSIQklbfEgmVRKuWexr+x6ZjSRXzM=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JJGqSZ653Ah5yCicAUax+WXgineCF8HIRp0EtW7KovbBcDZhM+GYHM4ooL+DfmDsSZe0jWHEWFWWnwebrcp9oXmqRngltsoVIypCb83tHhTiqVhOURAL0FDnekRg29nYSrko1RtFu74Ou7PVCbOe/ZfJMSluFdXyC8xd+6r0F+U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7C547C433F1; Mon, 22 Jan 2024 16:06:31 +0000 (UTC) Date: Mon, 22 Jan 2024 11:07:59 -0500 From: Steven Rostedt To: Ye Bin Cc: , , , Subject: Re: [PATCH v2 1/7] string.h: add str_has_suffix() helper for test string ends with specify string Message-ID: <20240122110759.29c50a19@gandalf.local.home> In-Reply-To: <20240122074015.4042575-2-yebin10@huawei.com> References: <20240122074015.4042575-1-yebin10@huawei.com> <20240122074015.4042575-2-yebin10@huawei.com> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 > + > #endif /* _LINUX_STRING_H_ */