From patchwork Sun Dec 15 23:02:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 22322 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 12977C32F6 for ; Sun, 15 Dec 2024 23:02:32 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BB31067F32; Mon, 16 Dec 2024 00:02:28 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="LCpaqpXv"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DBFF067F1D for ; Mon, 16 Dec 2024 00:02:25 +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 499B92C6; Mon, 16 Dec 2024 00:01:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1734303709; bh=uauMBSHP1hmbgl27o/OtM059YBY7Ft15XGKnQd3nRuw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LCpaqpXvmJInQ8Jdbpa/AKzyrJ1nbjwoiZyMOugtBeID6UCAdfkJgF0qb+UnB6Ray L/2ZSeKBvysgDam19XtC/5yzPlhtIbMFwtQnf+w3oVsp2e1EP/ddQqLYiOEI2HHd8M aa+TldSlwc4Kc7/aavkvLumNqDhS7buXYLcS7lSA= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH 2/8] libcamera: base: utils: Add string_view concatenation operators Date: Mon, 16 Dec 2024 01:02:00 +0200 Message-ID: <20241215230206.11002-3-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" The operator+() overloads to concatenate std::string and std::string_view instances have been introduced in C++26 only. Add a local implementation in utils.cpp, conditioned by a meson check for availability in the standard C++ library. Signed-off-by: Laurent Pinchart --- include/libcamera/base/utils.h | 47 ++++++++++++++++++++++++++++++++++ src/libcamera/base/meson.build | 12 +++++++++ test/utils.cpp | 34 ++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index 780aeda6a0ce732a..dd012fd58501cd8d 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -433,6 +433,53 @@ private: template std::basic_ostream &operator<<(std::basic_ostream &os, const utils::Duration &d); + +#if not HAVE_STD_STRING_VIEW_OPERATOR_PLUS +template +std::basic_string +operator+(const std::basic_string &lhs, + std::basic_string_view rhs) +{ + std::basic_string str; + + str.reserve(lhs.size() + rhs.size()); + str.append(lhs); + str.append(rhs); + + return str; +} + +template +std::basic_string +operator+(std::basic_string_view lhs, + const std::basic_string &rhs) +{ + std::basic_string str; + + str.reserve(lhs.size() + rhs.size()); + str.append(lhs); + str.append(rhs); + + return str; +} + +template +std::basic_string +operator+(std::basic_string &&lhs, + std::basic_string_view rhs) +{ + return std::move(lhs.append(rhs)); +} + +template +std::basic_string +operator+(std::basic_string_view lhs, + std::basic_string &&rhs) +{ + return std::move(rhs.insert(0, lhs)); +} +#endif /* HAVE_STD_STRING_VIEW_OPERATOR_PLUS */ + #endif } /* namespace libcamera */ diff --git a/src/libcamera/base/meson.build b/src/libcamera/base/meson.build index a742dfdfeb24f9a9..1643d6034921a0cb 100644 --- a/src/libcamera/base/meson.build +++ b/src/libcamera/base/meson.build @@ -41,6 +41,18 @@ if libunwind.found() config_h.set('HAVE_UNWIND', 1) endif +if cxx.compiles(''' +#include +#include + +int main() +{ + std::string s = std::string() + std::string_view(); + return s.size(); +}''', name : 'concatenation of std::string and std::string_view') + config_h.set('HAVE_STD_STRING_VIEW_OPERATOR_PLUS', 1) +endif + libcamera_base_deps = [ libatomic, libdw, diff --git a/test/utils.cpp b/test/utils.cpp index d25475cb93b96a0a..29e3c26406b4f72e 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -170,6 +170,36 @@ protected: return TestPass; } + int testStringView() + { + std::string s{ "Hello" }; + std::string_view sv{ "World!" }; + + if (s + sv != "HelloWorld!") { + cerr << "operator+(const std::string &, std::string_view) test failed"; + return TestFail; + } + + if (sv + s != "World!Hello") { + cerr << "operator+(std::string_view, const std::string &) test failed"; + return TestFail; + } + + if (std::move(s) + sv != "HelloWorld!") { + cerr << "operator+(std::string &&, std::string_view) test failed"; + return TestFail; + } + + s = "Hello"; + + if (sv + std::move(s) != "World!Hello") { + cerr << "operator+(std::string_view, std::string &&) test failed"; + return TestFail; + } + + return TestPass; + } + int run() { /* utils::hex() test. */ @@ -307,6 +337,10 @@ protected: if (testDuration() != TestPass) return TestFail; + /* std::string_view operator+() test. */ + if (testStringView() != TestPass) + return TestFail; + return TestPass; } };