Skip to content

Commit 6b018ed

Browse files
authored
add support for positional arguments as width and precision specifiers (#4643)
* make positional arguments work as width and precision specifiers with floating-point formats too * fix test case failing ci tests
1 parent 46a4599 commit 6b018ed

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

include/fmt/printf.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,21 @@ auto parse_header(const Char*& it, const Char* end, format_specs& specs,
357357
if (specs.width == -1) report_error("number is too big");
358358
} else if (*it == '*') {
359359
++it;
360-
specs.width = static_cast<int>(
361-
get_arg(-1).visit(detail::printf_width_handler(specs)));
360+
// Check for positional width argument like *1$
361+
if (it != end && *it >= '0' && *it <= '9') {
362+
int width_index = parse_nonnegative_int(it, end, -1);
363+
if (it != end && *it == '$') {
364+
++it;
365+
specs.width = static_cast<int>(
366+
get_arg(width_index).visit(detail::printf_width_handler(specs)));
367+
} else {
368+
// Invalid format, rewind and treat as non-positional
369+
report_error("invalid format specifier");
370+
}
371+
} else {
372+
specs.width = static_cast<int>(
373+
get_arg(-1).visit(detail::printf_width_handler(specs)));
374+
}
362375
}
363376
}
364377
return arg_index;
@@ -439,8 +452,21 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format,
439452
specs.precision = parse_nonnegative_int(it, end, 0);
440453
} else if (c == '*') {
441454
++it;
442-
specs.precision =
443-
static_cast<int>(get_arg(-1).visit(printf_precision_handler()));
455+
// Check for positional precision argument like .*1$
456+
if (it != end && *it >= '0' && *it <= '9') {
457+
int precision_index = parse_nonnegative_int(it, end, -1);
458+
if (it != end && *it == '$') {
459+
++it;
460+
specs.precision = static_cast<int>(
461+
get_arg(precision_index).visit(printf_precision_handler()));
462+
} else {
463+
// Invalid format, rewind and treat as non-positional
464+
report_error("invalid format specifier");
465+
}
466+
} else {
467+
specs.precision =
468+
static_cast<int>(get_arg(-1).visit(printf_precision_handler()));
469+
}
444470
} else {
445471
specs.precision = 0;
446472
}

test/printf-test.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,35 @@ TEST(printf_test, dynamic_precision) {
296296
}
297297
}
298298

299+
TEST(printf_test, positional_width) {
300+
EXPECT_EQ(" 42", test_sprintf("%2$*1$d", 5, 42));
301+
EXPECT_EQ("42 ", test_sprintf("%2$*1$d", -5, 42));
302+
EXPECT_EQ(" abc", test_sprintf("%2$*1$s", 5, "abc"));
303+
EXPECT_THROW_MSG(test_sprintf("%2$*1$d", 5.0, 42), format_error,
304+
"width is not integer");
305+
EXPECT_THROW_MSG(test_sprintf("%2$*1$d"), format_error, "argument not found");
306+
EXPECT_THROW_MSG(test_sprintf("%2$*1$d", big_num, 42), format_error,
307+
"number is too big");
308+
}
309+
310+
TEST(printf_test, positional_precision) {
311+
EXPECT_EQ("00042", test_sprintf("%2$.*1$d", 5, 42));
312+
EXPECT_EQ("42", test_sprintf("%2$.*1$d", -5, 42));
313+
EXPECT_EQ("Hell", test_sprintf("%2$.*1$s", 4, "Hello"));
314+
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d", 5.0, 42), format_error,
315+
"precision is not integer");
316+
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d"), format_error, "argument not found");
317+
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d", big_num, 42), format_error,
318+
"number is too big");
319+
}
320+
321+
TEST(printf_test, positional_width_and_precision) {
322+
EXPECT_EQ(" 00042", test_sprintf("%3$*1$.*2$d", 7, 5, 42));
323+
EXPECT_EQ(" ab", test_sprintf("%3$*1$.*2$s", 7, 2, "abcdef"));
324+
EXPECT_EQ(" 00042", test_sprintf("%3$*1$.*2$x", 7, 5, 0x42));
325+
EXPECT_EQ("100.4400000", test_sprintf("%6$-*5$.*4$f%3$s%2$s%1$s", "", "", "", 7, 4, 100.44));
326+
}
327+
299328
template <typename T> struct make_signed {
300329
using type = T;
301330
};

0 commit comments

Comments
 (0)