Skip to content

Commit 9457905

Browse files
committed
FUDI: use std::from_chars()
1 parent 65b3cef commit 9457905

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/fudi/connection.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ class Connection : public std::enable_shared_from_this<Connection>
7070
void _read_socket()
7171
{
7272
auto self{shared_from_this()};
73-
// NB: minus one for zero termination
74-
auto buffersize = _read_buffer.size() - 1;
73+
auto buffersize = _read_buffer.size();
7574
if (_read_offset >= buffersize)
7675
{
7776
SSR_ERROR("Input buffer is full; dropping contents");
@@ -85,9 +84,6 @@ class Connection : public std::enable_shared_from_this<Connection>
8584
if (!ec)
8685
{
8786
_read_offset += length;
88-
// NB: zero termination is needed for std::strtof and std::strtoul
89-
// TODO: remove this once std::from_chars is used instead.
90-
_read_buffer[_read_offset] = '\0';
9187
auto input = std::string_view{_read_buffer.data(), _read_offset};
9288
_parser.parse(input);
9389
auto consumed = input.data() - _read_buffer.data();
@@ -104,8 +100,7 @@ class Connection : public std::enable_shared_from_this<Connection>
104100
}
105101

106102
// See MAXPDSTRING in m_pd.h, and INBUFSIZE in s_inter.c
107-
// ... plus one for zero termination
108-
std::array<char, 4096 + 1> _read_buffer;
103+
std::array<char, 4096> _read_buffer;
109104
std::size_t _read_offset{0};
110105
asio::ip::tcp::socket _socket;
111106
Subscriber _subscriber;

src/fudi/parser.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#ifndef SSR_FUDI_PARSER_H
2929
#define SSR_FUDI_PARSER_H
3030

31-
#include <cstdlib> // for std::strtof(), std::strtoul()
31+
#include <charconv> // for std::from_chars
3232
#include <functional> // for std::function
3333
#include <variant>
3434

@@ -280,9 +280,10 @@ constexpr auto parse_string(std::string& value)
280280
constexpr auto id_or_number(std::variant<std::string, unsigned int>& value)
281281
{
282282
return [&value](std::string_view& input) {
283-
char* str_end;
284-
unsigned int number = std::strtoul(input.data(), &str_end, 10);
285-
if (str_end == input.data())
283+
unsigned int number{};
284+
auto [str_end, ec] = std::from_chars(
285+
input.data(), input.data() + input.size(), number);
286+
if (ec != std::errc())
286287
{
287288
std::string temp;
288289
auto result = parse_string(temp)(input);
@@ -333,10 +334,9 @@ constexpr auto parse_float(float& value)
333334
{
334335
return Match::incomplete;
335336
}
336-
char* str_end;
337-
// TODO: use std::from_chars once it is widely available
338-
value = std::strtof(temp.data(), &str_end);
339-
if (str_end == temp.data())
337+
auto [str_end, ec] = std::from_chars(
338+
temp.data(), temp.data() + temp.size(), value);
339+
if (ec != std::errc())
340340
{
341341
return Match::no;
342342
}

0 commit comments

Comments
 (0)