Skip to content

Commit

Permalink
FUDI: use std::from_chars()
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed Jan 23, 2022
1 parent 65b3cef commit 9457905
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
9 changes: 2 additions & 7 deletions src/fudi/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ class Connection : public std::enable_shared_from_this<Connection>
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");
Expand All @@ -85,9 +84,6 @@ class Connection : public std::enable_shared_from_this<Connection>
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();
Expand All @@ -104,8 +100,7 @@ class Connection : public std::enable_shared_from_this<Connection>
}

// See MAXPDSTRING in m_pd.h, and INBUFSIZE in s_inter.c
// ... plus one for zero termination
std::array<char, 4096 + 1> _read_buffer;
std::array<char, 4096> _read_buffer;
std::size_t _read_offset{0};
asio::ip::tcp::socket _socket;
Subscriber _subscriber;
Expand Down
16 changes: 8 additions & 8 deletions src/fudi/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#ifndef SSR_FUDI_PARSER_H
#define SSR_FUDI_PARSER_H

#include <cstdlib> // for std::strtof(), std::strtoul()
#include <charconv> // for std::from_chars
#include <functional> // for std::function
#include <variant>

Expand Down Expand Up @@ -280,9 +280,10 @@ constexpr auto parse_string(std::string& value)
constexpr auto id_or_number(std::variant<std::string, unsigned int>& 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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 9457905

Please sign in to comment.