rm copy of parseHex() from 1.2+ stdlib; some proc -> func (#197)
This commit is contained in:
parent
273ce152a6
commit
607059fe31
|
@ -10,13 +10,13 @@ when (NimMajor, NimMinor) < (1, 4):
|
||||||
else:
|
else:
|
||||||
{.push raises: [].}
|
{.push raises: [].}
|
||||||
|
|
||||||
proc ipv4*(address: array[4, byte]): ValidIpAddress =
|
func ipv4*(address: array[4, byte]): ValidIpAddress =
|
||||||
ValidIpAddress(value: IpAddress(family: IPv4, address_v4: address))
|
ValidIpAddress(value: IpAddress(family: IPv4, address_v4: address))
|
||||||
|
|
||||||
template ipv4*(a, b, c, d: byte): ValidIpAddress =
|
template ipv4*(a, b, c, d: byte): ValidIpAddress =
|
||||||
ipv4([a, b, c, d])
|
ipv4([a, b, c, d])
|
||||||
|
|
||||||
proc ipv6*(address: array[16, byte]): ValidIpAddress =
|
func ipv6*(address: array[16, byte]): ValidIpAddress =
|
||||||
ValidIpAddress(value: IpAddress(family: IPv6, address_v6: address))
|
ValidIpAddress(value: IpAddress(family: IPv6, address_v6: address))
|
||||||
|
|
||||||
template family*(a: ValidIpAddress): IpAddressFamily =
|
template family*(a: ValidIpAddress): IpAddressFamily =
|
||||||
|
@ -42,4 +42,3 @@ converter toNormalIp*(ip: ValidIpAddress): IpAddress =
|
||||||
ip.value
|
ip.value
|
||||||
|
|
||||||
func default*(T: type ValidIpAddress): T {.error.}
|
func default*(T: type ValidIpAddress): T {.error.}
|
||||||
|
|
||||||
|
|
|
@ -1,59 +1,3 @@
|
||||||
# From: https://github.com/nim-lang/Nim/pull/11067/
|
|
||||||
proc parseHex*[T: SomeInteger](s: string, number: var T, start = 0, maxLen = 0): int
|
|
||||||
{.inline, noSideEffect.} =
|
|
||||||
## Parses a hexadecimal number and stores its value in ``number``.
|
|
||||||
##
|
|
||||||
## Returns the number of the parsed characters or 0 in case of an error.
|
|
||||||
## If error, the value of ``number`` is not changed.
|
|
||||||
##
|
|
||||||
## If ``maxLen == 0``, the parsing continues until the first non-hex character
|
|
||||||
## or to the end of the string. Otherwise, no more than ``maxLen`` characters
|
|
||||||
## are parsed starting from the ``start`` position.
|
|
||||||
##
|
|
||||||
## It does not check for overflow. If the value represented by the string is
|
|
||||||
## too big to fit into ``number``, only the value of last fitting characters
|
|
||||||
## will be stored in ``number`` without producing an error.
|
|
||||||
runnableExamples:
|
|
||||||
var num: int
|
|
||||||
doAssert parseHex("4E_69_ED", num) == 8
|
|
||||||
doAssert num == 5138925
|
|
||||||
doAssert parseHex("X", num) == 0
|
|
||||||
doAssert parseHex("#ABC", num) == 4
|
|
||||||
var num8: int8
|
|
||||||
doAssert parseHex("0x_4E_69_ED", num8) == 11
|
|
||||||
doAssert num8 == 0xED'i8
|
|
||||||
doAssert parseHex("0x_4E_69_ED", num8, 3, 2) == 2
|
|
||||||
doAssert num8 == 0x4E'i8
|
|
||||||
var num8u: uint8
|
|
||||||
doAssert parseHex("0x_4E_69_ED", num8u) == 11
|
|
||||||
doAssert num8u == 237
|
|
||||||
var num64: int64
|
|
||||||
doAssert parseHex("4E69ED4E69ED", num64) == 12
|
|
||||||
doAssert num64 == 86216859871725
|
|
||||||
var i = start
|
|
||||||
var output = T(0)
|
|
||||||
var foundDigit = false
|
|
||||||
let last = min(s.len, if maxLen == 0: s.len else: i + maxLen)
|
|
||||||
if i + 1 < last and s[i] == '0' and (s[i+1] in {'x', 'X'}): inc(i, 2)
|
|
||||||
elif i < last and s[i] == '#': inc(i)
|
|
||||||
while i < last:
|
|
||||||
case s[i]
|
|
||||||
of '_': discard
|
|
||||||
of '0'..'9':
|
|
||||||
output = output shl 4 or T(ord(s[i]) - ord('0'))
|
|
||||||
foundDigit = true
|
|
||||||
of 'a'..'f':
|
|
||||||
output = output shl 4 or T(ord(s[i]) - ord('a') + 10)
|
|
||||||
foundDigit = true
|
|
||||||
of 'A'..'F':
|
|
||||||
output = output shl 4 or T(ord(s[i]) - ord('A') + 10)
|
|
||||||
foundDigit = true
|
|
||||||
else: break
|
|
||||||
inc(i)
|
|
||||||
if foundDigit:
|
|
||||||
number = output
|
|
||||||
result = i - start
|
|
||||||
|
|
||||||
import std/parseutils; export parseutils
|
import std/parseutils; export parseutils
|
||||||
#From https://github.com/nim-lang/Nim/pull/21349 in -devel and in version-1-6:
|
#From https://github.com/nim-lang/Nim/pull/21349 in -devel and in version-1-6:
|
||||||
# https://github.com/nim-lang/Nim/commit/c546ba5d23bb2e7bc562a071c88efd94cca7b89e
|
# https://github.com/nim-lang/Nim/commit/c546ba5d23bb2e7bc562a071c88efd94cca7b89e
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
proc add*(str: var string, chars: openArray[char]) =
|
func add*(str: var string, chars: openArray[char]) =
|
||||||
for c in chars:
|
for c in chars:
|
||||||
str.add c
|
str.add c
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import ../stew/base58
|
||||||
|
|
||||||
when defined(nimHasUsed): {.used.}
|
when defined(nimHasUsed): {.used.}
|
||||||
|
|
||||||
proc hexToBytes*(a: string, result: var openArray[byte]) =
|
func hexToBytes*(a: string, result: var openArray[byte]) =
|
||||||
doAssert(len(a) == 2 * len(result))
|
doAssert(len(a) == 2 * len(result))
|
||||||
var i = 0
|
var i = 0
|
||||||
var k = 0
|
var k = 0
|
||||||
|
@ -29,7 +29,7 @@ proc hexToBytes*(a: string, result: var openArray[byte]) =
|
||||||
inc(i)
|
inc(i)
|
||||||
result[k] = r.byte
|
result[k] = r.byte
|
||||||
|
|
||||||
proc fromHex*(a: string): seq[byte] =
|
func fromHex*(a: string): seq[byte] =
|
||||||
doAssert(len(a) %% 2 == 0)
|
doAssert(len(a) %% 2 == 0)
|
||||||
if len(a) == 0:
|
if len(a) == 0:
|
||||||
result = newSeq[byte]()
|
result = newSeq[byte]()
|
||||||
|
|
|
@ -110,7 +110,7 @@ template test() =
|
||||||
# T(1 shl 63) raises!
|
# T(1 shl 63) raises!
|
||||||
doAssert bit64 == 0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000'u64
|
doAssert bit64 == 0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000'u64
|
||||||
|
|
||||||
proc runtimeTest =
|
func runtimeTest =
|
||||||
var bytes = @[byte 0b11001101, 0b10010010, 0b00000000, 0b11111111,
|
var bytes = @[byte 0b11001101, 0b10010010, 0b00000000, 0b11111111,
|
||||||
0b11000010, 0b00110110, 0b11010110, 0b00101010,
|
0b11000010, 0b00110110, 0b11010110, 0b00101010,
|
||||||
0b01101110, 0b11101001, 0b10101011, 0b00110010]
|
0b01101110, 0b11101001, 0b10101011, 0b00110010]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Nimbus - Types, data structures and shared utilities used in network sync
|
# Nimbus - Types, data structures and shared utilities used in network sync
|
||||||
#
|
#
|
||||||
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
||||||
# Licensed under either of
|
# Licensed under either of
|
||||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0)
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
@ -55,57 +55,57 @@ else:
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
# use a sub-range for `FancyPoint` elements
|
# use a sub-range for `FancyPoint` elements
|
||||||
proc high(T: type FancyPoint): T = uHigh.to(FancyPoint)
|
func high(T: type FancyPoint): T = uHigh.to(FancyPoint)
|
||||||
proc low(T: type FancyPoint): T = uLow.to(FancyPoint)
|
func low(T: type FancyPoint): T = uLow.to(FancyPoint)
|
||||||
|
|
||||||
proc to(num: FancyPoint; T: type FancyScalar): T = num.T
|
func to(num: FancyPoint; T: type FancyScalar): T = num.T
|
||||||
proc `$`(num: FancyPoint): string = $num.to(FancyScalar)
|
func `$`(num: FancyPoint): string = $num.to(FancyScalar)
|
||||||
|
|
||||||
proc `+`*(a: FancyPoint; b: FancyScalar): FancyPoint =
|
func `+`*(a: FancyPoint; b: FancyScalar): FancyPoint =
|
||||||
(a.to(FancyScalar) + b).FancyPoint
|
(a.to(FancyScalar) + b).FancyPoint
|
||||||
|
|
||||||
proc `-`*(a: FancyPoint; b: FancyScalar): FancyPoint =
|
func `-`*(a: FancyPoint; b: FancyScalar): FancyPoint =
|
||||||
(a.to(FancyScalar) - b).FancyPoint
|
(a.to(FancyScalar) - b).FancyPoint
|
||||||
|
|
||||||
proc `-`*(a, b: FancyPoint): FancyScalar =
|
func `-`*(a, b: FancyPoint): FancyScalar =
|
||||||
(a.to(FancyScalar) - b.to(FancyScalar))
|
(a.to(FancyScalar) - b.to(FancyScalar))
|
||||||
|
|
||||||
proc `==`*(a, b: FancyPoint): bool = a.to(FancyScalar) == b.to(FancyScalar)
|
func `==`*(a, b: FancyPoint): bool = a.to(FancyScalar) == b.to(FancyScalar)
|
||||||
proc `<=`*(a, b: FancyPoint): bool = a.to(FancyScalar) <= b.to(FancyScalar)
|
func `<=`*(a, b: FancyPoint): bool = a.to(FancyScalar) <= b.to(FancyScalar)
|
||||||
proc `<`*(a, b: FancyPoint): bool = a.to(FancyScalar) < b.to(FancyScalar)
|
func `<`*(a, b: FancyPoint): bool = a.to(FancyScalar) < b.to(FancyScalar)
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Private functions
|
# Private functions
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
proc truncate(num: FancyPoint; T: type uint64): uint64 =
|
func truncate(num: FancyPoint; T: type uint64): uint64 =
|
||||||
num.to(FancyScalar).truncate(uint64)
|
num.to(FancyScalar).truncate(uint64)
|
||||||
|
|
||||||
proc merge(br: FancyRanges; left, right: uint64): uint64 =
|
func merge(br: FancyRanges; left, right: uint64): uint64 =
|
||||||
let (a, b) = (left.to(FancyPoint), right.to(FancyPoint))
|
let (a, b) = (left.to(FancyPoint), right.to(FancyPoint))
|
||||||
br.merge(a, b).truncate(uint64)
|
br.merge(a, b).truncate(uint64)
|
||||||
|
|
||||||
proc reduce(br: FancyRanges; left, right: uint64): uint64 =
|
func reduce(br: FancyRanges; left, right: uint64): uint64 =
|
||||||
let (a, b) = (left.to(FancyPoint), right.to(FancyPoint))
|
let (a, b) = (left.to(FancyPoint), right.to(FancyPoint))
|
||||||
br.reduce(a, b).truncate(uint64)
|
br.reduce(a, b).truncate(uint64)
|
||||||
|
|
||||||
proc covered(br: FancyRanges; left, right: uint64): uint64 =
|
func covered(br: FancyRanges; left, right: uint64): uint64 =
|
||||||
let (a, b) = (left.to(FancyPoint), right.to(FancyPoint))
|
let (a, b) = (left.to(FancyPoint), right.to(FancyPoint))
|
||||||
br.covered(a, b).truncate(uint64)
|
br.covered(a, b).truncate(uint64)
|
||||||
|
|
||||||
proc delete(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
func delete(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
||||||
br.delete(start.to(FancyPoint))
|
br.delete(start.to(FancyPoint))
|
||||||
|
|
||||||
proc le(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
func le(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
||||||
br.le(start.to(FancyPoint))
|
br.le(start.to(FancyPoint))
|
||||||
|
|
||||||
proc ge(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
func ge(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
||||||
br.ge(start.to(FancyPoint))
|
br.ge(start.to(FancyPoint))
|
||||||
|
|
||||||
proc envelope(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
func envelope(br: FancyRanges; start: uint64): Result[FancyInterval,void] =
|
||||||
br.envelope(start.to(FancyPoint))
|
br.envelope(start.to(FancyPoint))
|
||||||
|
|
||||||
proc iv(left, right: uint64): FancyInterval =
|
func iv(left, right: uint64): FancyInterval =
|
||||||
FancyInterval.new(left.to(FancyPoint), right.to(FancyPoint))
|
FancyInterval.new(left.to(FancyPoint), right.to(FancyPoint))
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Nimbus
|
# Nimbus
|
||||||
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
||||||
# Licensed under either of
|
# Licensed under either of
|
||||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0)
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
@ -45,16 +45,16 @@ let
|
||||||
# Debugging
|
# Debugging
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
proc `$`(rc: KeyedQueuePair[uint,uint]): string =
|
func `$`(rc: KeyedQueuePair[uint,uint]): string =
|
||||||
"(" & $rc.key & "," & $rc.data & ")"
|
"(" & $rc.key & "," & $rc.data & ")"
|
||||||
|
|
||||||
proc `$`(rc: Result[KeyedQueuePair[uint,uint],void]): string {.used.} =
|
func `$`(rc: Result[KeyedQueuePair[uint,uint],void]): string {.used.} =
|
||||||
result = "<"
|
result = "<"
|
||||||
if rc.isOk:
|
if rc.isOk:
|
||||||
result &= $rc.value.key & "," & $rc.value.data
|
result &= $rc.value.key & "," & $rc.value.data
|
||||||
result &= ">"
|
result &= ">"
|
||||||
|
|
||||||
proc `$`(rc: Result[uint,void]): string =
|
func `$`(rc: Result[uint,void]): string =
|
||||||
result = "<"
|
result = "<"
|
||||||
if rc.isOk:
|
if rc.isOk:
|
||||||
result &= $rc.value
|
result &= $rc.value
|
||||||
|
@ -73,23 +73,23 @@ proc say(noisy = false; pfx = "***"; args: varargs[string, `$`]) =
|
||||||
# Converters
|
# Converters
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
proc toValue(n: int): uint =
|
func toValue(n: int): uint =
|
||||||
(n + 1000).uint
|
(n + 1000).uint
|
||||||
|
|
||||||
proc fromValue(n: uint): int =
|
func fromValue(n: uint): int =
|
||||||
(n - 1000).int
|
(n - 1000).int
|
||||||
|
|
||||||
proc toKey(n: int): uint =
|
func toKey(n: int): uint =
|
||||||
n.uint
|
n.uint
|
||||||
|
|
||||||
proc fromKey(n: uint): int =
|
func fromKey(n: uint): int =
|
||||||
n.int
|
n.int
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Helpers
|
# Helpers
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
proc lruValue(lru: var LruCache; n: int): uint =
|
func lruValue(lru: var LruCache; n: int): uint =
|
||||||
let
|
let
|
||||||
key = n.toKey
|
key = n.toKey
|
||||||
rc = lru.q.lruFetch(key)
|
rc = lru.q.lruFetch(key)
|
||||||
|
@ -100,16 +100,16 @@ proc lruValue(lru: var LruCache; n: int): uint =
|
||||||
result = lru.q.lruAppend(key, key.fromKey.toValue, lru.size)
|
result = lru.q.lruAppend(key, key.fromKey.toValue, lru.size)
|
||||||
doAssert lru.q.verify.isOk
|
doAssert lru.q.verify.isOk
|
||||||
|
|
||||||
proc toLruCache(a: openArray[int]): LruCache =
|
func toLruCache(a: openArray[int]): LruCache =
|
||||||
result.size = lruCacheLimit
|
result.size = lruCacheLimit
|
||||||
for n in a.toSeq.mapIt(it mod lruCacheModulo):
|
for n in a.toSeq.mapIt(it mod lruCacheModulo):
|
||||||
doAssert result.lruValue(n) == n.toValue
|
doAssert result.lruValue(n) == n.toValue
|
||||||
|
|
||||||
proc toQueue(a: openArray[int]): KUQueue =
|
func toQueue(a: openArray[int]): KUQueue =
|
||||||
for n in a:
|
for n in a:
|
||||||
result[n.toKey] = n.toValue
|
result[n.toKey] = n.toValue
|
||||||
|
|
||||||
proc toUnique(a: openArray[int]): seq[uint] =
|
func toUnique(a: openArray[int]): seq[uint] =
|
||||||
var q = a.toQueue
|
var q = a.toQueue
|
||||||
toSeq(q.nextKeys)
|
toSeq(q.nextKeys)
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ proc addOrFlushGroupwise(rq: var KUQueue;
|
||||||
doAssert rqLen == seen.len + rq.len
|
doAssert rqLen == seen.len + rq.len
|
||||||
seen.setLen(0)
|
seen.setLen(0)
|
||||||
|
|
||||||
proc compileGenericFunctions(rq: var KUQueue) =
|
func compileGenericFunctions(rq: var KUQueue) =
|
||||||
## Verifies that functions compile, at all
|
## Verifies that functions compile, at all
|
||||||
rq.del(0)
|
rq.del(0)
|
||||||
rq[0] = 0 # so `rq[0]` works
|
rq[0] = 0 # so `rq[0]` works
|
||||||
|
|
Loading…
Reference in New Issue