diff options
| author | Marc Tiehuis <marctiehuis@gmail.com> | 2019-03-22 22:10:51 +1300 |
|---|---|---|
| committer | Marc Tiehuis <marctiehuis@gmail.com> | 2019-03-22 22:10:51 +1300 |
| commit | 6f90d2c209ccf81498139133ee9ee4023eb176a6 (patch) | |
| tree | 86b85e656df35494a832e8f8826a6fc43436d62e /src/bigint.cpp | |
| parent | b5cc92f16391b1ff5d75e5e6b072b8338b3e8218 (diff) | |
| download | zig-6f90d2c209ccf81498139133ee9ee4023eb176a6.tar.gz zig-6f90d2c209ccf81498139133ee9ee4023eb176a6.zip | |
Fix bigint_append_buf
All current usages use base 10 and have a limb length of 1, hence why
we weren't hitting this error in practice.
Diffstat (limited to 'src/bigint.cpp')
| -rw-r--r-- | src/bigint.cpp | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/bigint.cpp b/src/bigint.cpp index 4c103adce3..d3178c35c6 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -40,7 +40,7 @@ static uint8_t digit_to_char(uint8_t digit, bool uppercase) { if (digit <= 9) { return digit + '0'; } else if (digit <= 35) { - return digit + (uppercase ? 'A' : 'a'); + return (digit - 10) + (uppercase ? 'A' : 'a'); } else { zig_unreachable(); } @@ -1545,6 +1545,10 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) { buf_appendf(buf, "%" ZIG_PRI_u64, op->data.digit); return; } + if (op->digit_count == 1 && base == 16) { + buf_appendf(buf, "%" ZIG_PRI_x64, op->data.digit); + return; + } size_t first_digit_index = buf_len(buf); BigInt digit_bi = {0}; @@ -1556,7 +1560,7 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) { bigint_init_bigint(a, op); BigInt base_bi = {0}; - bigint_init_unsigned(&base_bi, 10); + bigint_init_unsigned(&base_bi, base); for (;;) { bigint_rem(&digit_bi, a, &base_bi); @@ -1574,7 +1578,7 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) { } // reverse - for (size_t i = first_digit_index; i < buf_len(buf); i += 1) { + for (size_t i = first_digit_index; i < buf_len(buf) / 2; i += 1) { size_t other_i = buf_len(buf) + first_digit_index - i - 1; uint8_t tmp = buf_ptr(buf)[i]; buf_ptr(buf)[i] = buf_ptr(buf)[other_i]; |
