From nobody Thu Dec 25 01:22:29 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (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 5844358134; Tue, 23 Jan 2024 09:19:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706001556; cv=none; b=pk7Q8aNUZ+MPpk8C+/tmpi9pCr6FwzsMcaiU5clVcr9w1LIVE6SYtJuUWZKcKo6RyfjKRsm6Jx3HWq061p0IuBpqVvRtFlYVEg6wnY/BD8gUY2mBNtsJUzLALYxu6s9xADfDINQ42kXhFG6RRE7j5B7gUB+a7KyQx5z8yRjSz5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706001556; c=relaxed/simple; bh=SiWtbyNZNzEz2T7i5SIjV//aX/niwPXUWmBVnXoNsP4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HIAR2wTADuePdmXaxQu6uwhOePOiA/O7dVTOaHzGi9njSgxegiuQVW9vxMMnHsxwvhQXICLCDbWyT/JuedkxiafMgbh0lviVigy6+oNLIfsJqELjFPHZ0A7mbxSQ1qVBJ3aXicmAlmqWH/Ymo+bX6lBmlJxgHspSo0YUJ4QZWMM= 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.187 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.88.194]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4TK1hK5R6nzvVDK; Tue, 23 Jan 2024 17:17:37 +0800 (CST) Received: from canpemm500010.china.huawei.com (unknown [7.192.105.118]) by mail.maildlp.com (Postfix) with ESMTPS id D7D851404FE; Tue, 23 Jan 2024 17:19:08 +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; Tue, 23 Jan 2024 17:19:03 +0800 From: Ye Bin To: , , , CC: , Subject: [PATCH v4 1/7] string.h: add str_has_suffix() helper for test string ends with specify string Date: Tue, 23 Jan 2024 17:21:33 +0800 Message-ID: <20240123092139.3698375-2-yebin10@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240123092139.3698375-1-yebin10@huawei.com> References: <20240123092139.3698375-1-yebin10@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500010.china.huawei.com (7.192.105.118) Content-Type: text/plain; charset="utf-8" str_has_suffix() is test string if ends with specify string, and also this API may return the index of where the suffix was found. Signed-off-by: Ye Bin --- include/linux/string.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 433c207a01da..2fb0f22237fe 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -405,4 +405,32 @@ static __always_inline size_t str_has_prefix(const cha= r *str, const char *prefix return strncmp(str, prefix, len) =3D=3D 0 ? len : 0; } =20 +/** + * 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 + * @index: The index into @str of where @suffix is if found (NULL to ignor= e) + * + * 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 *index) +{ + size_t len =3D strlen(suffix); + size_t str_len =3D strlen(str); + + if (len > str_len) + return 0; + + if (strncmp(str + str_len - len, suffix, len)) + return 0; + + if (index) + *index =3D str_len - len; + + return len; +} + #endif /* _LINUX_STRING_H_ */ --=20 2.31.1