From patchwork Wed Aug 17 20:03:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 12946386 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 039BFC32772 for ; Wed, 17 Aug 2022 20:03:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=80sEQVT5TxeJ/vIL+UdKhA5vhpjBpxY4VMoHGYobL2g=; b=EUdfD3QSHrqeKX oSabbzbN4jTzzVJ435LddtZXFUrkp5wARVAh1Qad5qY/URnlf5tx6Vx1dFq6FjpM5bORbvjKi6YBx XnxK4HETIldTjDWMUJGbfOkb+49Nzo8h0R16Rx791EWGpCjF3DcrcMW6b1/IJM4IKk8c+dJM7anns 5uVFWiPTZZ4Uk7blbisV6nGhK9Y/opq0lrCt3/MaDteH2D99tnFKusj8lJ87D6tMmleUnCj2mLtBB uJ5/0K3asOdKdiE4Jx+o+HEGcNVioA5eF8JPNiZuR3YV5t/wAOZE5LQpgellggnIRwDL70UUpMbrZ QCXitcyVc/knxdb/22+g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOPGX-0065Mk-CL; Wed, 17 Aug 2022 20:03:49 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOPGU-0065KR-3S for linux-phy@lists.infradead.org; Wed, 17 Aug 2022 20:03:47 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D4CD56151B; Wed, 17 Aug 2022 20:03:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBB2FC4347C; Wed, 17 Aug 2022 20:03:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660766623; bh=wv0Zebl3/M8/sl1p7uqmjBF5ITW2e0LsrTBPWFRbNuA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IybX3rRa0kAATdNa9CEutWHHd6nJNB/y5T4vkHUm4SCBxsG8J6o2lnKb5ITB9GZwT WOunqvHZDReF98jHFUrT4omATeTvB3UCsg/rZDPxcxrrDrtzAcum1B8j4gTkqj0NLe en9O7rOnvqqyggowmKPE4BeGmXuDlKMcTntGHaJ3PwQamtksLtjybpR/aPxmhpwVMg pA2qJizPk36y4j50+h95mxHjC0e9BVblhawKMwatNS0D4RpoUMf6u6kUMoXnrzZfTH 6NXMp7SKRpNPotFkq14UcIQPtJFT6KTkDsDWLgb0zPPUVkbS/L4c+4E5yToxy65t3j CT26aaydcxB2A== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Vinod Koul , Kishon Vijay Abraham I , Linux Phy , Kees Cook , Andy Shevchenko Cc: Daniel Scally , Gregory Clement , Heikki Krogerus , Sakari Ailus , linux-kernel@vger.kernel.org, pali@kernel.org, josef.schlehofer@nic.cz, =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH linux-phy v2 1/4] string.h: Add str_has_proper_prefix() Date: Wed, 17 Aug 2022 22:03:32 +0200 Message-Id: <20220817200335.911-2-kabel@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220817200335.911-1-kabel@kernel.org> References: <20220817200335.911-1-kabel@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220817_130346_212180_3C664522 X-CRM114-Status: GOOD ( 11.56 ) X-BeenThere: linux-phy@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux Phy Mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-phy" Errors-To: linux-phy-bounces+linux-phy=archiver.kernel.org@lists.infradead.org Add str_has_proper_prefix(), similar to str_has_prefix(), but requires that the prefix is proper: the string itself must be longer than the prefix. Signed-off-by: Marek BehĂșn --- Andy, Kees, could you ack this if it is ok? --- include/linux/string.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 61ec7e4f6311..375f51b9182c 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -316,4 +316,22 @@ static __always_inline size_t str_has_prefix(const char *str, const char *prefix return strncmp(str, prefix, len) == 0 ? len : 0; } +/** + * str_has_proper_prefix - Test if a string has a proper prefix + * @str: The string to test + * @prefix: The string to see if @str starts with + * + * This is like str_has_prefix(), but fails if the strings are equal. + * + * Returns: + * * strlen(@prefix) if @str starts with @prefix and they aren't equal + * * 0 otherwise + */ +static __always_inline size_t str_has_proper_prefix(const char *str, + const char *prefix) +{ + size_t len = strlen(prefix); + return strncmp(str, prefix, len) == 0 && len != strlen(str) ? len : 0; +} + #endif /* _LINUX_STRING_H_ */