zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 127bb124a06dc93f4def23224d400f4050754674 (tree)
parent 3560f61c1630c36dddaf46e88efc0ccc7b91cf44
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Fri, 22 Mar 2019 10:23:18 -0400

Merge pull request #2091 from ziglang/bigint-print-fix

Fix bigint_append_buf
Diffstat:
Msrc/bigint.cpp | 10+++++++---
Mtest/compile_errors.zig | 2+-
2 files changed, 8 insertions(+), 4 deletions(-)

diff --git 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]; diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -441,7 +441,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var ptr: [*c]u8 = x; \\} , - "tmp.zig:2:33: error: integer value 71615590737044764481 cannot be implicitly casted to type 'usize'", + "tmp.zig:2:33: error: integer value 18446744073709551617 cannot be implicitly casted to type 'usize'", "tmp.zig:6:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'", );