mirror of
https://github.com/status-im/nim-stew.git
synced 2025-01-24 02:50:18 +00:00
reenable Defect raises for Nim 1.2
This commit is contained in:
parent
faabd40c30
commit
6cc42beac7
@ -18,14 +18,16 @@ export arrayops.`&`, arrayops.initArrayWith, arrayops.`[]=`
|
|||||||
|
|
||||||
when (NimMajor, NimMinor) < (1, 4):
|
when (NimMajor, NimMinor) < (1, 4):
|
||||||
{.push raises: [Defect].}
|
{.push raises: [Defect].}
|
||||||
|
{.pragma: hexRaises, raises: [Defect, ValueError].}
|
||||||
else:
|
else:
|
||||||
{.push raises: [].}
|
{.push raises: [].}
|
||||||
|
{.pragma: hexRaises, raises: [ValueError].}
|
||||||
|
|
||||||
########################################################################################################
|
########################################################################################################
|
||||||
##################################### Hex utilities ################################################
|
##################################### Hex utilities ################################################
|
||||||
|
|
||||||
proc readHexChar*(c: char): byte
|
proc readHexChar*(c: char): byte
|
||||||
{.raises: [ValueError], noSideEffect, inline.} =
|
{.hexRaises, noSideEffect, inline.} =
|
||||||
## Converts an hex char to a byte
|
## Converts an hex char to a byte
|
||||||
case c
|
case c
|
||||||
of '0'..'9': result = byte(ord(c) - ord('0'))
|
of '0'..'9': result = byte(ord(c) - ord('0'))
|
||||||
@ -42,7 +44,7 @@ template skip0xPrefix(hexStr: openArray[char]): int =
|
|||||||
|
|
||||||
func hexToByteArrayImpl(
|
func hexToByteArrayImpl(
|
||||||
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int):
|
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int):
|
||||||
int {.raises: [ValueError].} =
|
int {.hexRaises.} =
|
||||||
var sIdx = skip0xPrefix(hexStr)
|
var sIdx = skip0xPrefix(hexStr)
|
||||||
# Fun with closed intervals
|
# Fun with closed intervals
|
||||||
doAssert fromIdx >= 0 and
|
doAssert fromIdx >= 0 and
|
||||||
@ -65,7 +67,7 @@ func hexToByteArrayImpl(
|
|||||||
|
|
||||||
func hexToByteArray*(
|
func hexToByteArray*(
|
||||||
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int)
|
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int)
|
||||||
{.raises: [ValueError].} =
|
{.hexRaises.} =
|
||||||
## Read hex-encoded data from `hexStr[mapHex(fromIdx..toIdx)]` and store
|
## Read hex-encoded data from `hexStr[mapHex(fromIdx..toIdx)]` and store
|
||||||
## corresponding bytes in `output[fromIdx..toIdx]` where `mapHex` takes into
|
## corresponding bytes in `output[fromIdx..toIdx]` where `mapHex` takes into
|
||||||
## account stripped characters.
|
## account stripped characters.
|
||||||
@ -80,7 +82,7 @@ func hexToByteArray*(
|
|||||||
discard hexToByteArrayImpl(hexStr, output, fromIdx, toIdx)
|
discard hexToByteArrayImpl(hexStr, output, fromIdx, toIdx)
|
||||||
|
|
||||||
func hexToByteArray*(hexStr: openArray[char], output: var openArray[byte])
|
func hexToByteArray*(hexStr: openArray[char], output: var openArray[byte])
|
||||||
{.raises: [ValueError].} =
|
{.hexRaises.} =
|
||||||
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
||||||
## `output`.
|
## `output`.
|
||||||
##
|
##
|
||||||
@ -92,7 +94,7 @@ func hexToByteArray*(hexStr: openArray[char], output: var openArray[byte])
|
|||||||
hexToByteArray(hexStr, output, 0, output.high)
|
hexToByteArray(hexStr, output, 0, output.high)
|
||||||
|
|
||||||
func hexToByteArray*[N: static[int]](hexStr: openArray[char]): array[N, byte]
|
func hexToByteArray*[N: static[int]](hexStr: openArray[char]): array[N, byte]
|
||||||
{.raises: [ValueError], noinit.}=
|
{.hexRaises, noinit.}=
|
||||||
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
||||||
##
|
##
|
||||||
## * `0x`/`0X` is stripped if present
|
## * `0x`/`0X` is stripped if present
|
||||||
@ -103,7 +105,7 @@ func hexToByteArray*[N: static[int]](hexStr: openArray[char]): array[N, byte]
|
|||||||
hexToByteArray(hexStr, result)
|
hexToByteArray(hexStr, result)
|
||||||
|
|
||||||
func hexToByteArray*(hexStr: openArray[char], N: static int): array[N, byte]
|
func hexToByteArray*(hexStr: openArray[char], N: static int): array[N, byte]
|
||||||
{.raises: [ValueError], noinit.}=
|
{.hexRaises, noinit.}=
|
||||||
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
||||||
##
|
##
|
||||||
## * `0x`/`0X` is stripped if present
|
## * `0x`/`0X` is stripped if present
|
||||||
@ -114,7 +116,7 @@ func hexToByteArray*(hexStr: openArray[char], N: static int): array[N, byte]
|
|||||||
hexToByteArray(hexStr, result)
|
hexToByteArray(hexStr, result)
|
||||||
|
|
||||||
func hexToByteArrayStrict*(hexStr: openArray[char], output: var openArray[byte])
|
func hexToByteArrayStrict*(hexStr: openArray[char], output: var openArray[byte])
|
||||||
{.raises: [ValueError].} =
|
{.hexRaises.} =
|
||||||
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
||||||
## `output`.
|
## `output`.
|
||||||
##
|
##
|
||||||
@ -126,7 +128,7 @@ func hexToByteArrayStrict*(hexStr: openArray[char], output: var openArray[byte])
|
|||||||
raise (ref ValueError)(msg: "hex string too long")
|
raise (ref ValueError)(msg: "hex string too long")
|
||||||
|
|
||||||
func hexToByteArrayStrict*[N: static[int]](hexStr: openArray[char]): array[N, byte]
|
func hexToByteArrayStrict*[N: static[int]](hexStr: openArray[char]): array[N, byte]
|
||||||
{.raises: [ValueError, Defect], noinit, inline.}=
|
{.hexRaises, noinit, inline.}=
|
||||||
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
||||||
## `output`.
|
## `output`.
|
||||||
##
|
##
|
||||||
@ -137,7 +139,7 @@ func hexToByteArrayStrict*[N: static[int]](hexStr: openArray[char]): array[N, by
|
|||||||
hexToByteArrayStrict(hexStr, result)
|
hexToByteArrayStrict(hexStr, result)
|
||||||
|
|
||||||
func hexToByteArrayStrict*(hexStr: openArray[char], N: static int): array[N, byte]
|
func hexToByteArrayStrict*(hexStr: openArray[char], N: static int): array[N, byte]
|
||||||
{.raises: [ValueError, Defect], noinit, inline.}=
|
{.hexRaises, noinit, inline.}=
|
||||||
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
||||||
## `output`.
|
## `output`.
|
||||||
##
|
##
|
||||||
@ -148,7 +150,7 @@ func hexToByteArrayStrict*(hexStr: openArray[char], N: static int): array[N, byt
|
|||||||
hexToByteArrayStrict(hexStr, result)
|
hexToByteArrayStrict(hexStr, result)
|
||||||
|
|
||||||
func fromHex*[N](A: type array[N, byte], hexStr: string): A
|
func fromHex*[N](A: type array[N, byte], hexStr: string): A
|
||||||
{.raises: [ValueError], noinit, inline.}=
|
{.hexRaises, noinit, inline.}=
|
||||||
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
||||||
##
|
##
|
||||||
## * `0x`/`0X` is stripped if present
|
## * `0x`/`0X` is stripped if present
|
||||||
@ -159,7 +161,7 @@ func fromHex*[N](A: type array[N, byte], hexStr: string): A
|
|||||||
hexToByteArray(hexStr, result)
|
hexToByteArray(hexStr, result)
|
||||||
|
|
||||||
func hexToPaddedByteArray*[N: static[int]](hexStr: string): array[N, byte]
|
func hexToPaddedByteArray*[N: static[int]](hexStr: string): array[N, byte]
|
||||||
{.raises: [ValueError].} =
|
{.hexRaises.} =
|
||||||
## Read a hex string and store it in a byte array `output`.
|
## Read a hex string and store it in a byte array `output`.
|
||||||
## The string may be shorter than the byte array.
|
## The string may be shorter than the byte array.
|
||||||
## No "endianness" reordering is done.
|
## No "endianness" reordering is done.
|
||||||
@ -188,7 +190,7 @@ func hexToPaddedByteArray*[N: static[int]](hexStr: string): array[N, byte]
|
|||||||
bIdx += shift shr 2
|
bIdx += shift shr 2
|
||||||
|
|
||||||
func hexToSeqByte*(hexStr: string): seq[byte]
|
func hexToSeqByte*(hexStr: string): seq[byte]
|
||||||
{.raises: [ValueError].} =
|
{.hexRaises.} =
|
||||||
## Read an hex string and store it in a sequence of bytes. No "endianness" reordering is done.
|
## Read an hex string and store it in a sequence of bytes. No "endianness" reordering is done.
|
||||||
if (hexStr.len and 1) == 1:
|
if (hexStr.len and 1) == 1:
|
||||||
raise (ref ValueError)(msg: "hex string must have even length")
|
raise (ref ValueError)(msg: "hex string must have even length")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user