diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-03-27 17:26:51 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-03-27 17:26:51 -0400 |
| commit | 3c52a756339a2d96824d21a7d3ad5de7fc1085a0 (patch) | |
| tree | e5299a25ebb76186d6372b700710d7c8c7fe0728 /src/stdlib/integers.c | |
| parent | 2186e84de0c0fd47ba48eaa35f74ea2754c3b81f (diff) | |
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.
Diffstat (limited to 'src/stdlib/integers.c')
| -rw-r--r-- | src/stdlib/integers.c | 14 |
1 files changed, 12 insertions, 2 deletions
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 <gmp.h> #include <stdbool.h> #include <stdint.h> +#include <stdio.h> #include <stdlib.h> #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 |
