From 5eb47f8ed920a909eb6999ae8fb683d688c48ec5 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sun, 2 Jun 2019 14:24:38 +0200 Subject: [PATCH] fixes #11369 (#11381) --- lib/pure/strutils.nim | 20 ++++++++++---------- tests/stdlib/tstrutil.nim | 7 +++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 7e6d31727e..114141e458 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -892,14 +892,14 @@ proc toBin*(x: BiggestInt, len: Positive): string {.noSideEffect, doAssert b.toBin(8) == "00000001" doAssert b.toBin(9) == "100000001" var - mask: BiggestInt = 1 - shift: BiggestInt = 0 + mask = BiggestUInt 1 + shift = BiggestUInt 0 assert(len > 0) result = newString(len) for j in countdown(len-1, 0): - result[j] = chr(int((x and mask) shr shift) + ord('0')) - shift = shift + 1 - mask = mask shl 1 + result[j] = chr(int((BiggestUInt(x) and mask) shr shift) + ord('0')) + inc shift + mask = mask shl BiggestUInt(1) proc toOct*(x: BiggestInt, len: Positive): string {.noSideEffect, rtl, extern: "nsuToOct".} = @@ -917,14 +917,14 @@ proc toOct*(x: BiggestInt, len: Positive): string {.noSideEffect, doAssert b.toOct(3) == "001" doAssert b.toOct(5) == "01001" var - mask: BiggestInt = 7 - shift: BiggestInt = 0 + mask = BiggestUInt 7 + shift = BiggestUInt 0 assert(len > 0) result = newString(len) for j in countdown(len-1, 0): - result[j] = chr(int((x and mask) shr shift) + ord('0')) - shift = shift + 3 - mask = mask shl 3 + result[j] = chr(int((BiggestUInt(x) and mask) shr shift) + ord('0')) + inc shift, 3 + mask = mask shl BiggestUInt(3) proc toHex*(x: BiggestInt, len: Positive): string {.noSideEffect, rtl, extern: "nsuToHex".} = diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim index fffa85bd1a..f830205047 100644 --- a/tests/stdlib/tstrutil.nim +++ b/tests/stdlib/tstrutil.nim @@ -308,6 +308,13 @@ assert(' '.repeat(0) == "") assert(" ".repeat(0) == "") assert(spaces(0) == "") +# bug #11369 + +var num: int64 = -1 +assert num.toBin(64) == "1111111111111111111111111111111111111111111111111111111111111111" +assert num.toOct(24) == "001777777777777777777777" + + # bug #8911 when true: static: