From 9457905bd112f597a01d59e7f38cd105147a2cd1 Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Sun, 23 Jan 2022 13:50:33 +0100 Subject: [PATCH] FUDI: use std::from_chars() --- src/fudi/connection.h | 9 ++------- src/fudi/parser.h | 16 ++++++++-------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/fudi/connection.h b/src/fudi/connection.h index 25ddfc37..0d6fcbaa 100644 --- a/src/fudi/connection.h +++ b/src/fudi/connection.h @@ -70,8 +70,7 @@ class Connection : public std::enable_shared_from_this void _read_socket() { auto self{shared_from_this()}; - // NB: minus one for zero termination - auto buffersize = _read_buffer.size() - 1; + auto buffersize = _read_buffer.size(); if (_read_offset >= buffersize) { SSR_ERROR("Input buffer is full; dropping contents"); @@ -85,9 +84,6 @@ class Connection : public std::enable_shared_from_this if (!ec) { _read_offset += length; - // NB: zero termination is needed for std::strtof and std::strtoul - // TODO: remove this once std::from_chars is used instead. - _read_buffer[_read_offset] = '\0'; auto input = std::string_view{_read_buffer.data(), _read_offset}; _parser.parse(input); auto consumed = input.data() - _read_buffer.data(); @@ -104,8 +100,7 @@ class Connection : public std::enable_shared_from_this } // See MAXPDSTRING in m_pd.h, and INBUFSIZE in s_inter.c - // ... plus one for zero termination - std::array _read_buffer; + std::array _read_buffer; std::size_t _read_offset{0}; asio::ip::tcp::socket _socket; Subscriber _subscriber; diff --git a/src/fudi/parser.h b/src/fudi/parser.h index ebabf61d..a3365299 100644 --- a/src/fudi/parser.h +++ b/src/fudi/parser.h @@ -28,7 +28,7 @@ #ifndef SSR_FUDI_PARSER_H #define SSR_FUDI_PARSER_H -#include // for std::strtof(), std::strtoul() +#include // for std::from_chars #include // for std::function #include @@ -280,9 +280,10 @@ constexpr auto parse_string(std::string& value) constexpr auto id_or_number(std::variant& value) { return [&value](std::string_view& input) { - char* str_end; - unsigned int number = std::strtoul(input.data(), &str_end, 10); - if (str_end == input.data()) + unsigned int number{}; + auto [str_end, ec] = std::from_chars( + input.data(), input.data() + input.size(), number); + if (ec != std::errc()) { std::string temp; auto result = parse_string(temp)(input); @@ -333,10 +334,9 @@ constexpr auto parse_float(float& value) { return Match::incomplete; } - char* str_end; - // TODO: use std::from_chars once it is widely available - value = std::strtof(temp.data(), &str_end); - if (str_end == temp.data()) + auto [str_end, ec] = std::from_chars( + temp.data(), temp.data() + temp.size(), value); + if (ec != std::errc()) { return Match::no; }