Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add to_chars overload for floating points. #1160

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions include/seqan3/std/charconv
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,8 @@ void from_chars(char const *, char const *, bool, int = 10) = delete;
template <std::Integral value_type>
inline std::to_chars_result to_chars(char * first, char * last, value_type value, int base) noexcept
{
assert(first != nullptr);
assert(last != nullptr);
assert(2 <= base && base <= 36);
return seqan3::detail::to_chars_integral(first, last, value, base, is_signed<value_type>());
}
Expand All @@ -856,9 +858,31 @@ inline std::to_chars_result to_chars(char * first, char * last, value_type value
template <std::Integral value_type>
inline std::to_chars_result to_chars(char * first, char * last, value_type value) noexcept
{
assert(first != nullptr);
assert(last != nullptr);
return seqan3::detail::to_chars_itoa(first, last, value, is_signed<value_type>());
}

//!\brief std::to_chars overload for floating point via a std::stringstream for default base = 10.
//!\ingroup charconv
template <seqan3::FloatingPoint floating_point_type>
inline std::to_chars_result to_chars(char * first, char * last, floating_point_type value) noexcept
{
assert(first != nullptr);
assert(last != nullptr);

std::ostringstream ss;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please assert that both ptr values are not nullptr

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in 2 separate asserts so you can see by the line number which one failed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine 🙄

ss << value;
auto str = ss.str();

if (last - first < static_cast<std::ptrdiff_t>(str.size()))
return {last, std::errc::value_too_large};

std::copy(str.begin(), str.end(), first);

return {first + str.size(), std::errc{}};
}

// -----------------------------------------------------------------------------
// from_chars for integral types
// -----------------------------------------------------------------------------
Expand Down
45 changes: 41 additions & 4 deletions test/unit/std/charconv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <seqan3/std/charconv>
#include <seqan3/std/concepts>

// =============================================================================
// std::from_chars for integral types
// =============================================================================

template <typename T>
class integral_from_char_test: public ::testing::Test { };

Expand Down Expand Up @@ -220,14 +224,31 @@ TYPED_TEST(integral_from_char_test, hexadicimal_number)
}
}

TYPED_TEST(integral_from_char_test, to_char)
// =============================================================================
// std::to_chars for integral types
// =============================================================================

TYPED_TEST(integral_from_char_test, to_chars)
{
TypeParam val{120};
std::array<char, 10> buffer{};

auto res = std::to_chars(buffer.data(), buffer.data() + buffer.size(), val);

EXPECT_EQ(res.ptr, &buffer[3]);
EXPECT_EQ(res.ec, std::errc{});
rrahn marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_EQ((std::string_view{buffer.data(), 3}), std::string_view{"120"});
}

TYPED_TEST(integral_from_char_test, to_chars_error)
{
TypeParam val{120};
char buffer[10];
std::array<char, 1> buffer{};

// buffer.clear();
[[maybe_unused]] auto res = std::to_chars(&buffer[0], &buffer[0] + sizeof(buffer), val);
auto res = std::to_chars(buffer.data(), buffer.data() + buffer.size(), val);

EXPECT_EQ(res.ptr, buffer.data() + buffer.size());
EXPECT_EQ(res.ec, std::errc::value_too_large);
}

// =============================================================================
Expand Down Expand Up @@ -483,3 +504,19 @@ TYPED_TEST(from_char_real_test, non_valid_strings)
EXPECT_EQ(res.ec, std::errc::invalid_argument);
}
}

// =============================================================================
// std::to_chars for float, double and long double
// =============================================================================

TYPED_TEST(from_char_real_test, to_chars)
{
TypeParam val{120.3};
std::array<char, 10> buffer{};

auto res = std::to_chars(buffer.data(), buffer.data() + buffer.size(), val);

EXPECT_EQ(res.ptr, &buffer[5]);
EXPECT_EQ(res.ec, std::errc{});
EXPECT_EQ((std::string_view{buffer.data(), 5}), std::string_view{"120.3"});
}