Skip to content

Commit c5ff5c2

Browse files
authored
Merge pull request #992 from cppalliance/more_format
Fix formatting support so that we can format into the middle of a string
2 parents b1d82a9 + b30ddba commit c5ff5c2

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

include/boost/decimal/fmt_format.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ constexpr auto parse_impl(ParseContext &ctx)
7777
}
7878

7979
// If there is a . then we need to capture the precision argument
80-
if (*it == '.')
80+
if (it != ctx.end() && *it == '.')
8181
{
8282
++it;
8383
ctx_precision = 0;
@@ -172,7 +172,6 @@ struct formatter
172172
template <typename FormatContext>
173173
auto format(const T& v, FormatContext& ctx) const
174174
{
175-
auto out = ctx.out();
176175
std::array<char, 128> buffer {};
177176
auto buffer_front = buffer.data();
178177
bool has_sign {false};
@@ -229,7 +228,7 @@ struct formatter
229228
s.insert(s.begin() + static_cast<std::size_t>(has_sign), static_cast<std::size_t>(padding_digits) - s.size(), '0');
230229
}
231230

232-
return std::copy(s.begin(), s.end(), out);
231+
return fmt::format_to(ctx.out(), "{}", s);
233232
}
234233
};
235234

include/boost/decimal/format.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ constexpr auto parse_impl(ParseContext &ctx)
7777
}
7878

7979
// If there is a . then we need to capture the precision argument
80-
if (*it == '.')
80+
if (it != ctx.end() && *it == '.')
8181
{
8282
++it;
8383
ctx_precision = 0;
@@ -176,7 +176,6 @@ struct formatter<T>
176176
using namespace boost::decimal;
177177
using namespace boost::decimal::detail;
178178

179-
auto out = ctx.out();
180179
std::array<char, 128> buffer {};
181180
auto buffer_front = buffer.data();
182181
bool has_sign {false};
@@ -233,7 +232,7 @@ struct formatter<T>
233232
s.insert(s.begin() + static_cast<std::size_t>(has_sign), static_cast<std::size_t>(padding_digits) - s.size(), '0');
234233
}
235234

236-
return std::copy(s.begin(), s.end(), out);
235+
return std::format_to(ctx.out(), "{}", s);
237236
}
238237
};
239238

test/test_format.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ void test_hex()
189189
BOOST_TEST_EQ(std::format("{:A}", -std::numeric_limits<T>::signaling_NaN()), "-NAN(SNAN)");
190190
}
191191

192+
template <typename T>
193+
void test_with_string()
194+
{
195+
BOOST_TEST_EQ(std::format("Height is: {:.0f} meters", T {0}), "Height is: 0 meters");
196+
BOOST_TEST_EQ(std::format("Height is: {} meters", T {2}), "Height is: 2 meters");
197+
}
198+
192199
int main()
193200
{
194201
test_general<decimal32_t>();
@@ -219,6 +226,13 @@ int main()
219226
test_hex<decimal128_t>();
220227
test_hex<decimal_fast128_t>();
221228

229+
test_with_string<decimal32_t>();
230+
test_with_string<decimal_fast32_t>();
231+
test_with_string<decimal64_t>();
232+
test_with_string<decimal_fast64_t>();
233+
test_with_string<decimal128_t>();
234+
test_with_string<decimal_fast128_t>();
235+
222236
return boost::report_errors();
223237
}
224238

test/test_format_fmtlib.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ void test_hex()
213213
BOOST_TEST_EQ(fmt::format("{:A}", -std::numeric_limits<T>::signaling_NaN()), "-NAN(SNAN)");
214214
}
215215

216+
template <typename T>
217+
void test_with_string()
218+
{
219+
BOOST_TEST_EQ(std::format("Height is: {:.0f} meters", T {0}), "Height is: 0 meters");
220+
BOOST_TEST_EQ(std::format("Height is: {} meters", T {2}), "Height is: 2 meters");
221+
}
222+
216223
#ifdef _MSC_VER
217224
#pragma warning(pop)
218225
#endif
@@ -247,6 +254,13 @@ int main()
247254
test_hex<decimal128_t>();
248255
test_hex<decimal_fast128_t>();
249256

257+
test_with_string<decimal32_t>();
258+
test_with_string<decimal_fast32_t>();
259+
test_with_string<decimal64_t>();
260+
test_with_string<decimal_fast64_t>();
261+
test_with_string<decimal128_t>();
262+
test_with_string<decimal_fast128_t>();
263+
250264
return boost::report_errors();
251265
}
252266

0 commit comments

Comments
 (0)