diff --git a/libc/fmt/pflink.h b/libc/fmt/pflink.h index 761ad7471..7af6cc357 100644 --- a/libc/fmt/pflink.h +++ b/libc/fmt/pflink.h @@ -12,19 +12,19 @@ */ #define PFLINK(...) _PFLINK(__VA_ARGS__) -#define _PFLINK(FMT, ...) \ - ({ \ - if (___PFLINK(FMT, strpbrk, "faAeg")) STATIC_YOINK("__fmt_dtoa"); \ - if (___PFLINK(FMT, strpbrk, "cmrqs")) { \ - if (___PFLINK(FMT, strstr, "%m")) STATIC_YOINK("strerror"); \ - if (___PFLINK(FMT, strstr, "%*") || \ - ___PFLINK(FMT, strpbrk, "0123456789")) { \ - STATIC_YOINK("strnwidth"); \ - STATIC_YOINK("strnwidth16"); \ - STATIC_YOINK("wcsnwidth"); \ - } \ - } \ - FMT; \ +#define _PFLINK(FMT, ...) \ + ({ \ + if (___PFLINK(FMT, strpbrk, "fFaAeEgG")) STATIC_YOINK("__fmt_dtoa"); \ + if (___PFLINK(FMT, strpbrk, "cmrqs")) { \ + if (___PFLINK(FMT, strstr, "%m")) STATIC_YOINK("strerror"); \ + if (___PFLINK(FMT, strstr, "%*") || \ + ___PFLINK(FMT, strpbrk, "0123456789")) { \ + STATIC_YOINK("strnwidth"); \ + STATIC_YOINK("strnwidth16"); \ + STATIC_YOINK("wcsnwidth"); \ + } \ + } \ + FMT; \ }) #define SFLINK(...) _SFLINK(__VA_ARGS__) diff --git a/test/libc/fmt/printf_uppercase_e_static_yoink_test.c b/test/libc/fmt/printf_uppercase_e_static_yoink_test.c new file mode 100644 index 000000000..33b3cc95a --- /dev/null +++ b/test/libc/fmt/printf_uppercase_e_static_yoink_test.c @@ -0,0 +1,42 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Gabriel Ravier │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ + +#include "libc/fmt/fmt.h" +#include "libc/intrin/kprintf.h" +#include "libc/runtime/runtime.h" +#include "libc/str/str.h" + +// We specifically avoid the test framework because otherwise __fmt_dtoa is +// always automatically pulled in from code in there - and this is what we're +// testing for here +int main() { + char buffer[30]; + + int snprintf_result = snprintf(buffer, sizeof(buffer), "%E", .0); + if (strcmp(buffer, "0.000000E+00")) { + kprintf( + "error: snprintf gave us '%s' instead of the expected '0.000000E+00'\n", + buffer); + abort(); + } + if (snprintf_result != 12) { + kprintf("error: snprintf returned %d instead of 12\n", snprintf_result); + abort(); + } +} diff --git a/test/libc/fmt/printf_uppercase_f_static_yoink_test.c b/test/libc/fmt/printf_uppercase_f_static_yoink_test.c new file mode 100644 index 000000000..ebbb13d0c --- /dev/null +++ b/test/libc/fmt/printf_uppercase_f_static_yoink_test.c @@ -0,0 +1,41 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Gabriel Ravier │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ + +#include "libc/fmt/fmt.h" +#include "libc/intrin/kprintf.h" +#include "libc/runtime/runtime.h" +#include "libc/str/str.h" + +// We specifically avoid the test framework because otherwise __fmt_dtoa is +// always automatically pulled in from code in there - and this is what we're +// testing for here +int main() { + char buffer[30]; + + int snprintf_result = snprintf(buffer, sizeof(buffer), "%F", .0); + if (strcmp(buffer, "0.000000")) { + kprintf("error: snprintf gave us '%s' instead of the expected '0.000000'\n", + buffer); + abort(); + } + if (snprintf_result != 8) { + kprintf("error: snprintf returned %d instead of 8\n", snprintf_result); + abort(); + } +} diff --git a/test/libc/fmt/printf_uppercase_g_static_yoink_test.c b/test/libc/fmt/printf_uppercase_g_static_yoink_test.c new file mode 100644 index 000000000..c3270c3d8 --- /dev/null +++ b/test/libc/fmt/printf_uppercase_g_static_yoink_test.c @@ -0,0 +1,41 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Gabriel Ravier │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ + +#include "libc/fmt/fmt.h" +#include "libc/intrin/kprintf.h" +#include "libc/runtime/runtime.h" +#include "libc/str/str.h" + +// We specifically avoid the test framework because otherwise __fmt_dtoa is +// always automatically pulled in from code in there - and this is what we're +// testing for here +int main() { + char buffer[30]; + + int snprintf_result = snprintf(buffer, sizeof(buffer), "%G", .0); + if (strcmp(buffer, "0")) { + kprintf("error: snprintf gave us '%s' instead of the expected '0'\n", + buffer); + abort(); + } + if (snprintf_result != 1) { + kprintf("error: snprintf returned %d instead of 1\n", snprintf_result); + abort(); + } +}