diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-13 02:09:18 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-13 02:09:18 -0400 |
| commit | 158a1c6ad2d9b3f2a598e6d1d8debcc824ad73d0 (patch) | |
| tree | c7f05fc95b47e51bd9aa200860ebfdecee91fe57 /builtins | |
| parent | a32d90fcd737a27fd2d098a8d81b5965917439cf (diff) | |
More fixes
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/functions.c | 4 | ||||
| -rw-r--r-- | builtins/integers.c | 30 | ||||
| -rw-r--r-- | builtins/integers.h | 6 |
3 files changed, 30 insertions, 10 deletions
diff --git a/builtins/functions.c b/builtins/functions.c index cb75442d..d71587b9 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -45,9 +45,9 @@ static void print_stack_trace(FILE *out) fprintf(out, "\x1b[34m"); fflush(out); void *array[1024]; - size_t size = backtrace(array, sizeof(array)/sizeof(array[0])); + int64_t size = (int64_t)backtrace(array, sizeof(array)/sizeof(array[0])); char **strings = strings = backtrace_symbols(array, size); - for (size_t i = 2; i < size - 4; i++) { + for (int64_t i = 2; i < size - 4; i++) { char *filename = strings[i]; const char *cmd = heap_strf("addr2line -e %.*s -fisp | sed 's/\\$/./g;s/ at /() at /' >&2", strcspn(filename, "("), filename); FILE *fp = popen(cmd, "w"); diff --git a/builtins/integers.c b/builtins/integers.c index 8bc66172..dd38de91 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -75,28 +75,48 @@ public bool Int$hash(const Int_t *x, const TypeInfo *type) { return hash; } -public CORD Int$hex(Int_t i, int64_t digits, bool uppercase, bool prefix) { +public CORD Int$format(Int_t i, Int_t digits_int) +{ + int64_t digits = Int$as_i64(digits_int); + if (__builtin_expect(i.small & 1, 1)) { + return CORD_asprintf("%0.*ld", digits, (i.small)>>2); + } else { + CORD str = mpz_get_str(NULL, 10, *i.big); + bool negative = (str[0] == '-'); + if (digits > (int64_t)CORD_len(str)) { + if (negative) + str = CORD_all("-", CORD_chars('0', digits - CORD_len(str)), CORD_substr(str, 1, ~0)); + else + str = CORD_all(CORD_chars('0', digits - CORD_len(str)), str); + } + return str; + } +} + +public CORD Int$hex(Int_t i, Int_t digits_int, bool uppercase, bool prefix) { + int64_t digits = Int$as_i64(digits_int); const char *hex_fmt = uppercase ? (prefix ? "0x%0.*lX" : "%0.*lX") : (prefix ? "0x%0.*lx" : "%0.*lx"); if (__builtin_expect(i.small & 1, 1)) { - return CORD_asprintf(hex_fmt, (i.small)>>2); + return CORD_asprintf(hex_fmt, digits, (i.small)>>2); } else { CORD str = mpz_get_str(NULL, 16, *i.big); if (uppercase) str = Text$upper(str); if (digits > (int64_t)CORD_len(str)) - str = CORD_cat(str, CORD_chars('0', digits - CORD_len(str))); + str = CORD_cat(CORD_chars('0', digits - CORD_len(str)), str); if (prefix) str = CORD_cat("0x", str); return str; } } -public CORD Int$octal(Int_t i, int64_t digits, bool prefix) { +public CORD Int$octal(Int_t i, Int_t digits_int, bool prefix) { + int64_t digits = Int$as_i64(digits_int); const char *octal_fmt = prefix ? "0o%0.*lo" : "%0.*lo"; if (__builtin_expect(i.small & 1, 1)) { return CORD_asprintf(octal_fmt, (int)digits, (uint64_t)(i.small >> 2)); } else { CORD str = mpz_get_str(NULL, 8, *i.big); if (digits > (int64_t)CORD_len(str)) - str = CORD_cat(str, CORD_chars('0', digits - CORD_len(str))); + str = CORD_cat(CORD_chars('0', digits - CORD_len(str)), str); if (prefix) str = CORD_cat("0o", str); return str; } diff --git a/builtins/integers.h b/builtins/integers.h index a586dfcf..f93e7abf 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -50,9 +50,9 @@ int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type); int32_t Int$compare_value(const Int_t x, const Int_t y); bool Int$equal(const Int_t *x, const Int_t *y, const TypeInfo *type); bool Int$equal_value(const Int_t x, const Int_t y); -CORD Int$format(Int_t i, int64_t digits); -CORD Int$hex(Int_t i, int64_t digits, bool uppercase, bool prefix); -CORD Int$octal(Int_t i, int64_t digits, bool prefix); +CORD Int$format(Int_t i, Int_t digits); +CORD Int$hex(Int_t i, Int_t digits, bool uppercase, bool prefix); +CORD Int$octal(Int_t i, Int_t digits, bool prefix); void Int$init_random(long seed); Int_t Int$random(Int_t min, Int_t max); Range_t Int$to(Int_t from, Int_t to); |
