From patchwork Sun Dec 15 23:02:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 22325 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 753B1C32F6 for ; Sun, 15 Dec 2024 23:02:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E621D67F45; Mon, 16 Dec 2024 00:02:37 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OY6LTa9b"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 84F9967F1D for ; Mon, 16 Dec 2024 00:02:29 +0100 (CET) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5025B2C6; Mon, 16 Dec 2024 00:01:53 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1734303713; bh=jicbLubGAelOBp5iooZvoFoBWq8tpZBmR9fVz8vVW0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OY6LTa9bGZ+zBaw3oEZNEXATBsFb+pXH1n+RW8EwCDP+mztEp1m4+59EXI6iz4Paa pgxVOefYaQ6DgFVQTYvUbYf25NPz1CRQbwWOvQeOEiJQcGu095CI0VZ2i10KNJ4T3/ CKbu0kXW+ch0hnU3lKDxLySiBkR1pvS0GH3MAM4s= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH 5/8] libcamera: base: utils: Use std::string_view Date: Mon, 16 Dec 2024 01:02:03 +0200 Message-ID: <20241215230206.11002-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> References: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Replace usage of const std::string references passed to class member functions with std::string_view. This allows using static C string literals in the callers without the overhead of constructing a std::string instance. As std::string can't be implicitly constructed from std::string_view, an explicit construction is added at the end of dirname(). Signed-off-by: Laurent Pinchart --- include/libcamera/base/utils.h | 15 ++++++++------- src/libcamera/base/utils.cpp | 14 +++++++------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index dd012fd58501cd8d..3ef1c0e87383f8d2 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,7 @@ namespace utils { const char *basename(const char *path); char *secure_getenv(const char *name); -std::string dirname(const std::string &path); +std::string dirname(std::string_view path); template std::vector map_keys(const T &map) @@ -143,7 +144,7 @@ size_t strlcpy(char *dst, const char *src, size_t size); #ifndef __DOXYGEN__ template -std::string join(const Container &items, const std::string &sep, UnaryOp op) +std::string join(const Container &items, std::string_view sep, UnaryOp op) { std::ostringstream ss; bool first = true; @@ -162,7 +163,7 @@ std::string join(const Container &items, const std::string &sep, UnaryOp op) } template -std::string join(const Container &items, const std::string &sep) +std::string join(const Container &items, std::string_view sep) { std::ostringstream ss; bool first = true; @@ -181,7 +182,7 @@ std::string join(const Container &items, const std::string &sep) } #else template -std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr); +std::string join(const Container &items, std::string_view sep, UnaryOp op = nullptr); #endif namespace details { @@ -189,7 +190,7 @@ namespace details { class StringSplitter { public: - StringSplitter(const std::string &str, const std::string &delim); + StringSplitter(std::string_view str, std::string_view delim); class iterator { @@ -238,9 +239,9 @@ private: } /* namespace details */ -details::StringSplitter split(const std::string &str, const std::string &delim); +details::StringSplitter split(std::string_view str, std::string_view delim); -std::string toAscii(const std::string &str); +std::string toAscii(std::string_view str); std::string libcameraBuildPath(); std::string libcameraSourcePath(); diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index bcfc1941a92a3d69..886aa9ecdc2bddfe 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -80,7 +80,7 @@ char *secure_getenv(const char *name) * * \return A string of the directory component of the path */ -std::string dirname(const std::string &path) +std::string dirname(std::string_view path) { if (path.empty()) return "."; @@ -116,7 +116,7 @@ std::string dirname(const std::string &path) pos--; } - return path.substr(0, pos + 1); + return std::string(path.substr(0, pos + 1)); } /** @@ -247,7 +247,7 @@ size_t strlcpy(char *dst, const char *src, size_t size) return strlen(src); } -details::StringSplitter::StringSplitter(const std::string &str, const std::string &delim) +details::StringSplitter::StringSplitter(std::string_view str, std::string_view delim) : str_(str), delim_(delim) { } @@ -278,7 +278,7 @@ std::string details::StringSplitter::iterator::operator*() const /** * \fn template \ - * std::string utils::join(const Container &items, const std::string &sep, UnaryOp op) + * std::string utils::join(const Container &items, std::string_view sep, UnaryOp op) * \brief Join elements of a container in a string with a separator * \param[in] items The container * \param[in] sep The separator to add between elements @@ -293,7 +293,7 @@ std::string details::StringSplitter::iterator::operator*() const */ /** - * \fn split(const std::string &str, const std::string &delim) + * \fn split(std::string_view str, std::string_view delim) * \brief Split a string based on a delimiter * \param[in] str The string to split * \param[in] delim The delimiter string @@ -305,7 +305,7 @@ std::string details::StringSplitter::iterator::operator*() const * \return An object that can be used in a range-based for loop to iterate over * the substrings */ -details::StringSplitter split(const std::string &str, const std::string &delim) +details::StringSplitter split(std::string_view str, std::string_view delim) { /** \todo Try to avoid copies of str and delim */ return details::StringSplitter(str, delim); @@ -319,7 +319,7 @@ details::StringSplitter split(const std::string &str, const std::string &delim) * * \return A string equal to \a str stripped out of all non-ASCII characters */ -std::string toAscii(const std::string &str) +std::string toAscii(std::string_view str) { std::string ret; for (const char &c : str)