Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp353681rdb; Tue, 23 Jan 2024 01:26:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IEqweMytrzgjTFitNklL8zh2M3iAbWOujCeyUYTXt3H4moWXz6fJwlEld/aTn5HD2cUlwQw X-Received: by 2002:a17:906:d52:b0:a30:b7a5:c34e with SMTP id r18-20020a1709060d5200b00a30b7a5c34emr380801ejh.17.1706001971491; Tue, 23 Jan 2024 01:26:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706001971; cv=pass; d=google.com; s=arc-20160816; b=Qa78Xnh+k4pZ3I/7l+mdSMsRaA/Txik4jpeEiRvQfK4e4dPy4pWU9xU97SRM/APX93 vdCpks6bA4U74pDa9VFufBtu9MDtzHqY37eDMNyVJ0DvPYVrlv8YiyMx96YokKS8zduu bYaJlWO30WRZkd0Ou/2n16F1pBKgxHmBZ5USv1i38ZBJhqwRCGEzvzJc0lVEYKCwDOyr B/QterJXDDuxzuotp1fPnCRC1zmQu5bWhZoyUMzcfXyPiMqQYYlm7BQ6/RdUv1rSvr2Q Pdh3emkxxN1ZBqY4RkPJ5apQwdTRHSmGOgkEgamSdYk8+zrH4ekVIpB1D/IC1+a4xdPj rsgw== 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=Ing89cY/F6bCC1I7CF6ia5gCQf67f43F4dJqnpYJp20=; fh=Us/DORbzHj5XmgnBeJW+VlCBdo2pSXKHAejHonylCvY=; b=thxB3X98HMh29Qxh+h3fH6fZttf7/Nqe5pYbMzKXZv4XXyEJdbP4jjSObbPakXG8fG nLfBA5YxXzh+3jnJD2ycESzDpN8zhTzKXGBCHfmN3E8rVuCHmgV9EiVHogvKspFjCVK8 8xJU3UBgqT6liku57jIEFYepHB8eW3jDh18M55uqARTZ4dEUE5F77qneWMU805IskP83 WYBFDp2koBdjeVlRfUL9kxa8IFT4QG0sZ8ng+CtA4/QDmjIXt94C+T8k7MPDyf3QE+Tn 2SGjcTtQbDIY4zGXEHiYPinkC6k8DIzVynkZD743UZyaq0Uj6T33VVyyISeemsslDqpW rbKQ== 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-34979-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34979-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id j26-20020a170906255a00b00a2bf9d25f4dsi11592367ejb.525.2024.01.23.01.26.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 01:26:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34979-linux.lists.archive=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-34979-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34979-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 am.mirrors.kernel.org (Postfix) with ESMTPS id A176A1F27DA7 for ; Tue, 23 Jan 2024 09:19:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A9D1759B4B; Tue, 23 Jan 2024 09:19:16 +0000 (UTC) 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: 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) 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 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 + * @index: The index into @str of where @suffix is if found (NULL to ignore) + * + * 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 = strlen(suffix); + size_t str_len = strlen(str); + + if (len > str_len) + return 0; + + if (strncmp(str + str_len - len, suffix, len)) + return 0; + + if (index) + *index = str_len - len; + + return len; +} + #endif /* _LINUX_STRING_H_ */ -- 2.31.1