enable styleCheck:usages (#93)

This commit is contained in:
Jacek Sieka 2021-12-02 16:24:02 +01:00 committed by GitHub
parent 3b9e906a18
commit 8a405309c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 108 additions and 108 deletions

View File

@ -17,7 +17,7 @@ proc test(env, path: string) =
lang = getEnv"TEST_LANG" lang = getEnv"TEST_LANG"
exec "nim " & lang & " " & env & exec "nim " & lang & " " & env &
" -r --hints:off --skipParentCfg " & path " -r --hints:off --skipParentCfg --styleCheck:usages --styleCheck:error " & path
task test, "Run all tests": task test, "Run all tests":
test "--threads:off", "tests/all_tests" test "--threads:off", "tests/all_tests"

View File

@ -54,13 +54,13 @@ func copyFrom*[T](
assign(v.toOpenArray(0, elems - 1), src.toOpenArray(0, elems - 1)) assign(v.toOpenArray(0, elems - 1), src.toOpenArray(0, elems - 1))
elems elems
func initArrayWith*[N: static[int], T](value: T): array[N, T] {.noInit, inline.}= func initArrayWith*[N: static[int], T](value: T): array[N, T] {.noinit, inline.}=
result.fill(value) result.fill(value)
func `&`*[N1, N2: static[int], T]( func `&`*[N1, N2: static[int], T](
a: array[N1, T], a: array[N1, T],
b: array[N2, T] b: array[N2, T]
): array[N1 + N2, T] {.inline, noInit.}= ): array[N1 + N2, T] {.inline, noinit.}=
## Array concatenation ## Array concatenation
assign(result.toOpenArray(0, N1 - 1), a) assign(result.toOpenArray(0, N1 - 1), a)
assign(result.toOpenArray(N1, result.high), b) assign(result.toOpenArray(N1, result.high), b)

View File

@ -40,7 +40,7 @@ type
len*: int8 # >= 1 when holding valid unsigned integer len*: int8 # >= 1 when holding valid unsigned integer
proc decode*[A: byte|char](B: typedesc[Base10], T: typedesc[SomeUnsignedInt], proc decode*[A: byte|char](B: typedesc[Base10], T: typedesc[SomeUnsignedInt],
src: openarray[A]): Result[T, cstring] = src: openArray[A]): Result[T, cstring] =
## Convert base10 encoded string or array of bytes to unsigned integer. ## Convert base10 encoded string or array of bytes to unsigned integer.
const const
MaxValue = T(high(T) div 10) MaxValue = T(high(T) div 10)
@ -132,7 +132,7 @@ proc encodedLength*(B: typedesc[Base10], value: SomeUnsignedInt): int8 =
return 12'i8 + B.encodedLength(value div P12) return 12'i8 + B.encodedLength(value div P12)
proc encode[A: byte|char](B: typedesc[Base10], value: SomeUnsignedInt, proc encode[A: byte|char](B: typedesc[Base10], value: SomeUnsignedInt,
output: var openarray[A], output: var openArray[A],
length: int8): Result[int8, cstring] = length: int8): Result[int8, cstring] =
const Digits = cstring( const Digits = cstring(
"0001020304050607080910111213141516171819" & "0001020304050607080910111213141516171819" &
@ -175,7 +175,7 @@ proc encode[A: byte|char](B: typedesc[Base10], value: SomeUnsignedInt,
ok(length) ok(length)
proc encode*[A: byte|char](B: typedesc[Base10], value: SomeUnsignedInt, proc encode*[A: byte|char](B: typedesc[Base10], value: SomeUnsignedInt,
output: var openarray[A]): Result[int8, cstring] = output: var openArray[A]): Result[int8, cstring] =
## Encode integer value to array of characters or bytes. ## Encode integer value to array of characters or bytes.
B.encode(value, output, B.encodedLength(value)) B.encode(value, output, B.encodedLength(value))

View File

@ -82,7 +82,7 @@ proc decodedLength*(btype: typedesc[Base32Types], length: int): int =
let reminder = length mod 8 let reminder = length mod 8
result = (length div 8) * 5 + ((reminder * 5) div 8) result = (length div 8) * 5 + ((reminder * 5) div 8)
proc convert5to8(inbytes: openarray[byte], outbytes: var openarray[char], proc convert5to8(inbytes: openArray[byte], outbytes: var openArray[char],
length: int): int {.inline.} = length: int): int {.inline.} =
if length >= 1: if length >= 1:
outbytes[0] = chr(inbytes[0] shr 3) outbytes[0] = chr(inbytes[0] shr 3)
@ -107,7 +107,7 @@ proc convert5to8(inbytes: openarray[byte], outbytes: var openarray[char],
outbytes[7] = chr(inbytes[4] and 31'u8) outbytes[7] = chr(inbytes[4] and 31'u8)
result = 8 result = 8
proc convert8to5(inbytes: openarray[byte], outbytes: var openarray[byte], proc convert8to5(inbytes: openArray[byte], outbytes: var openArray[byte],
length: int): int {.inline.} = length: int): int {.inline.} =
if length >= 2: if length >= 2:
outbytes[0] = inbytes[0] shl 3 outbytes[0] = inbytes[0] shl 3
@ -132,8 +132,8 @@ proc convert8to5(inbytes: openarray[byte], outbytes: var openarray[byte],
outbytes[4] = outbytes[4] or (inbytes[7] and 31'u8) outbytes[4] = outbytes[4] or (inbytes[7] and 31'u8)
result = 5 result = 5
proc encode*(btype: typedesc[Base32Types], inbytes: openarray[byte], proc encode*(btype: typedesc[Base32Types], inbytes: openArray[byte],
outstr: var openarray[char], outlen: var int): Base32Status = outstr: var openArray[char], outlen: var int): Base32Status =
## Encode array of bytes ``inbytes`` using BASE32 encoding and store ## Encode array of bytes ``inbytes`` using BASE32 encoding and store
## result to ``outstr``. On success ``Base32Status.Success`` will be returned ## result to ``outstr``. On success ``Base32Status.Success`` will be returned
## and ``outlen`` will be set to number of characters stored inside of ## and ``outlen`` will be set to number of characters stored inside of
@ -184,7 +184,7 @@ proc encode*(btype: typedesc[Base32Types], inbytes: openarray[byte],
result = Base32Status.Success result = Base32Status.Success
proc encode*(btype: typedesc[Base32Types], proc encode*(btype: typedesc[Base32Types],
inbytes: openarray[byte]): string {.inline.} = inbytes: openArray[byte]): string {.inline.} =
## Encode array of bytes ``inbytes`` using BASE32 encoding and return ## Encode array of bytes ``inbytes`` using BASE32 encoding and return
## encoded string. ## encoded string.
if len(inbytes) == 0: if len(inbytes) == 0:
@ -197,8 +197,8 @@ proc encode*(btype: typedesc[Base32Types],
else: else:
result = "" result = ""
proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openarray[T], proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openArray[T],
outbytes: var openarray[byte], outlen: var int): Base32Status = outbytes: var openArray[byte], outlen: var int): Base32Status =
## Decode BASE32 string and store array of bytes to ``outbytes``. On success ## Decode BASE32 string and store array of bytes to ``outbytes``. On success
## ``Base32Status.Success`` will be returned and ``outlen`` will be set ## ``Base32Status.Success`` will be returned and ``outlen`` will be set
## to number of bytes stored. ## to number of bytes stored.
@ -271,7 +271,7 @@ proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openarray[T],
result = Base32Status.Success result = Base32Status.Success
proc decode*[T: byte|char](btype: typedesc[Base32Types], proc decode*[T: byte|char](btype: typedesc[Base32Types],
instr: openarray[T]): seq[byte] = instr: openArray[T]): seq[byte] =
## Decode BASE32 string ``instr`` and return sequence of bytes as result. ## Decode BASE32 string ``instr`` and return sequence of bytes as result.
if len(instr) == 0: if len(instr) == 0:
result = newSeq[byte]() result = newSeq[byte]()

View File

@ -24,7 +24,7 @@ type
## Type to use Bitcoin alphabet ## Type to use Bitcoin alphabet
FLCBase58* = object FLCBase58* = object
## Type to use Flickr alphabet ## Type to use Flickr alphabet
Base58* = BtcBase58 Base58* = BTCBase58
## By default we are using Bitcoin alphabet ## By default we are using Bitcoin alphabet
Base58C* = BTCBase58 | FLCBase58 Base58C* = BTCBase58 | FLCBase58
## Supported types ## Supported types
@ -57,8 +57,8 @@ proc decodedLength*(btype: typedesc[Base58C], length: int): int =
## ``length``. ## ``length``.
result = length + 4 result = length + 4
proc encode*(btype: typedesc[Base58C], inbytes: openarray[byte], proc encode*(btype: typedesc[Base58C], inbytes: openArray[byte],
outstr: var openarray[char], outlen: var int): Base58Status = outstr: var openArray[char], outlen: var int): Base58Status =
## Encode array of bytes ``inbytes`` using BASE58 encoding and store ## Encode array of bytes ``inbytes`` using BASE58 encoding and store
## result to ``outstr``. On success ``Base58Status.Success`` will be returned ## result to ``outstr``. On success ``Base58Status.Success`` will be returned
## and ``outlen`` will be set to number of characters stored inside of ## and ``outlen`` will be set to number of characters stored inside of
@ -112,7 +112,7 @@ proc encode*(btype: typedesc[Base58C], inbytes: openarray[byte],
result = Base58Status.Success result = Base58Status.Success
proc encode*(btype: typedesc[Base58C], proc encode*(btype: typedesc[Base58C],
inbytes: openarray[byte]): string {.inline.} = inbytes: openArray[byte]): string {.inline.} =
## Encode array of bytes ``inbytes`` using BASE58 encoding and return ## Encode array of bytes ``inbytes`` using BASE58 encoding and return
## encoded string. ## encoded string.
var size = (len(inbytes) * 138) div 100 + 1 var size = (len(inbytes) * 138) div 100 + 1
@ -123,8 +123,8 @@ proc encode*(btype: typedesc[Base58C],
else: else:
result = "" result = ""
proc decode*[T: byte|char](btype: typedesc[Base58C], instr: openarray[T], proc decode*[T: byte|char](btype: typedesc[Base58C], instr: openArray[T],
outbytes: var openarray[byte], outlen: var int): Base58Status = outbytes: var openArray[byte], outlen: var int): Base58Status =
## Decode BASE58 string and store array of bytes to ``outbytes``. On success ## Decode BASE58 string and store array of bytes to ``outbytes``. On success
## ``Base58Status.Success`` will be returned and ``outlen`` will be set ## ``Base58Status.Success`` will be returned and ``outlen`` will be set
## to number of bytes stored. ## to number of bytes stored.

View File

@ -68,8 +68,8 @@ proc decodedLength*(btype: typedesc[Base64Types],
elif (btype is Base64) or (btype is Base64Url): elif (btype is Base64) or (btype is Base64Url):
result = (length * 4 + 3 - 1) div 3 result = (length * 4 + 3 - 1) div 3
proc encode*(btype: typedesc[Base64Types], inbytes: openarray[byte], proc encode*(btype: typedesc[Base64Types], inbytes: openArray[byte],
outstr: var openarray[char], outlen: var int): Base64Status = outstr: var openArray[char], outlen: var int): Base64Status =
## Encode array of bytes ``inbytes`` using BASE64 encoding and store ## Encode array of bytes ``inbytes`` using BASE64 encoding and store
## result to ``outstr``. ## result to ``outstr``.
## ##
@ -127,7 +127,7 @@ proc encode*(btype: typedesc[Base64Types], inbytes: openarray[byte],
result = Base64Status.Success result = Base64Status.Success
proc encode*(btype: typedesc[Base64Types], proc encode*(btype: typedesc[Base64Types],
inbytes: openarray[byte]): string {.inline.} = inbytes: openArray[byte]): string {.inline.} =
## Encode array of bytes ``inbytes`` using BASE64 encoding and return ## Encode array of bytes ``inbytes`` using BASE64 encoding and return
## encoded string. ## encoded string.
var size = btype.encodedLength(len(inbytes)) var size = btype.encodedLength(len(inbytes))
@ -138,8 +138,8 @@ proc encode*(btype: typedesc[Base64Types],
else: else:
result = "" result = ""
proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openarray[T], proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openArray[T],
outbytes: var openarray[byte], outlen: var int): Base64Status = outbytes: var openArray[byte], outlen: var int): Base64Status =
## Decode BASE64 string and store array of bytes to ``outbytes``. On success ## Decode BASE64 string and store array of bytes to ``outbytes``. On success
## ``Base64Status.Success`` will be returned and ``outlen`` will be set ## ``Base64Status.Success`` will be returned and ``outlen`` will be set
## to number of bytes stored. ## to number of bytes stored.
@ -224,7 +224,7 @@ proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openarray[T],
result = Base64Status.Success result = Base64Status.Success
proc decode*[T: byte|char](btype: typedesc[Base64Types], proc decode*[T: byte|char](btype: typedesc[Base64Types],
instr: openarray[T]): seq[byte] = instr: openArray[T]): seq[byte] =
## Decode BASE64 string ``instr`` and return sequence of bytes as result. ## Decode BASE64 string ``instr`` and return sequence of bytes as result.
if len(instr) == 0: if len(instr) == 0:
result = newSeq[byte]() result = newSeq[byte]()

View File

@ -511,49 +511,49 @@ template byteIndex(pos: Natural): int =
template bitIndex(pos: Natural): int = template bitIndex(pos: Natural): int =
pos and 0b111 # same as pos mod 8 pos and 0b111 # same as pos mod 8
func getBit*(bytes: openarray[byte], pos: Natural): bool {.inline.} = func getBit*(bytes: openArray[byte], pos: Natural): bool {.inline.} =
getBit(bytes[byteIndex pos], bitIndex pos) getBit(bytes[byteIndex pos], bitIndex pos)
template getBitLE*(bytes: openarray[byte], pos: Natural): bool = template getBitLE*(bytes: openArray[byte], pos: Natural): bool =
getBit(bytes, pos) getBit(bytes, pos)
func getBitBE*(bytes: openarray[byte], pos: Natural): bool {.inline.} = func getBitBE*(bytes: openArray[byte], pos: Natural): bool {.inline.} =
getBitBE(bytes[byteIndex pos], bitIndex pos) getBitBE(bytes[byteIndex pos], bitIndex pos)
func changeBit*(bytes: var openarray[byte], pos: Natural, value: bool) {.inline.} = func changeBit*(bytes: var openArray[byte], pos: Natural, value: bool) {.inline.} =
changeBit(bytes[byteIndex pos], bitIndex pos, value) changeBit(bytes[byteIndex pos], bitIndex pos, value)
template changeBitLE*(bytes: var openarray[byte], pos: Natural, value: bool) = template changeBitLE*(bytes: var openArray[byte], pos: Natural, value: bool) =
changeBit(bytes, pos, value) changeBit(bytes, pos, value)
func changeBitBE*(bytes: var openarray[byte], pos: Natural, value: bool) {.inline.} = func changeBitBE*(bytes: var openArray[byte], pos: Natural, value: bool) {.inline.} =
changeBitBE(bytes[byteIndex pos], bitIndex pos, value) changeBitBE(bytes[byteIndex pos], bitIndex pos, value)
func setBit*(bytes: var openarray[byte], pos: Natural) {.inline.} = func setBit*(bytes: var openArray[byte], pos: Natural) {.inline.} =
setBit(bytes[byteIndex pos], bitIndex pos) setBit(bytes[byteIndex pos], bitIndex pos)
template setBitLE*(bytes: var openarray[byte], pos: Natural) = template setBitLE*(bytes: var openArray[byte], pos: Natural) =
setBit(bytes, pos) setBit(bytes, pos)
func setBitBE*(bytes: var openarray[byte], pos: Natural) {.inline.} = func setBitBE*(bytes: var openArray[byte], pos: Natural) {.inline.} =
setBitBE(bytes[byteIndex pos], bitIndex pos) setBitBE(bytes[byteIndex pos], bitIndex pos)
func clearBit*(bytes: var openarray[byte], pos: Natural) {.inline.} = func clearBit*(bytes: var openArray[byte], pos: Natural) {.inline.} =
clearBit(bytes[byteIndex pos], bitIndex pos) clearBit(bytes[byteIndex pos], bitIndex pos)
template clearBitLE*(bytes: var openarray[byte], pos: Natural) = template clearBitLE*(bytes: var openArray[byte], pos: Natural) =
clearBit(bytes, pos) clearBit(bytes, pos)
func clearBitBE*(bytes: var openarray[byte], pos: Natural) {.inline.} = func clearBitBE*(bytes: var openArray[byte], pos: Natural) {.inline.} =
clearBitBE(bytes[byteIndex pos], bitIndex pos) clearBitBE(bytes[byteIndex pos], bitIndex pos)
func toggleBit*(bytes: var openarray[byte], pos: Natural) {.inline.} = func toggleBit*(bytes: var openArray[byte], pos: Natural) {.inline.} =
toggleBit(bytes[byteIndex pos], bitIndex pos) toggleBit(bytes[byteIndex pos], bitIndex pos)
template toggleBitLE*(bytes: var openarray[byte], pos: Natural) = template toggleBitLE*(bytes: var openArray[byte], pos: Natural) =
toggleBit(bytes, pos) toggleBit(bytes, pos)
func toggleBitBE*(bytes: var openarray[byte], pos: Natural) {.inline.} = func toggleBitBE*(bytes: var openArray[byte], pos: Natural) {.inline.} =
toggleBitBE(bytes[byteIndex pos], bitIndex pos) toggleBitBE(bytes[byteIndex pos], bitIndex pos)
template setBit*(x: var BitIndexable, bit: Natural, val: bool) {.deprecated: "changeBit".} = template setBit*(x: var BitIndexable, bit: Natural, val: bool) {.deprecated: "changeBit".} =
@ -577,28 +577,28 @@ template lowerBitLE*(x: var BitIndexable, bit: Natural) {.deprecated: "clearBitL
template lowerBitBE*(x: var BitIndexable, bit: Natural) {.deprecated: "clearBitBE".} = template lowerBitBE*(x: var BitIndexable, bit: Natural) {.deprecated: "clearBitBE".} =
clearBitBE(x, bit) clearBitBE(x, bit)
template setBit*(bytes: var openarray[byte], pos: Natural, val: bool) {.deprecated: "changeBit".} = template setBit*(bytes: var openArray[byte], pos: Natural, val: bool) {.deprecated: "changeBit".} =
changeBit(bytes, pos, val) changeBit(bytes, pos, val)
template setBitLE*(bytes: var openarray[byte], pos: Natural, val: bool) {.deprecated: "changeBitLE".} = template setBitLE*(bytes: var openArray[byte], pos: Natural, val: bool) {.deprecated: "changeBitLE".} =
changeBitLE(bytes, pos, val) changeBitLE(bytes, pos, val)
template setBitBE*(bytes: var openarray[byte], pos: Natural, val: bool) {.deprecated: "changeBitBE".} = template setBitBE*(bytes: var openArray[byte], pos: Natural, val: bool) {.deprecated: "changeBitBE".} =
changeBitBE(bytes, pos, val) changeBitBE(bytes, pos, val)
template raiseBit*(bytes: var openarray[byte], pos: Natural) {.deprecated: "setBit".} = template raiseBit*(bytes: var openArray[byte], pos: Natural) {.deprecated: "setBit".} =
setBit(bytes, pos) setBit(bytes, pos)
template raiseBitLE*(bytes: var openarray[byte], pos: Natural) {.deprecated: "setBitLE".} = template raiseBitLE*(bytes: var openArray[byte], pos: Natural) {.deprecated: "setBitLE".} =
setBitLE(bytes, pos) setBitLE(bytes, pos)
template raiseBitBE*(bytes: var openarray[byte], pos: Natural) {.deprecated: "setBitBE".} = template raiseBitBE*(bytes: var openArray[byte], pos: Natural) {.deprecated: "setBitBE".} =
setBitBE(bytes, pos) setBitBE(bytes, pos)
template lowerBit*(bytes: var openarray[byte], pos: Natural) {.deprecated: "clearBit".} = template lowerBit*(bytes: var openArray[byte], pos: Natural) {.deprecated: "clearBit".} =
clearBit(bytes, pos) clearBit(bytes, pos)
template lowerBitLE*(bytes: var openarray[byte], pos: Natural) {.deprecated: "clearBitLE".} = template lowerBitLE*(bytes: var openArray[byte], pos: Natural) {.deprecated: "clearBitLE".} =
clearBitLE(bytes, pos) clearBitLE(bytes, pos)
template lowerBitBE*(bytes: var openarray[byte], pos: Natural) {.deprecated: "clearBitBE".} = template lowerBitBE*(bytes: var openArray[byte], pos: Natural) {.deprecated: "clearBitBE".} =
clearBitBE(bytes, pos) clearBitBE(bytes, pos)
func getBitsBE*(data: openarray[byte], slice: HSlice, T: type[SomeUnsignedInt]): T = func getBitsBE*(data: openArray[byte], slice: HSlice, T: type[SomeUnsignedInt]): T =
## Treats `data` as an unsigned big endian integer and returns a slice of bits ## Treats `data` as an unsigned big endian integer and returns a slice of bits
## extracted from it, assuming 0 to be the possition of the most significant bit. ## extracted from it, assuming 0 to be the possition of the most significant bit.
let totalBits = data.len * 8 let totalBits = data.len * 8
@ -638,6 +638,6 @@ func getBitsBE*(data: openarray[byte], slice: HSlice, T: type[SomeUnsignedInt]):
else: else:
(firstLimb shl firstLimbUnusedBits) shr (resultBits - sliceLen) (firstLimb shl firstLimbUnusedBits) shr (resultBits - sliceLen)
template getBitsBE*(data: openarray[byte], slice: HSlice): BiggestUInt = template getBitsBE*(data: openArray[byte], slice: HSlice): BiggestUInt =
getBitsBE(data, slice, BiggestUInt) getBitsBE(data, slice, BiggestUInt)

View File

@ -29,7 +29,7 @@ type
BitArray*[bits: static int] = object BitArray*[bits: static int] = object
bytes*: array[(bits + 7) div 8, byte] bytes*: array[(bits + 7) div 8, byte]
func bitsLen*(bytes: openarray[byte]): int = func bitsLen*(bytes: openArray[byte]): int =
let let
bytesCount = bytes.len bytesCount = bytes.len
lastByte = bytes[bytesCount - 1] lastByte = bytes[bytesCount - 1]
@ -58,14 +58,14 @@ func add*(s: var BitSeq, value: bool) =
s.Bytes[lastBytePos].changeBit 7, value s.Bytes[lastBytePos].changeBit 7, value
s.Bytes.add byte(1) s.Bytes.add byte(1)
func loadLEBytes(WordType: type, bytes: openarray[byte]): WordType = func loadLEBytes(WordType: type, bytes: openArray[byte]): WordType =
# TODO: this is a temporary proc until the endians API is improved # TODO: this is a temporary proc until the endians API is improved
var shift = 0 var shift = 0
for b in bytes: for b in bytes:
result = result or (WordType(b) shl shift) result = result or (WordType(b) shl shift)
shift += 8 shift += 8
func storeLEBytes(value: SomeUnsignedInt, dst: var openarray[byte]) = func storeLEBytes(value: SomeUnsignedInt, dst: var openArray[byte]) =
when system.cpuEndian == bigEndian: when system.cpuEndian == bigEndian:
var shift = 0 var shift = 0
for i in 0 ..< dst.len: for i in 0 ..< dst.len:
@ -212,8 +212,8 @@ template clearBit*(a: var BitArray, pos: Natural) =
# TODO: Submit this to the standard library as `cmp` # TODO: Submit this to the standard library as `cmp`
# At the moment, it doesn't work quite well because Nim selects # At the moment, it doesn't work quite well because Nim selects
# the generic cmp[T] from the system module instead of choosing # the generic cmp[T] from the system module instead of choosing
# the openarray overload # the openArray overload
func compareArrays[T](a, b: openarray[T]): int = func compareArrays[T](a, b: openArray[T]): int =
result = cmp(a.len, b.len) result = cmp(a.len, b.len)
if result != 0: return if result != 0: return

View File

@ -59,17 +59,17 @@ func hexToByteArray*(hexStr: string, output: var openArray[byte])
hexToByteArray(hexStr, output, 0, output.high) hexToByteArray(hexStr, output, 0, output.high)
func hexToByteArray*[N: static[int]](hexStr: string): array[N, byte] func hexToByteArray*[N: static[int]](hexStr: string): array[N, byte]
{.raises: [ValueError, Defect], noInit, inline.}= {.raises: [ValueError, Defect], noinit, inline.}=
## Read an hex string and store it in a byte array. No "endianness" reordering is done. ## Read an hex string and store it in a byte array. No "endianness" reordering is done.
hexToByteArray(hexStr, result) hexToByteArray(hexStr, result)
func hexToByteArray*(hexStr: string, N: static int): array[N, byte] func hexToByteArray*(hexStr: string, N: static int): array[N, byte]
{.raises: [ValueError, Defect], noInit, inline.}= {.raises: [ValueError, Defect], noinit, inline.}=
## Read an hex string and store it in a byte array. No "endianness" reordering is done. ## Read an hex string and store it in a byte array. No "endianness" reordering is done.
hexToByteArray(hexStr, result) hexToByteArray(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, Defect], noInit, inline.}= {.raises: [ValueError, Defect], noinit, inline.}=
## Read an hex string and store it in a byte array. No "endianness" reordering is done. ## Read an hex string and store it in a byte array. No "endianness" reordering is done.
hexToByteArray(hexStr, result) hexToByteArray(hexStr, result)
@ -115,7 +115,7 @@ func hexToSeqByte*(hexStr: string): seq[byte]
for i in 0 ..< N: for i in 0 ..< N:
result[i] = hexStr[2*i + skip].readHexChar shl 4 or hexStr[2*i + 1 + skip].readHexChar result[i] = hexStr[2*i + skip].readHexChar shl 4 or hexStr[2*i + 1 + skip].readHexChar
func toHexAux(ba: openarray[byte]): string = func toHexAux(ba: openArray[byte]): string =
## Convert a byte-array to its hex representation ## Convert a byte-array to its hex representation
## Output is in lowercase ## Output is in lowercase
## No "endianness" reordering is done. ## No "endianness" reordering is done.
@ -127,7 +127,7 @@ func toHexAux(ba: openarray[byte]): string =
result[2*i] = hexChars[int ba[i] shr 4 and 0xF] result[2*i] = hexChars[int ba[i] shr 4 and 0xF]
result[2*i+1] = hexChars[int ba[i] and 0xF] result[2*i+1] = hexChars[int ba[i] and 0xF]
func toHex*(ba: openarray[byte]): string {.inline.} = func toHex*(ba: openArray[byte]): string {.inline.} =
## Convert a byte-array to its hex representation ## Convert a byte-array to its hex representation
## Output is in lowercase ## Output is in lowercase
## No "endianness" reordering is done. ## No "endianness" reordering is done.

View File

@ -1,5 +1,5 @@
when not compiles(writeFile("filename", @[byte(1)])): when not compiles(writeFile("filename", @[byte(1)])):
proc writeFile*(filename: string, content: openarray[byte]) = proc writeFile*(filename: string, content: openArray[byte]) =
## Opens a file named `filename` for writing. Then writes the ## Opens a file named `filename` for writing. Then writes the
## `content` completely to the file and closes the file afterwards. ## `content` completely to the file and closes the file afterwards.
## Raises an IO exception in case of an error. ## Raises an IO exception in case of an error.

View File

@ -20,7 +20,7 @@ template init*(lvalue: var auto, a1, a2, a3: auto) =
when not declared(default): when not declared(default):
proc default*(T: type): T = discard proc default*(T: type): T = discard
proc toArray*[T](N: static int, data: openarray[T]): array[N, T] = proc toArray*[T](N: static int, data: openArray[T]): array[N, T] =
doAssert data.len == N doAssert data.len == N
copyMem(addr result[0], unsafeAddr data[0], N) copyMem(addr result[0], unsafeAddr data[0], N)

View File

@ -20,15 +20,15 @@ proc toMemRange*(x: string): MemRange =
result.start = x.cstring.pointer result.start = x.cstring.pointer
result.size = x.len result.size = x.len
proc toMemRange*[T](x: openarray[T], fromIdx, toIdx: int): MemRange = proc toMemRange*[T](x: openArray[T], fromIdx, toIdx: int): MemRange =
doAssert(fromIdx >= 0 and toIdx >= fromIdx and fromIdx < x.len and toIdx < x.len) doAssert(fromIdx >= 0 and toIdx >= fromIdx and fromIdx < x.len and toIdx < x.len)
result.start = unsafeAddr x[fromIdx] result.start = unsafeAddr x[fromIdx]
result.size = (toIdx - fromIdx + 1) * T.sizeof result.size = (toIdx - fromIdx + 1) * T.sizeof
proc toMemRange*[T](x: openarray[T], fromIdx: int): MemRange {.inline.} = proc toMemRange*[T](x: openArray[T], fromIdx: int): MemRange {.inline.} =
toMemRange(x, fromIdx, x.high) toMemRange(x, fromIdx, x.high)
proc toMemRange*[T](x: openarray[T]): MemRange {.inline.} = proc toMemRange*[T](x: openArray[T]): MemRange {.inline.} =
toMemRange(x, 0, x.high) toMemRange(x, 0, x.high)
template toMemRange*(mr: MemRange): MemRange = mr template toMemRange*(mr: MemRange): MemRange = mr

View File

@ -1,7 +1,7 @@
import ../ptrops import ../ptrops
export ptrops export ptrops
proc baseAddr*[T](x: openarray[T]): pointer = cast[pointer](x) proc baseAddr*[T](x: openArray[T]): pointer = cast[pointer](x)
# Please note that we use templates here on purpose. # Please note that we use templates here on purpose.
# As much as I believe in the power of optimizing compilers, it turned # As much as I believe in the power of optimizing compilers, it turned

View File

@ -36,31 +36,31 @@ proc toImmutableRange[T](a: seq[T]): Range[T] =
result.mLen = a.len result.mLen = a.len
when unsafeAPIEnabled: when unsafeAPIEnabled:
proc toImmutableRangeNoGCHold[T](a: openarray[T]): Range[T] = proc toImmutableRangeNoGCHold[T](a: openArray[T]): Range[T] =
if a.len != 0: if a.len != 0:
result.start = unsafeAddr a[0] result.start = unsafeAddr a[0]
result.mLen = a.len result.mLen = a.len
proc toImmutableRange[T](a: openarray[T]): Range[T] {.inline.} = proc toImmutableRange[T](a: openArray[T]): Range[T] {.inline.} =
toImmutableRangeNoGCHold(a) toImmutableRangeNoGCHold(a)
proc toRange*[T](a: var seq[T]): MutRange[T] {.inline.} = proc toRange*[T](a: var seq[T]): MutRange[T] {.inline.} =
MutRange[T](toImmutableRange(a)) MutRange[T](toImmutableRange(a))
when unsafeAPIEnabled: when unsafeAPIEnabled:
proc toRange*[T](a: var openarray[T]): MutRange[T] {.inline.} = proc toRange*[T](a: var openArray[T]): MutRange[T] {.inline.} =
MutRange[T](toImmutableRange(a)) MutRange[T](toImmutableRange(a))
template initStackRange*[T](sz: static[int]): MutRange[T] = template initStackRange*[T](sz: static[int]): MutRange[T] =
var data: array[sz, T] var data: array[sz, T]
data.toRange() data.toRange()
proc toRange*[T](a: openarray[T]): Range[T] {.inline.} = toImmutableRange(a) proc toRange*[T](a: openArray[T]): Range[T] {.inline.} = toImmutableRange(a)
proc unsafeRangeConstruction*[T](a: var openarray[T]): MutRange[T] {.inline.} = proc unsafeRangeConstruction*[T](a: var openArray[T]): MutRange[T] {.inline.} =
MutRange[T](toImmutableRange(a)) MutRange[T](toImmutableRange(a))
proc unsafeRangeConstruction*[T](a: openarray[T]): Range[T] {.inline.} = proc unsafeRangeConstruction*[T](a: openArray[T]): Range[T] {.inline.} =
toImmutableRange(a) toImmutableRange(a)
proc newRange*[T](sz: int): MutRange[T] {.inline.} = proc newRange*[T](sz: int): MutRange[T] {.inline.} =
@ -152,7 +152,7 @@ proc `[]`*[T, U, V](r: Range[T], s: HSlice[U, V]): Range[T] {.inline.} =
proc `[]`*[T, U, V](r: MutRange[T], s: HSlice[U, V]): MutRange[T] {.inline.} = proc `[]`*[T, U, V](r: MutRange[T], s: HSlice[U, V]): MutRange[T] {.inline.} =
MutRange[T](sliceNormalized(r, r ^^ s.a, r ^^ s.b)) MutRange[T](sliceNormalized(r, r ^^ s.a, r ^^ s.b))
proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: openarray[T]) = proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: openArray[T]) =
let a = r ^^ s.a let a = r ^^ s.a
let b = r ^^ s.b let b = r ^^ s.b
let L = b - a + 1 let L = b - a + 1

View File

@ -36,7 +36,7 @@ proc writeMacroResultsNow* {.compileTime.} =
file.add newCommentStmtNode("Generated at line " & $location.line) file.add newCommentStmtNode("Generated at line " & $location.line)
file.add macroOutput file.add macroOutput
for i in 0..< macroLocations.len: for i in 0 ..< macroLocations.len:
addToFile files.mgetOrPut(macroLocations[i].filename, nil), addToFile files.mgetOrPut(macroLocations[i].filename, nil),
macroLocations[i], macroOutputs[i] macroLocations[i], macroOutputs[i]
@ -408,7 +408,7 @@ macro unpackArgs*(callee: untyped, args: untyped): untyped =
for arg in args: for arg in args:
let arg = if arg.kind == nnkHiddenStdConv: arg[1] let arg = if arg.kind == nnkHiddenStdConv: arg[1]
else: arg else: arg
if arg.kind == nnkArgList: if arg.kind == nnkArglist:
for subarg in arg: for subarg in arg:
result.add subarg result.add subarg
else: else:

View File

@ -1,4 +1,4 @@
proc add*(str: var string, chars: openarray[char]) = proc add*(str: var string, chars: openArray[char]) =
for c in chars: for c in chars:
str.add c str.add c

View File

@ -310,7 +310,7 @@ proc verify*[K,V](sl: var SortedSet[K,V]):
## proc `$`*[V](data: V): string {.gcsafe,raises:[Defect,CatchableError].} ## proc `$`*[V](data: V): string {.gcsafe,raises:[Defect,CatchableError].}
## ##
sl.tree.rbTreeVerify( sl.tree.rbTreeVerify(
lt = proc(a, b: SortedSetItemRef[K,V]): bool = a.sLstLt(b), lt = proc(a, b: SortedSetItemRef[K,V]): bool = a.slstLt(b),
pr = proc(c: RbInfo; s: string) = c.slstPr(s)) pr = proc(c: RbInfo; s: string) = c.slstPr(s))
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -1,4 +1,4 @@
proc add*(s: var string, data: openarray[char]) = proc add*(s: var string, data: openArray[char]) =
if data.len > 0: if data.len > 0:
let prevEnd = s.len let prevEnd = s.len
s.setLen(prevEnd + data.len) s.setLen(prevEnd + data.len)

View File

@ -144,7 +144,7 @@ func getResult*[IntType, F](p: VarintParser[IntType, F]): IntType {.inline.} =
else: else:
p.res p.res
func readVarint*(input: openarray[byte], func readVarint*(input: openArray[byte],
outVal: var SomeInteger, outVal: var SomeInteger,
flavour: static VarintFlavour = ProtoBuf): int = flavour: static VarintFlavour = ProtoBuf): int =
## Reads a varint from a buffer and stores it in `outVal`. ## Reads a varint from a buffer and stores it in `outVal`.

View File

@ -3,7 +3,7 @@ import ../stew/base58
when defined(nimHasUsed): {.used.} when defined(nimHasUsed): {.used.}
proc hexToBytes*(a: string, result: var openarray[byte]) = proc 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

View File

@ -50,13 +50,13 @@ proc `$`(rc: KeyedQueuePair[uint,uint]): string =
proc `$`(rc: Result[KeyedQueuePair[uint,uint],void]): string = proc `$`(rc: Result[KeyedQueuePair[uint,uint],void]): string =
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 = proc `$`(rc: Result[uint,void]): string =
result = "<" result = "<"
if rc.isOK: if rc.isOk:
result &= $rc.value result &= $rc.value
result &= ">" result &= ">"
@ -93,7 +93,7 @@ proc 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)
if rc.isOK: if rc.isOk:
return rc.value return rc.value
lru.q.lruAppend(key, key.fromKey.toValue, lru.size) lru.q.lruAppend(key, key.fromKey.toValue, lru.size)
@ -223,7 +223,7 @@ suite "KeyedQueue: Data queue with keyed random access":
test "Delete corresponding entries by keyed access from previous queues": test "Delete corresponding entries by keyed access from previous queues":
var seen: seq[int] var seen: seq[int]
let sub7 = keyList.len div 7 let sub7 = keyList.len div 7
for n in toSeq(countUp(0,sub7)).concat(toSeq(countUp(3*sub7,4*sub7))): for n in toSeq(countup(0,sub7)).concat(toSeq(countup(3*sub7,4*sub7))):
let let
key = keyList[n].toKey key = keyList[n].toKey
canDeleteOk = (key.fromKey notin seen) canDeleteOk = (key.fromKey notin seen)
@ -285,12 +285,12 @@ suite "KeyedQueue: Data queue with keyed random access":
seen: seq[int] seen: seq[int]
all: seq[uint] all: seq[uint]
rc = rq.first rc = rq.first
while rc.isOK: while rc.isOk:
let key = rc.value.key let key = rc.value.key
all.add key all.add key
rc = rq.next(key) rc = rq.next(key)
rq.addOrFlushGroupwise(groupLen, seen, key.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, key.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all check uniqueKeys == all
@ -303,7 +303,7 @@ suite "KeyedQueue: Data queue with keyed random access":
for w in rq.nextKeys: for w in rq.nextKeys:
all.add w all.add w
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all check uniqueKeys == all
@ -315,7 +315,7 @@ suite "KeyedQueue: Data queue with keyed random access":
for w in rq.nextPairs: for w in rq.nextPairs:
all.add w.key all.add w.key
rq.addOrFlushGroupwise(groupLen, seen, w.key.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, w.key.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all check uniqueKeys == all
@ -328,7 +328,7 @@ suite "KeyedQueue: Data queue with keyed random access":
let w = v.fromValue.toKey let w = v.fromValue.toKey
all.add w all.add w
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all check uniqueKeys == all
@ -340,12 +340,12 @@ suite "KeyedQueue: Data queue with keyed random access":
seen: seq[int] seen: seq[int]
all: seq[uint] all: seq[uint]
rc = rq.last rc = rq.last
while rc.isOK: while rc.isOk:
let key = rc.value.key let key = rc.value.key
all.add key all.add key
rc = rq.prev(key) rc = rq.prev(key)
rq.addOrFlushGroupwise(groupLen, seen, key.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, key.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all.reversed check uniqueKeys == all.reversed
@ -358,7 +358,7 @@ suite "KeyedQueue: Data queue with keyed random access":
for w in rq.prevKeys: for w in rq.prevKeys:
all.add w all.add w
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all.reversed check uniqueKeys == all.reversed
@ -370,7 +370,7 @@ suite "KeyedQueue: Data queue with keyed random access":
for w in rq.prevPairs: for w in rq.prevPairs:
all.add w.key all.add w.key
rq.addOrFlushGroupwise(groupLen, seen, w.key.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, w.key.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all.reversed check uniqueKeys == all.reversed
@ -383,7 +383,7 @@ suite "KeyedQueue: Data queue with keyed random access":
let w = v.fromValue.toKey let w = v.fromValue.toKey
all.add w all.add w
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy) rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
check rq.verify.isOK check rq.verify.isOk
check seen.len == rq.len check seen.len == rq.len
check seen.len < groupLen check seen.len < groupLen
check uniqueKeys == all.reversed check uniqueKeys == all.reversed
@ -400,7 +400,7 @@ suite "KeyedQueue: Data queue with keyed random access":
check uniqueKeys[count] == rc.value check uniqueKeys[count] == rc.value
rc = rq.nextKey(rc.value) rc = rq.nextKey(rc.value)
count.inc count.inc
check rq.verify.isOK check rq.verify.isOk
check count == uniqueKeys.len check count == uniqueKeys.len
block: block:
var var
@ -411,7 +411,7 @@ suite "KeyedQueue: Data queue with keyed random access":
check uniqueKeys[count] == rc.value.data.fromValue.toKey check uniqueKeys[count] == rc.value.data.fromValue.toKey
rc = rq.next(rc.value.key) rc = rq.next(rc.value.key)
count.inc count.inc
check rq.verify.isOK check rq.verify.isOk
check count == uniqueKeys.len check count == uniqueKeys.len
# reverse ... # reverse ...
@ -424,7 +424,7 @@ suite "KeyedQueue: Data queue with keyed random access":
count.dec count.dec
check uniqueKeys[count] == rc.value check uniqueKeys[count] == rc.value
rc = rq.prevKey(rc.value) rc = rq.prevKey(rc.value)
check rq.verify.isOK check rq.verify.isOk
check count == 0 check count == 0
block: block:
var var
@ -435,7 +435,7 @@ suite "KeyedQueue: Data queue with keyed random access":
count.dec count.dec
check uniqueKeys[count] == rc.value.data.fromValue.toKey check uniqueKeys[count] == rc.value.data.fromValue.toKey
rc = rq.prev(rc.value.key) rc = rq.prev(rc.value.key)
check rq.verify.isOK check rq.verify.isOk
check count == 0 check count == 0
# -------------------------------------- # --------------------------------------
@ -450,8 +450,8 @@ suite "KeyedQueue: Data queue with keyed random access":
break break
let key = rc.value let key = rc.value
check rq.second.value.data == rq[key] check rq.second.value.data == rq[key]
check rq.delete(key).isOK check rq.delete(key).isOk
check rq.verify.isOK check rq.verify.isOk
check rq.len == 1 check rq.len == 1
block: block:
@ -463,8 +463,8 @@ suite "KeyedQueue: Data queue with keyed random access":
break break
let key = rc.value let key = rc.value
check rq.beforeLast.value.data == rq[key] check rq.beforeLast.value.data == rq[key]
check rq.delete(key).isOK check rq.delete(key).isOk
check rq.verify.isOK check rq.verify.isOk
check rq.len == 1 check rq.len == 1
# -------------------------------------- # --------------------------------------
@ -499,7 +499,7 @@ suite "KeyedQueue: Data queue as LRU cache":
values = toSeq(cache.q.nextPairs).mapIt(it.data) values = toSeq(cache.q.nextPairs).mapIt(it.data)
infoPfx = if reSched: ">>> rotate" else: "+++ append" infoPfx = if reSched: ">>> rotate" else: "+++ append"
noisy.say infoPfx, &"{value} => {queue}" noisy.say infoPfx, &"{value} => {queue}"
check cache.q.verify.isOK check cache.q.verify.isOk
check queue.mapIt($it) == values.mapIt($it.fromValue) check queue.mapIt($it) == values.mapIt($it.fromValue)
check item.toKey == cache.q.lastKey.value check item.toKey == cache.q.lastKey.value
check toSeq(cache.q.nextPairs) == toSeq(cExpected.q.nextPairs) check toSeq(cache.q.nextPairs) == toSeq(cExpected.q.nextPairs)
@ -512,8 +512,8 @@ suite "KeyedQueue: Data queue as LRU cache":
check c1 == c2 check c1 == c2
check c1.lruValue(77) == 77.toValue check c1.lruValue(77) == 77.toValue
check c1.q.verify.isOK check c1.q.verify.isOk
check c2.q.verify.isOK check c2.q.verify.isOk
noisy.say &"c1Specs: {c1.size} {c1.q.firstKey} {c1.q.lastKey} ..." noisy.say &"c1Specs: {c1.size} {c1.q.firstKey} {c1.q.lastKey} ..."
noisy.say &"c2Specs: {c2.size} {c2.q.firstKey} {c2.q.lastKey} ..." noisy.say &"c2Specs: {c2.size} {c2.q.firstKey} {c2.q.lastKey} ..."

View File

@ -115,7 +115,7 @@ suite "SortedSet: Sorted list based on red-black tree":
test "Delete items": test "Delete items":
var seen: seq[int] var seen: seq[int]
let sub7 = keyList.len div 7 let sub7 = keyList.len div 7
for n in toSeq(countUp(0,sub7)).concat(toSeq(countUp(3*sub7,4*sub7))): for n in toSeq(countup(0,sub7)).concat(toSeq(countup(3*sub7,4*sub7))):
let let
key = keyList[n] key = keyList[n]
canDeleteOk = (key notin seen) canDeleteOk = (key notin seen)