From patchwork Tue Nov 12 18:56:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 21877 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 5D8B7C324C for ; Tue, 12 Nov 2024 18:56:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B15DF657F9; Tue, 12 Nov 2024 19:56:46 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="rRYiNxgs"; 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 72A7B657B0 for ; Tue, 12 Nov 2024 19:56:45 +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 A3379710 for ; Tue, 12 Nov 2024 19:56:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1731437792; bh=m9PV8VXaLwI58zeg/Mxcl9qs0d+JDzAxQNf2VQNNowk=; h=From:To:Subject:Date:From; b=rRYiNxgsGozfVV7mBv5gytzZ6ys+mo4pzObp60SsmFHBPL7ZDvBZIL3ryQxjQ+j+D 37RFairnnHJFBc4hmz9OW0QujUsiXG9ppkbGe1++7MVyX5dIdlOQkh9LNgQq56hF30 /nQZFoMIGlPvf5AiMKqDbRexa2vwMXOIGAIRNq9s= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH] libcamera: yaml_parser: Use std::from_chars() Date: Tue, 12 Nov 2024 20:56:37 +0200 Message-ID: <20241112185637.10232-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 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" std::from_chars(), introduced in C++17, is a fast, locale-independent string-to-arithmetic conversion function. The C++ standard library provides overloads for all integer types, making it a prime candidate to replace the manual handling of integer sizes in the YamlParser string to integer conversion. Compared to std::strtol(), std::from_chars() doesn't recognize the '0x' prefix or '+' prefix, and doesn't ignore leading white space. As the YamlParser doesn't require those features, std::from_chars() can be used safely, reducing the amount of code. C++17 also requires the standard C++ library to provide overloads for floating-point types, but libc++ does not implement those. The float and bool implementations of YamlParser::Getter::get() are therefore kept as-is. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- include/libcamera/internal/yaml_parser.h | 2 +- src/libcamera/yaml_parser.cpp | 168 ++++------------------- 2 files changed, 29 insertions(+), 141 deletions(-) base-commit: dcb90f13cf79d909d04dacf2cfbd256a4744347f diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index de452844fe0a..8c7916565946 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -224,7 +224,7 @@ private: Empty, }; - template + template struct Getter { std::optional get(const YamlObject &obj) const; }; diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 7c0b341a32e7..db256ec5b04d 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -7,6 +7,7 @@ #include "libcamera/internal/yaml_parser.h" +#include #include #include #include @@ -146,151 +147,38 @@ YamlObject::Getter::get(const YamlObject &obj) const return std::nullopt; } -namespace { - -bool parseSignedInteger(const std::string &str, long min, long max, - long *result) +template +struct YamlObject::Getter || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v>> { - if (str == "") - return false; + std::optional get(const YamlObject &obj) const + { + if (obj.type_ != Type::Value) + return std::nullopt; - char *end; + const std::string &str = obj.value_; + T value; - errno = 0; - long value = std::strtol(str.c_str(), &end, 10); + auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), + value); + if (ptr != str.data() + str.size() || ec != std::errc()) + return std::nullopt; - if ('\0' != *end || errno == ERANGE || value < min || value > max) - return false; + return value; + } +}; - *result = value; - return true; -} - -bool parseUnsignedInteger(const std::string &str, unsigned long max, - unsigned long *result) -{ - if (str == "") - return false; - - /* - * strtoul() accepts strings representing a negative number, in which - * case it negates the converted value. We don't want to silently accept - * negative values and return a large positive number, so check for a - * minus sign (after optional whitespace) and return an error. - */ - std::size_t found = str.find_first_not_of(" \t"); - if (found != std::string::npos && str[found] == '-') - return false; - - char *end; - - errno = 0; - unsigned long value = std::strtoul(str.c_str(), &end, 10); - - if ('\0' != *end || errno == ERANGE || value > max) - return false; - - *result = value; - return true; -} - -} /* namespace */ - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - long value; - - if (!parseSignedInteger(obj.value_, std::numeric_limits::min(), - std::numeric_limits::max(), &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - unsigned long value; - - if (!parseUnsignedInteger(obj.value_, std::numeric_limits::max(), - &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - long value; - - if (!parseSignedInteger(obj.value_, std::numeric_limits::min(), - std::numeric_limits::max(), &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - unsigned long value; - - if (!parseUnsignedInteger(obj.value_, std::numeric_limits::max(), - &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - long value; - - if (!parseSignedInteger(obj.value_, std::numeric_limits::min(), - std::numeric_limits::max(), &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - unsigned long value; - - if (!parseUnsignedInteger(obj.value_, std::numeric_limits::max(), - &value)) - return std::nullopt; - - return value; -} +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; template<> std::optional