Implement C23 printf 'wf' length modifiers
wfN: Specifies that a following b, d, i, o, u, x, or X conversion specifier applies to a fastest minimum-width integer argument with a specific width where N is a positive decimal integer with no leading zeros (the argument will have been promoted according to the integer promotions, but its value shall be converted to the unpromoted type); or that a following n conversion specifier applies to a pointer to a fastest minimum-width integer type argument with a width of N bits. All fastest minimum-width integer types (7.22.1.3) defined in the header <stdint.h> shall be supported. Other supported values of N are implementation-defined.
Bug: b/271903607
Test: adb shell
Change-Id: Ida36d5a50af2a46fd04cb5fe039793d8872f9f3b
diff --git a/libc/stdio/printf_common.h b/libc/stdio/printf_common.h
index b0055f0..365728b 100644
--- a/libc/stdio/printf_common.h
+++ b/libc/stdio/printf_common.h
@@ -528,17 +528,29 @@
case 'b':
ADDUARG();
break;
- case 'w':
+ case 'w': {
n = 0;
+ bool fast = false;
ch = *fmt++;
+ if (ch == 'f') {
+ fast = true;
+ ch = *fmt++;
+ }
while (is_digit(ch)) {
APPEND_DIGIT(n, ch);
ch = *fmt++;
}
if (n == 64) {
flags |= LLONGINT;
+ } else {
+ if (n != 8 && fast) {
+#if defined(__LP64__)
+ flags |= LLONGINT;
+#endif
+ }
}
goto reswitch;
+ }
default: /* "%?" prints ?, unless ? is NUL */
if (ch == '\0') goto done;
break;
@@ -824,4 +836,15 @@
return convbuf;
}
+ // Trasnlate a fixed size integer argument for the %w/%wf format to a
+ // flag representation. Supported sizes are 8, 16, 32, and 64 so far.
+ // See details in bionic/libc/include/stdint.h
+ static int w_to_flag(int size, bool fast) {
+ static constexpr int fast_size = sizeof(void*) == 8 ? LLONGINT : 0;
+ if (size == 8) return CHARINT;
+ if (size == 16) return fast ? fast_size : SHORTINT;
+ if (size == 32) return fast ? fast_size : 0;
+ if (size == 64) return LLONGINT;
+ __fortify_fatal("%%w%s%d is unsupported", fast ? "f" : "", size);
+ }
};