Skip to content

Commit

Permalink
printf: Handle intmax size modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
qookei committed Apr 28, 2024
1 parent dd9d1ea commit 667ffd6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 16 additions & 1 deletion include/frg/printf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ enum class printf_size_mod {
#ifndef FRG_DONT_USE_LONG_DOUBLE
longdouble_size,
#endif
native_size
native_size,
intmax_size
};

template<typename T>
Expand Down Expand Up @@ -202,6 +203,10 @@ frg::expected<format_error> printf_format(A agent, const char *s, va_struct *vsp
szmod = printf_size_mod::native_size;
++s;
FRG_ASSERT(*s);
} else if(*s == 'j') {
szmod = printf_size_mod::intmax_size;
++s;
FRG_ASSERT(*s);
}

auto res = agent(*s, opts, szmod);
Expand Down Expand Up @@ -315,6 +320,8 @@ void do_printf_ints(S &sink, char t, format_options opts,
number = pop_arg<long long>(vsp, &opts);
}else if(szmod == printf_size_mod::native_size) {
number = pop_arg<intptr_t>(vsp, &opts);
}else if(szmod == printf_size_mod::intmax_size) {
number = pop_arg<intmax_t>(vsp, &opts);
}else{
FRG_ASSERT(szmod == printf_size_mod::default_size);
number = pop_arg<int>(vsp, &opts);
Expand Down Expand Up @@ -353,6 +360,8 @@ void do_printf_ints(S &sink, char t, format_options opts,
print(pop_arg<unsigned long long>(vsp, &opts));
}else if(szmod == printf_size_mod::native_size) {
print(pop_arg<size_t>(vsp, &opts));
}else if(szmod == printf_size_mod::intmax_size) {
print(pop_arg<uintmax_t>(vsp, &opts));
}else{
FRG_ASSERT(szmod == printf_size_mod::default_size);
print(pop_arg<unsigned int>(vsp, &opts));
Expand Down Expand Up @@ -383,6 +392,8 @@ void do_printf_ints(S &sink, char t, format_options opts,
print(pop_arg<unsigned long long>(vsp, &opts));
}else if(szmod == printf_size_mod::native_size) {
print(pop_arg<size_t>(vsp, &opts));
}else if(szmod == printf_size_mod::intmax_size) {
print(pop_arg<uintmax_t>(vsp, &opts));
}else{
FRG_ASSERT(szmod == printf_size_mod::default_size);
print(pop_arg<unsigned int>(vsp, &opts));
Expand Down Expand Up @@ -413,6 +424,8 @@ void do_printf_ints(S &sink, char t, format_options opts,
print(pop_arg<unsigned long long>(vsp, &opts));
}else if(szmod == printf_size_mod::native_size) {
print(pop_arg<size_t>(vsp, &opts));
}else if(szmod == printf_size_mod::intmax_size) {
print(pop_arg<uintmax_t>(vsp, &opts));
}else{
FRG_ASSERT(szmod == printf_size_mod::default_size);
print(pop_arg<unsigned int>(vsp, &opts));
Expand Down Expand Up @@ -441,6 +454,8 @@ void do_printf_ints(S &sink, char t, format_options opts,
print(pop_arg<unsigned long long>(vsp, &opts));
}else if(szmod == printf_size_mod::native_size) {
print(pop_arg<size_t>(vsp, &opts));
}else if(szmod == printf_size_mod::intmax_size) {
print(pop_arg<uintmax_t>(vsp, &opts));
}else{
FRG_ASSERT(szmod == printf_size_mod::default_size);
print(pop_arg<unsigned int>(vsp, &opts));
Expand Down
4 changes: 4 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ TEST(formatting, printf) {
do_test("12", "%zd", (size_t)12);
do_test("12", "%hd", 12);
do_test("12", "%hhd", 12);
do_test("12", "%jd", (uintmax_t)12);

// Test 'x' with different size mods to see
// if they work
Expand All @@ -355,6 +356,7 @@ TEST(formatting, printf) {
do_test("c", "%zx", (size_t)12);
do_test("c", "%hx", 12);
do_test("c", "%hhx", 12);
do_test("c", "%jx", (uintmax_t)12);

// Test 'X' with different size mods to see
// if they work
Expand All @@ -364,6 +366,7 @@ TEST(formatting, printf) {
do_test("C", "%zX", (size_t)12);
do_test("C", "%hX", 12);
do_test("C", "%hhX", 12);
do_test("C", "%jX", (uintmax_t)12);

// Test 'o' with different size mods to see
// if they work
Expand All @@ -373,6 +376,7 @@ TEST(formatting, printf) {
do_test("14", "%zo", (size_t)12);
do_test("14", "%ho", 12);
do_test("14", "%hho", 12);
do_test("14", "%jo", (uintmax_t)12);

// Test 's' with precision.
do_test("hello world", "%s", "hello world");
Expand Down

0 comments on commit 667ffd6

Please sign in to comment.