Skip to content

Commit

Permalink
Bugfix to fill character skipping with center alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskosunen committed Jun 7, 2024
1 parent 80bd453 commit 00e02f9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/scn/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6082,6 +6082,10 @@ struct arg_reader {

if (specs.align == detail::align_type::left ||
specs.align == detail::align_type::center) {
if (specs.precision != 0 &&
specs.precision - value_width - prefix_width == 0) {
return result_type{rng.begin(), 0};
}
return skip_fill(rng, specs.precision - value_width - prefix_width,
specs.fill, need_skipped_width);
}
Expand Down Expand Up @@ -6121,8 +6125,19 @@ struct arg_reader {
specs.width != 0 || specs.precision != 0;

// Read prefix
SCN_TRY(prefix_result, impl_prefix(rng, rd.skip_ws_before_read()));
auto [it, prefix_width] = prefix_result;
auto it = rng.begin();
std::ptrdiff_t prefix_width = 0;
if (specs.precision != 0) {
auto max_width_view = take_width(rng, specs.precision);
SCN_TRY(prefix_result,
impl_prefix(max_width_view, rd.skip_ws_before_read()));
it = prefix_result.first.base();
prefix_width = prefix_result.second;
}
else {
SCN_TRY(prefix_result, impl_prefix(rng, rd.skip_ws_before_read()));
std::tie(it, prefix_width) = prefix_result;
}
auto prefix_end_it = it;

// Read value
Expand All @@ -6131,8 +6146,8 @@ struct arg_reader {
if (specs.precision <= prefix_width) {
return unexpected_scan_error(
scan_error::invalid_scanned_value,
"Too many spaces before value, precision exceeded before "
"reading value");
"Too many fill characters before value, "
"precision exceeded before reading value");
}

const auto initial_width = specs.precision - prefix_width;
Expand Down
24 changes: 24 additions & 0 deletions tests/unittests/align_and_fill_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,30 @@ TEST(AlignAndFillTest, NoWidth_EqualPrec_CenterAlign_CorrectFill)
EXPECT_STREQ(r->begin(), "");
}

TEST(AlignAndFillTest, NoWidth_LesserPrec_CenterAlign_CorrectFill)
{
auto r = scn::scan<int>("**42**", "{:*^.5}");
ASSERT_TRUE(r);
EXPECT_EQ(r->value(), 42);
EXPECT_STREQ(r->begin(), "*");
}

TEST(AlignAndFillTest, NoWidth_EvenLesserPrec_CenterAlign_CorrectFill)
{
auto r = scn::scan<int>("**42**", "{:*^.4}");
ASSERT_TRUE(r);
EXPECT_EQ(r->value(), 42);
EXPECT_STREQ(r->begin(), "**");
}

TEST(AlignAndFillTest, NoWidth_EvenMoreLesserPrec_CenterAlign_CorrectFill)
{
auto r = scn::scan<int>("**42**", "{:*^.3}");
ASSERT_TRUE(r);
EXPECT_EQ(r->value(), 4);
EXPECT_STREQ(r->begin(), "2**");
}

TEST(AlignAndFillTest, P1729_Ex3r0)
{
auto r = scn::scan<int>(" 42", "{}");
Expand Down

0 comments on commit 00e02f9

Please sign in to comment.