diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 01:24:26 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 01:24:26 -0400 |
| commit | bd111dbe2ea1718dd541f347ce9c0b08068cc544 (patch) | |
| tree | 3b06ce97c9e40685d75d2833808797d49baef6e3 /builtins | |
| parent | 4e4e397e4c9171465a6f6e9bcb8d6edf2818d8ed (diff) | |
Automatically interpolate ints for pattern/shell/path
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/integers.c | 26 | ||||
| -rw-r--r-- | builtins/integers.h | 1 |
2 files changed, 15 insertions, 12 deletions
diff --git a/builtins/integers.c b/builtins/integers.c index 9160bfca..cbdf6f5a 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -17,24 +17,27 @@ static gmp_randstate_t Int_rng = {}; -public void Int$init_random(long seed) -{ +public void Int$init_random(long seed) { gmp_randinit_default(Int_rng); gmp_randseed_ui(Int_rng, (unsigned long)seed); } +public Text_t Int$value_as_text(Int_t i) { + if (__builtin_expect(i.small & 1, 1)) { + return Text$format("%ld", (i.small)>>2); + } else { + char *str = mpz_get_str(NULL, 10, *i.big); + return Text$from_str(str); + } +} + public Text_t Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type) { (void)type; if (!i) return Text("Int"); - if (__builtin_expect(i->small & 1, 1)) { - return Text$format(colorize ? "\x1b[35m%ld\x1b[m" : "%ld", (i->small)>>2); - } else { - char *str = mpz_get_str(NULL, 10, *i->big); - Text_t text = Text$from_str(str); - if (colorize) text = Text$concat(Text("\x1b[35m"), text, Text("\x1b[m")); - return text; - } + Text_t text = Int$value_as_text(*i); + if (colorize) text = Text$concat(Text("\x1b[35m"), text, Text("\x1b[m")); + return text; } public PUREFUNC int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type) { @@ -70,8 +73,7 @@ public PUREFUNC uint64_t Int$hash(const Int_t *x, const TypeInfo *type) { } } -public Text_t Int$format(Int_t i, Int_t digits_int) -{ +public Text_t Int$format(Int_t i, Int_t digits_int) { int64_t digits = Int_to_Int64(digits_int, false); if (__builtin_expect(i.small & 1, 1)) { return Text$format("%0.*ld", digits, (i.small)>>2); diff --git a/builtins/integers.h b/builtins/integers.h index 4a6416c1..641d4e28 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -82,6 +82,7 @@ DEFINE_INT_TYPE(int8_t, Int8) #define OptionalInt_t Int_t Text_t Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type); +Text_t Int$value_as_text(Int_t i); PUREFUNC uint64_t Int$hash(const Int_t *x, const TypeInfo *type); PUREFUNC int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type); PUREFUNC int32_t Int$compare_value(const Int_t x, const Int_t y); |
