Skip to content

Commit 8124648

Browse files
committed
Refactor quantum preservation to be a chars_format option
1 parent c9e2349 commit 8124648

File tree

4 files changed

+16
-86
lines changed

4 files changed

+16
-86
lines changed

include/boost/decimal/charconv.hpp

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <boost/decimal/detail/countl.hpp>
2727
#include <boost/decimal/detail/remove_trailing_zeros.hpp>
2828
#include <boost/decimal/detail/promotion.hpp>
29-
#include <boost/decimal/detail/quantum_preservation_format.hpp>
3029

3130
#ifndef BOOST_DECIMAL_BUILD_MODULE
3231
#include <cstdint>
@@ -1212,6 +1211,20 @@ constexpr auto to_chars_impl(char* first, char* last, const TargetDecimalType& v
12121211
return to_chars_scientific_impl(first, last, value, fmt);
12131212
case chars_format::hex:
12141213
return to_chars_hex_impl(first, last, value);
1214+
case chars_format::cohort_preserving_scientific:
1215+
BOOST_DECIMAL_IF_CONSTEXPR (detail::is_fast_type_v<TargetDecimalType>)
1216+
{
1217+
// Fast types have no concept of cohorts
1218+
return {last, std::errc::invalid_argument};
1219+
}
1220+
1221+
if (local_precision != -1)
1222+
{
1223+
// Precision and cohort preservation are mutually exclusive options
1224+
return {last, std::errc::invalid_argument};
1225+
}
1226+
1227+
return to_chars_cohort_preserving_scientific(first, last, value);
12151228
// LCOV_EXCL_START
12161229
default:
12171230
BOOST_DECIMAL_UNREACHABLE;
@@ -1243,38 +1256,6 @@ constexpr auto to_chars_impl(char* first, char* last, const TargetDecimalType& v
12431256
return to_chars_scientific_impl(first, last, value, fmt, local_precision); // LCOV_EXCL_LINE
12441257
}
12451258

1246-
// TODO(mborland): Remove once other modes are inplace
1247-
#ifdef _MSC_VER
1248-
# pragma warning(push)
1249-
# pragma warning(disable: 4065)
1250-
#endif
1251-
1252-
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
1253-
constexpr auto to_chars_impl(char* first, char* last, const TargetDecimalType& value, const chars_format fmt, const quantum_preservation method) noexcept -> to_chars_result
1254-
{
1255-
// No quantum preservation is the same thing as regular to_chars
1256-
if (method == quantum_preservation::off)
1257-
{
1258-
return to_chars_impl(first, last, value, fmt);
1259-
}
1260-
1261-
// Sanity check our bounds
1262-
if (BOOST_DECIMAL_UNLIKELY(first >= last))
1263-
{
1264-
return {last, std::errc::invalid_argument};
1265-
}
1266-
1267-
switch (fmt)
1268-
{
1269-
default:
1270-
return to_chars_cohort_preserving_scientific(first, last, value);
1271-
}
1272-
}
1273-
1274-
#ifdef _MSC_VER
1275-
# pragma warning(pop)
1276-
#endif
1277-
12781259
} //namespace detail
12791260

12801261
BOOST_DECIMAL_EXPORT template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
@@ -1300,13 +1281,6 @@ constexpr auto to_chars(char* first, char* last, const TargetDecimalType& value,
13001281
return detail::to_chars_impl(first, last, value, fmt, precision);
13011282
}
13021283

1303-
BOOST_DECIMAL_EXPORT template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
1304-
constexpr auto to_chars(char* first, char* last, const TargetDecimalType& value, const chars_format fmt, const quantum_preservation method) noexcept -> to_chars_result
1305-
{
1306-
static_assert(detail::is_ieee_type_v<TargetDecimalType>, "Fast types are automatically normalized, so they have no concept of quantum preservation");
1307-
return detail::to_chars_impl(first, last, value, fmt, method);
1308-
}
1309-
13101284
#ifdef BOOST_DECIMAL_HAS_STD_CHARCONV
13111285

13121286
BOOST_DECIMAL_EXPORT template <typename DecimalType>

include/boost/decimal/detail/chars_format.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ BOOST_DECIMAL_EXPORT enum class chars_format : unsigned
1717
scientific = 1 << 0,
1818
fixed = 1 << 1,
1919
hex = 1 << 2,
20+
cohort_preserving_scientific = 1 << 3,
2021
general = fixed | scientific
2122
};
2223

include/boost/decimal/detail/quantum_preservation_format.hpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/test_to_chars_quantum.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,12 @@ void test_to_chars_scientific(const ResultsType& decimals, const StringsType& st
2222
for (std::size_t i {}; i < decimals.size(); ++i)
2323
{
2424
char buffer[64] {};
25-
const auto r {to_chars(buffer, buffer + sizeof(buffer), decimals[i], chars_format::scientific, quantum_preservation::on)};
25+
const auto r {to_chars(buffer, buffer + sizeof(buffer), decimals[i], chars_format::cohort_preserving_scientific)};
2626
BOOST_TEST(r);
2727
*r.ptr = '\0';
2828

2929
BOOST_TEST_CSTR_EQ(buffer, strings[i]);
3030
}
31-
32-
for (std::size_t i {}; i < decimals.size(); ++i)
33-
{
34-
char quantum_buffer[64] {};
35-
const auto r_quantum {to_chars(quantum_buffer, quantum_buffer + sizeof(quantum_buffer), decimals[i], chars_format::scientific, quantum_preservation::off)};
36-
BOOST_TEST(r_quantum);
37-
*r_quantum.ptr = '\0';
38-
39-
char buffer[64] {};
40-
const auto r {to_chars(buffer, buffer + sizeof(buffer), decimals[i], chars_format::scientific)};
41-
BOOST_TEST(r);
42-
*r.ptr = '\0';
43-
44-
BOOST_TEST_CSTR_EQ(quantum_buffer, buffer);
45-
BOOST_TEST_CSTR_EQ(quantum_buffer, strings[0]);
46-
}
4731
}
4832

4933
template <typename T>

0 commit comments

Comments
 (0)