{"id":21877,"url":"https://patchwork.libcamera.org/api/patches/21877/?format=json","web_url":"https://patchwork.libcamera.org/patch/21877/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20241112185637.10232-1-laurent.pinchart@ideasonboard.com>","date":"2024-11-12T18:56:37","name":"libcamera: yaml_parser: Use std::from_chars()","commit_ref":"5c71df927ddaaa01204bff1e647c9d2bf653d95f","pull_url":null,"state":"accepted","archived":false,"hash":"ae2411303e6b782a55bf375e4fd98a4867107028","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21877/mbox/","series":[{"id":4786,"url":"https://patchwork.libcamera.org/api/series/4786/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4786","date":"2024-11-12T18:56:37","name":"libcamera: yaml_parser: Use std::from_chars()","version":1,"mbox":"https://patchwork.libcamera.org/series/4786/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21877/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21877/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 5D8B7C324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 Nov 2024 18:56:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B15DF657F9;\n\tTue, 12 Nov 2024 19:56:46 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 72A7B657B0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Nov 2024 19:56:45 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A3379710\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Nov 2024 19:56:32 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"rRYiNxgs\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1731437792;\n\tbh=m9PV8VXaLwI58zeg/Mxcl9qs0d+JDzAxQNf2VQNNowk=;\n\th=From:To:Subject:Date:From;\n\tb=rRYiNxgsGozfVV7mBv5gytzZ6ys+mo4pzObp60SsmFHBPL7ZDvBZIL3ryQxjQ+j+D\n\t37RFairnnHJFBc4hmz9OW0QujUsiXG9ppkbGe1++7MVyX5dIdlOQkh9LNgQq56hF30\n\t/nQZFoMIGlPvf5AiMKqDbRexa2vwMXOIGAIRNq9s=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","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","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"std::from_chars(), introduced in C++17, is a fast, locale-independent\nstring-to-arithmetic conversion function. The C++ standard library\nprovides overloads for all integer types, making it a prime candidate to\nreplace the manual handling of integer sizes in the YamlParser string to\ninteger conversion.\n\nCompared to std::strtol(), std::from_chars() doesn't recognize the '0x'\nprefix or '+' prefix, and doesn't ignore leading white space. As the\nYamlParser doesn't require those features, std::from_chars() can be used\nsafely, reducing the amount of code.\n\nC++17 also requires the standard C++ library to provide overloads for\nfloating-point types, but libc++ does not implement those. The float and\nbool implementations of YamlParser::Getter::get() are therefore kept\nas-is.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/yaml_parser.h |   2 +-\n src/libcamera/yaml_parser.cpp            | 168 ++++-------------------\n 2 files changed, 29 insertions(+), 141 deletions(-)\n\n\nbase-commit: dcb90f13cf79d909d04dacf2cfbd256a4744347f","diff":"diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\nindex de452844fe0a..8c7916565946 100644\n--- a/include/libcamera/internal/yaml_parser.h\n+++ b/include/libcamera/internal/yaml_parser.h\n@@ -224,7 +224,7 @@ private:\n \t\tEmpty,\n \t};\n \n-\ttemplate<typename T>\n+\ttemplate<typename T, typename Enable = void>\n \tstruct Getter {\n \t\tstd::optional<T> get(const YamlObject &obj) const;\n \t};\ndiff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\nindex 7c0b341a32e7..db256ec5b04d 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -7,6 +7,7 @@\n \n #include \"libcamera/internal/yaml_parser.h\"\n \n+#include <charconv>\n #include <cstdlib>\n #include <errno.h>\n #include <functional>\n@@ -146,151 +147,38 @@ YamlObject::Getter<bool>::get(const YamlObject &obj) const\n \treturn std::nullopt;\n }\n \n-namespace {\n-\n-bool parseSignedInteger(const std::string &str, long min, long max,\n-\t\t\tlong *result)\n+template<typename T>\n+struct YamlObject::Getter<T, std::enable_if_t<\n+\tstd::is_same_v<int8_t, T> ||\n+\tstd::is_same_v<uint8_t, T> ||\n+\tstd::is_same_v<int16_t, T> ||\n+\tstd::is_same_v<uint16_t, T> ||\n+\tstd::is_same_v<int32_t, T> ||\n+\tstd::is_same_v<uint32_t, T>>>\n {\n-\tif (str == \"\")\n-\t\treturn false;\n+\tstd::optional<T> get(const YamlObject &obj) const\n+\t{\n+\t\tif (obj.type_ != Type::Value)\n+\t\t\treturn std::nullopt;\n \n-\tchar *end;\n+\t\tconst std::string &str = obj.value_;\n+\t\tT value;\n \n-\terrno = 0;\n-\tlong value = std::strtol(str.c_str(), &end, 10);\n+\t\tauto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(),\n+\t\t\t\t\t\t value);\n+\t\tif (ptr != str.data() + str.size() || ec != std::errc())\n+\t\t\treturn std::nullopt;\n \n-\tif ('\\0' != *end || errno == ERANGE || value < min || value > max)\n-\t\treturn false;\n+\t\treturn value;\n+\t}\n+};\n \n-\t*result = value;\n-\treturn true;\n-}\n-\n-bool parseUnsignedInteger(const std::string &str, unsigned long max,\n-\t\t\t  unsigned long *result)\n-{\n-\tif (str == \"\")\n-\t\treturn false;\n-\n-\t/*\n-\t * strtoul() accepts strings representing a negative number, in which\n-\t * case it negates the converted value. We don't want to silently accept\n-\t * negative values and return a large positive number, so check for a\n-\t * minus sign (after optional whitespace) and return an error.\n-\t */\n-\tstd::size_t found = str.find_first_not_of(\" \\t\");\n-\tif (found != std::string::npos && str[found] == '-')\n-\t\treturn false;\n-\n-\tchar *end;\n-\n-\terrno = 0;\n-\tunsigned long value = std::strtoul(str.c_str(), &end, 10);\n-\n-\tif ('\\0' != *end || errno == ERANGE || value > max)\n-\t\treturn false;\n-\n-\t*result = value;\n-\treturn true;\n-}\n-\n-} /* namespace */\n-\n-template<>\n-std::optional<int8_t>\n-YamlObject::Getter<int8_t>::get(const YamlObject &obj) const\n-{\n-\tif (obj.type_ != Type::Value)\n-\t\treturn std::nullopt;\n-\n-\tlong value;\n-\n-\tif (!parseSignedInteger(obj.value_, std::numeric_limits<int8_t>::min(),\n-\t\t\t\tstd::numeric_limits<int8_t>::max(), &value))\n-\t\treturn std::nullopt;\n-\n-\treturn value;\n-}\n-\n-template<>\n-std::optional<uint8_t>\n-YamlObject::Getter<uint8_t>::get(const YamlObject &obj) const\n-{\n-\tif (obj.type_ != Type::Value)\n-\t\treturn std::nullopt;\n-\n-\tunsigned long value;\n-\n-\tif (!parseUnsignedInteger(obj.value_, std::numeric_limits<uint8_t>::max(),\n-\t\t\t\t  &value))\n-\t\treturn std::nullopt;\n-\n-\treturn value;\n-}\n-\n-template<>\n-std::optional<int16_t>\n-YamlObject::Getter<int16_t>::get(const YamlObject &obj) const\n-{\n-\tif (obj.type_ != Type::Value)\n-\t\treturn std::nullopt;\n-\n-\tlong value;\n-\n-\tif (!parseSignedInteger(obj.value_, std::numeric_limits<int16_t>::min(),\n-\t\t\t\tstd::numeric_limits<int16_t>::max(), &value))\n-\t\treturn std::nullopt;\n-\n-\treturn value;\n-}\n-\n-template<>\n-std::optional<uint16_t>\n-YamlObject::Getter<uint16_t>::get(const YamlObject &obj) const\n-{\n-\tif (obj.type_ != Type::Value)\n-\t\treturn std::nullopt;\n-\n-\tunsigned long value;\n-\n-\tif (!parseUnsignedInteger(obj.value_, std::numeric_limits<uint16_t>::max(),\n-\t\t\t\t  &value))\n-\t\treturn std::nullopt;\n-\n-\treturn value;\n-}\n-\n-template<>\n-std::optional<int32_t>\n-YamlObject::Getter<int32_t>::get(const YamlObject &obj) const\n-{\n-\tif (obj.type_ != Type::Value)\n-\t\treturn std::nullopt;\n-\n-\tlong value;\n-\n-\tif (!parseSignedInteger(obj.value_, std::numeric_limits<int32_t>::min(),\n-\t\t\t\tstd::numeric_limits<int32_t>::max(), &value))\n-\t\treturn std::nullopt;\n-\n-\treturn value;\n-}\n-\n-template<>\n-std::optional<uint32_t>\n-YamlObject::Getter<uint32_t>::get(const YamlObject &obj) const\n-{\n-\tif (obj.type_ != Type::Value)\n-\t\treturn std::nullopt;\n-\n-\tunsigned long value;\n-\n-\tif (!parseUnsignedInteger(obj.value_, std::numeric_limits<uint32_t>::max(),\n-\t\t\t\t  &value))\n-\t\treturn std::nullopt;\n-\n-\treturn value;\n-}\n+template struct YamlObject::Getter<int8_t>;\n+template struct YamlObject::Getter<uint8_t>;\n+template struct YamlObject::Getter<int16_t>;\n+template struct YamlObject::Getter<uint16_t>;\n+template struct YamlObject::Getter<int32_t>;\n+template struct YamlObject::Getter<uint32_t>;\n \n template<>\n std::optional<float>\n","prefixes":[]}