authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-22 10:23:18-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-03-22 10:23:18-04:00
log127bb124a06dc93f4def23224d400f4050754674
treec614b7a47f220378f3fb4149a0083ee144d13b63
parent3560f61c1630c36dddaf46e88efc0ccc7b91cf44
parenta3f42a5fe1bf483245e87d1084edd9ea21bce0f7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

Fix bigint_append_buf

2 files changed, 8 insertions(+), 4 deletions(-)

src/bigint.cpp+7-3
...@@ -40,7 +40,7 @@ static uint8_t digit_to_char(uint8_t digit, bool uppercase) {...@@ -40,7 +40,7 @@ static uint8_t digit_to_char(uint8_t digit, bool uppercase) {
40 if (digit <= 9) {40 if (digit <= 9) {
41 return digit + '0';41 return digit + '0';
42 } else if (digit <= 35) {42 } else if (digit <= 35) {
43 return digit + (uppercase ? 'A' : 'a');43 return (digit - 10) + (uppercase ? 'A' : 'a');
44 } else {44 } else {
45 zig_unreachable();45 zig_unreachable();
46 }46 }
...@@ -1545,6 +1545,10 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) {...@@ -1545,6 +1545,10 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) {
1545 buf_appendf(buf, "%" ZIG_PRI_u64, op->data.digit);1545 buf_appendf(buf, "%" ZIG_PRI_u64, op->data.digit);
1546 return;1546 return;
1547 }1547 }
1548 if (op->digit_count == 1 && base == 16) {
1549 buf_appendf(buf, "%" ZIG_PRI_x64, op->data.digit);
1550 return;
1551 }
1548 size_t first_digit_index = buf_len(buf);1552 size_t first_digit_index = buf_len(buf);
15491553
1550 BigInt digit_bi = {0};1554 BigInt digit_bi = {0};
...@@ -1556,7 +1560,7 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) {...@@ -1556,7 +1560,7 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) {
1556 bigint_init_bigint(a, op);1560 bigint_init_bigint(a, op);
15571561
1558 BigInt base_bi = {0};1562 BigInt base_bi = {0};
1559 bigint_init_unsigned(&base_bi, 10);1563 bigint_init_unsigned(&base_bi, base);
15601564
1561 for (;;) {1565 for (;;) {
1562 bigint_rem(&digit_bi, a, &base_bi);1566 bigint_rem(&digit_bi, a, &base_bi);
...@@ -1574,7 +1578,7 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) {...@@ -1574,7 +1578,7 @@ void bigint_append_buf(Buf *buf, const BigInt *op, uint64_t base) {
1574 }1578 }
15751579
1576 // reverse1580 // reverse
1577 for (size_t i = first_digit_index; i < buf_len(buf); i += 1) {1581 for (size_t i = first_digit_index; i < buf_len(buf) / 2; i += 1) {
1578 size_t other_i = buf_len(buf) + first_digit_index - i - 1;1582 size_t other_i = buf_len(buf) + first_digit_index - i - 1;
1579 uint8_t tmp = buf_ptr(buf)[i];1583 uint8_t tmp = buf_ptr(buf)[i];
1580 buf_ptr(buf)[i] = buf_ptr(buf)[other_i];1584 buf_ptr(buf)[i] = buf_ptr(buf)[other_i];
test/compile_errors.zig+1-1
...@@ -441,7 +441,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -441,7 +441,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
441 \\ var ptr: [*c]u8 = x;441 \\ var ptr: [*c]u8 = x;
442 \\}442 \\}
443 ,443 ,
444 "tmp.zig:2:33: error: integer value 71615590737044764481 cannot be implicitly casted to type 'usize'",444 "tmp.zig:2:33: error: integer value 18446744073709551617 cannot be implicitly casted to type 'usize'",
445 "tmp.zig:6:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'",445 "tmp.zig:6:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'",
446 );446 );
447447