-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fixes #15280 [backport:1.2] * make tests green again * adapt tests (cherry picked from commit 3f00a73)
- Loading branch information
Showing
3 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
discard """ | ||
errormsg: "expression 'len(src) shl 1' is of type 'int' and has to be used (or discarded)" | ||
line: 19 | ||
""" | ||
|
||
# bug #15280 | ||
|
||
type | ||
HexFlags* {.pure.} = enum | ||
LowerCase, ## Produce lowercase hexadecimal characters | ||
PadOdd, ## Pads odd strings | ||
SkipSpaces, ## Skips all the whitespace characters inside of string | ||
SkipPrefix ## Skips `0x` and `x` prefixes at the begining of string | ||
|
||
|
||
proc bytesToHex*(src: openarray[byte], dst: var openarray[char], | ||
flags: set[HexFlags]): int = | ||
if len(dst) == 0: | ||
(len(src) shl 1) | ||
else: | ||
var halflast = false | ||
let dstlen = len(dst) | ||
var srclen = len(src) | ||
|
||
if dstlen < (srclen shl 1): | ||
if (dstlen and 1) == 1: | ||
srclen = (dstlen - 1) shr 1 | ||
halflast = true | ||
else: | ||
srclen = (dstlen shr 1) | ||
|
||
let lowercase = (HexFlags.LowerCase in flags) | ||
|
||
var k = 0 | ||
for i in 0 ..< srclen: | ||
let x = int(src[i]) | ||
inc(k, 2) | ||
|
||
if halflast: | ||
let x = int(src[srclen]) | ||
inc(k) | ||
|
||
return k | ||
|