From 3c52a756339a2d96824d21a7d3ad5de7fc1085a0 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 27 Mar 2025 17:26:51 -0400 Subject: Deprecate custom printf specifiers in favor of print() function that uses _Generic() to generically convert any value to a string or print as a string. --- src/stdlib/integers.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/stdlib/integers.c') diff --git a/src/stdlib/integers.c b/src/stdlib/integers.c index 4d5d0a80..c5764d46 100644 --- a/src/stdlib/integers.c +++ b/src/stdlib/integers.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "arrays.h" @@ -16,9 +17,18 @@ #include "text.h" #include "types.h" +public int Int$print(FILE *f, Int_t i) { + if (likely(i.small & 1L)) { + return fprintf(f, "%ld", (i.small)>>2L); + } else { + char *str = mpz_get_str(NULL, 10, *i.big); + return fputs(str, f); + } +} + public Text_t Int$value_as_text(Int_t i) { if (likely(i.small & 1L)) { - return Text$format("%ld", (i.small)>>2L); + return Text$format("%ld", i.small>>2L); } else { char *str = mpz_get_str(NULL, 10, *i.big); return Text$from_str(str); @@ -416,7 +426,7 @@ public Int_t Int$prev_prime(Int_t x) mpz_t p; mpz_init_set_int(p, x); if (unlikely(mpz_prevprime(p, p) == 0)) - fail("There is no prime number before %k", (Text_t[1]){Int$as_text(&x, false, &Int$info)}); + fail("There is no prime number before ", x); return Int$from_mpz(p); } #endif -- cgit v1.2.3