Skip to content

Commit

Permalink
Implement LWG-3721: Allow an arg-id with a value of zero for width in…
Browse files Browse the repository at this point in the history
… std-format-spec (microsoft#2906)

Co-authored-by: Casey Carter <[email protected]>
  • Loading branch information
2 people authored and fsb4000 committed Aug 13, 2022
1 parent 49bffbb commit 1348b47
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
13 changes: 4 additions & 9 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -1421,15 +1421,10 @@ public:
template <class _Ty>
_NODISCARD constexpr unsigned long long operator()(const _Ty _Value) const {
if constexpr (is_integral_v<_Ty>) {
bool _Positive;
if constexpr (same_as<_Ty, bool>) { // avoid "bool > 0", which triggers C4804
_Positive = _Value != 0;
} else {
_Positive = _Value > 0;
}

if (!_Positive) {
_Throw_format_error("width is not positive.");
if constexpr (is_signed_v<_Ty>) {
if (_Value < 0) {
_Throw_format_error("Negative width.");
}
}
return static_cast<unsigned long long>(_Value);
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/std/tests/P0645R10_text_formatting_death/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void test_case_advance_no_range() {
context.advance_to(other_format_string.begin());
}

void test_case_zero_dynamic_width() {
(void) format("{:{}}", 42, 0);
void test_case_negative_dynamic_width() {
(void) format("{:{}}", 42, -2);
}

int main(int argc, char* argv[]) {
Expand All @@ -27,7 +27,7 @@ int main(int argc, char* argv[]) {
#if _ITERATOR_DEBUG_LEVEL != 0
exec.add_death_tests({
test_case_advance_no_range,
test_case_zero_dynamic_width,
test_case_negative_dynamic_width,
});
#endif // _ITERATOR_DEBUG_LEVEL != 0

Expand Down
2 changes: 2 additions & 0 deletions tests/std/tests/P0645R10_text_formatting_formatting/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,8 @@ void libfmt_formatter_test_runtime_width() {
== STR(" 0")); // behavior differs from libfmt, but conforms
throw_helper(STR("{0:{1}}"), 0, 0.0);

assert(format(STR("{0:{1}}"), 42, 0) == STR("42")); // LWG-3721: zero dynamic width is OK

assert(format(STR("{0:{1}}"), -42, 4) == STR(" -42"));
assert(format(STR("{0:{1}}"), 42u, 5) == STR(" 42"));
assert(format(STR("{0:{1}}"), -42l, 6) == STR(" -42"));
Expand Down

0 comments on commit 1348b47

Please sign in to comment.