enable styleCheck:usages (#93)
This commit is contained in:
parent
3b9e906a18
commit
8a405309c6
|
@ -17,7 +17,7 @@ proc test(env, path: string) =
|
|||
lang = getEnv"TEST_LANG"
|
||||
|
||||
exec "nim " & lang & " " & env &
|
||||
" -r --hints:off --skipParentCfg " & path
|
||||
" -r --hints:off --skipParentCfg --styleCheck:usages --styleCheck:error " & path
|
||||
|
||||
task test, "Run all tests":
|
||||
test "--threads:off", "tests/all_tests"
|
||||
|
|
|
@ -54,13 +54,13 @@ func copyFrom*[T](
|
|||
assign(v.toOpenArray(0, elems - 1), src.toOpenArray(0, elems - 1))
|
||||
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)
|
||||
|
||||
func `&`*[N1, N2: static[int], T](
|
||||
a: array[N1, T],
|
||||
b: array[N2, T]
|
||||
): array[N1 + N2, T] {.inline, noInit.}=
|
||||
): array[N1 + N2, T] {.inline, noinit.}=
|
||||
## Array concatenation
|
||||
assign(result.toOpenArray(0, N1 - 1), a)
|
||||
assign(result.toOpenArray(N1, result.high), b)
|
||||
|
|
|
@ -40,7 +40,7 @@ type
|
|||
len*: int8 # >= 1 when holding valid unsigned integer
|
||||
|
||||
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.
|
||||
const
|
||||
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)
|
||||
|
||||
proc encode[A: byte|char](B: typedesc[Base10], value: SomeUnsignedInt,
|
||||
output: var openarray[A],
|
||||
output: var openArray[A],
|
||||
length: int8): Result[int8, cstring] =
|
||||
const Digits = cstring(
|
||||
"0001020304050607080910111213141516171819" &
|
||||
|
@ -175,7 +175,7 @@ proc encode[A: byte|char](B: typedesc[Base10], value: SomeUnsignedInt,
|
|||
ok(length)
|
||||
|
||||
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.
|
||||
B.encode(value, output, B.encodedLength(value))
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ proc decodedLength*(btype: typedesc[Base32Types], length: int): int =
|
|||
let reminder = length mod 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.} =
|
||||
if length >= 1:
|
||||
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)
|
||||
result = 8
|
||||
|
||||
proc convert8to5(inbytes: openarray[byte], outbytes: var openarray[byte],
|
||||
proc convert8to5(inbytes: openArray[byte], outbytes: var openArray[byte],
|
||||
length: int): int {.inline.} =
|
||||
if length >= 2:
|
||||
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)
|
||||
result = 5
|
||||
|
||||
proc encode*(btype: typedesc[Base32Types], inbytes: openarray[byte],
|
||||
outstr: var openarray[char], outlen: var int): Base32Status =
|
||||
proc encode*(btype: typedesc[Base32Types], inbytes: openArray[byte],
|
||||
outstr: var openArray[char], outlen: var int): Base32Status =
|
||||
## Encode array of bytes ``inbytes`` using BASE32 encoding and store
|
||||
## result to ``outstr``. On success ``Base32Status.Success`` will be returned
|
||||
## 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
|
||||
|
||||
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
|
||||
## encoded string.
|
||||
if len(inbytes) == 0:
|
||||
|
@ -197,8 +197,8 @@ proc encode*(btype: typedesc[Base32Types],
|
|||
else:
|
||||
result = ""
|
||||
|
||||
proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openarray[T],
|
||||
outbytes: var openarray[byte], outlen: var int): Base32Status =
|
||||
proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openArray[T],
|
||||
outbytes: var openArray[byte], outlen: var int): Base32Status =
|
||||
## Decode BASE32 string and store array of bytes to ``outbytes``. On success
|
||||
## ``Base32Status.Success`` will be returned and ``outlen`` will be set
|
||||
## to number of bytes stored.
|
||||
|
@ -271,7 +271,7 @@ proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openarray[T],
|
|||
result = Base32Status.Success
|
||||
|
||||
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.
|
||||
if len(instr) == 0:
|
||||
result = newSeq[byte]()
|
||||
|
|
|
@ -24,7 +24,7 @@ type
|
|||
## Type to use Bitcoin alphabet
|
||||
FLCBase58* = object
|
||||
## Type to use Flickr alphabet
|
||||
Base58* = BtcBase58
|
||||
Base58* = BTCBase58
|
||||
## By default we are using Bitcoin alphabet
|
||||
Base58C* = BTCBase58 | FLCBase58
|
||||
## Supported types
|
||||
|
@ -57,8 +57,8 @@ proc decodedLength*(btype: typedesc[Base58C], length: int): int =
|
|||
## ``length``.
|
||||
result = length + 4
|
||||
|
||||
proc encode*(btype: typedesc[Base58C], inbytes: openarray[byte],
|
||||
outstr: var openarray[char], outlen: var int): Base58Status =
|
||||
proc encode*(btype: typedesc[Base58C], inbytes: openArray[byte],
|
||||
outstr: var openArray[char], outlen: var int): Base58Status =
|
||||
## Encode array of bytes ``inbytes`` using BASE58 encoding and store
|
||||
## result to ``outstr``. On success ``Base58Status.Success`` will be returned
|
||||
## 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
|
||||
|
||||
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
|
||||
## encoded string.
|
||||
var size = (len(inbytes) * 138) div 100 + 1
|
||||
|
@ -123,8 +123,8 @@ proc encode*(btype: typedesc[Base58C],
|
|||
else:
|
||||
result = ""
|
||||
|
||||
proc decode*[T: byte|char](btype: typedesc[Base58C], instr: openarray[T],
|
||||
outbytes: var openarray[byte], outlen: var int): Base58Status =
|
||||
proc decode*[T: byte|char](btype: typedesc[Base58C], instr: openArray[T],
|
||||
outbytes: var openArray[byte], outlen: var int): Base58Status =
|
||||
## Decode BASE58 string and store array of bytes to ``outbytes``. On success
|
||||
## ``Base58Status.Success`` will be returned and ``outlen`` will be set
|
||||
## to number of bytes stored.
|
||||
|
|
|
@ -68,8 +68,8 @@ proc decodedLength*(btype: typedesc[Base64Types],
|
|||
elif (btype is Base64) or (btype is Base64Url):
|
||||
result = (length * 4 + 3 - 1) div 3
|
||||
|
||||
proc encode*(btype: typedesc[Base64Types], inbytes: openarray[byte],
|
||||
outstr: var openarray[char], outlen: var int): Base64Status =
|
||||
proc encode*(btype: typedesc[Base64Types], inbytes: openArray[byte],
|
||||
outstr: var openArray[char], outlen: var int): Base64Status =
|
||||
## Encode array of bytes ``inbytes`` using BASE64 encoding and store
|
||||
## result to ``outstr``.
|
||||
##
|
||||
|
@ -127,7 +127,7 @@ proc encode*(btype: typedesc[Base64Types], inbytes: openarray[byte],
|
|||
result = Base64Status.Success
|
||||
|
||||
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
|
||||
## encoded string.
|
||||
var size = btype.encodedLength(len(inbytes))
|
||||
|
@ -138,8 +138,8 @@ proc encode*(btype: typedesc[Base64Types],
|
|||
else:
|
||||
result = ""
|
||||
|
||||
proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openarray[T],
|
||||
outbytes: var openarray[byte], outlen: var int): Base64Status =
|
||||
proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openArray[T],
|
||||
outbytes: var openArray[byte], outlen: var int): Base64Status =
|
||||
## Decode BASE64 string and store array of bytes to ``outbytes``. On success
|
||||
## ``Base64Status.Success`` will be returned and ``outlen`` will be set
|
||||
## to number of bytes stored.
|
||||
|
@ -224,7 +224,7 @@ proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openarray[T],
|
|||
result = Base64Status.Success
|
||||
|
||||
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.
|
||||
if len(instr) == 0:
|
||||
result = newSeq[byte]()
|
||||
|
|
|
@ -511,49 +511,49 @@ template byteIndex(pos: Natural): int =
|
|||
template bitIndex(pos: Natural): int =
|
||||
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)
|
||||
|
||||
template getBitLE*(bytes: openarray[byte], pos: Natural): bool =
|
||||
template getBitLE*(bytes: openArray[byte], pos: Natural): bool =
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
template changeBitLE*(bytes: var openarray[byte], pos: Natural, value: bool) =
|
||||
template changeBitLE*(bytes: var openArray[byte], pos: Natural, value: bool) =
|
||||
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)
|
||||
|
||||
func setBit*(bytes: var openarray[byte], pos: Natural) {.inline.} =
|
||||
func setBit*(bytes: var openArray[byte], pos: Natural) {.inline.} =
|
||||
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)
|
||||
|
||||
func setBitBE*(bytes: var openarray[byte], pos: Natural) {.inline.} =
|
||||
func setBitBE*(bytes: var openArray[byte], pos: Natural) {.inline.} =
|
||||
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)
|
||||
|
||||
template clearBitLE*(bytes: var openarray[byte], pos: Natural) =
|
||||
template clearBitLE*(bytes: var openArray[byte], pos: Natural) =
|
||||
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)
|
||||
|
||||
func toggleBit*(bytes: var openarray[byte], pos: Natural) {.inline.} =
|
||||
func toggleBit*(bytes: var openArray[byte], pos: Natural) {.inline.} =
|
||||
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)
|
||||
|
||||
func toggleBitBE*(bytes: var openarray[byte], pos: Natural) {.inline.} =
|
||||
func toggleBitBE*(bytes: var openArray[byte], pos: Natural) {.inline.} =
|
||||
toggleBitBE(bytes[byteIndex pos], bitIndex pos)
|
||||
|
||||
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".} =
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
|
||||
template raiseBit*(bytes: var openarray[byte], pos: Natural) {.deprecated: "setBit".} =
|
||||
template raiseBit*(bytes: var openArray[byte], pos: Natural) {.deprecated: "setBit".} =
|
||||
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)
|
||||
template raiseBitBE*(bytes: var openarray[byte], pos: Natural) {.deprecated: "setBitBE".} =
|
||||
template raiseBitBE*(bytes: var openArray[byte], pos: Natural) {.deprecated: "setBitBE".} =
|
||||
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)
|
||||
template lowerBitLE*(bytes: var openarray[byte], pos: Natural) {.deprecated: "clearBitLE".} =
|
||||
template lowerBitLE*(bytes: var openArray[byte], pos: Natural) {.deprecated: "clearBitLE".} =
|
||||
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)
|
||||
|
||||
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
|
||||
## extracted from it, assuming 0 to be the possition of the most significant bit.
|
||||
let totalBits = data.len * 8
|
||||
|
@ -638,6 +638,6 @@ func getBitsBE*(data: openarray[byte], slice: HSlice, T: type[SomeUnsignedInt]):
|
|||
else:
|
||||
(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)
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ type
|
|||
BitArray*[bits: static int] = object
|
||||
bytes*: array[(bits + 7) div 8, byte]
|
||||
|
||||
func bitsLen*(bytes: openarray[byte]): int =
|
||||
func bitsLen*(bytes: openArray[byte]): int =
|
||||
let
|
||||
bytesCount = bytes.len
|
||||
lastByte = bytes[bytesCount - 1]
|
||||
|
@ -58,14 +58,14 @@ func add*(s: var BitSeq, value: bool) =
|
|||
s.Bytes[lastBytePos].changeBit 7, value
|
||||
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
|
||||
var shift = 0
|
||||
for b in bytes:
|
||||
result = result or (WordType(b) shl shift)
|
||||
shift += 8
|
||||
|
||||
func storeLEBytes(value: SomeUnsignedInt, dst: var openarray[byte]) =
|
||||
func storeLEBytes(value: SomeUnsignedInt, dst: var openArray[byte]) =
|
||||
when system.cpuEndian == bigEndian:
|
||||
var shift = 0
|
||||
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`
|
||||
# At the moment, it doesn't work quite well because Nim selects
|
||||
# the generic cmp[T] from the system module instead of choosing
|
||||
# the openarray overload
|
||||
func compareArrays[T](a, b: openarray[T]): int =
|
||||
# the openArray overload
|
||||
func compareArrays[T](a, b: openArray[T]): int =
|
||||
result = cmp(a.len, b.len)
|
||||
if result != 0: return
|
||||
|
||||
|
|
|
@ -59,17 +59,17 @@ func hexToByteArray*(hexStr: string, output: var openArray[byte])
|
|||
hexToByteArray(hexStr, output, 0, output.high)
|
||||
|
||||
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.
|
||||
hexToByteArray(hexStr, result)
|
||||
|
||||
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.
|
||||
hexToByteArray(hexStr, result)
|
||||
|
||||
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.
|
||||
hexToByteArray(hexStr, result)
|
||||
|
||||
|
@ -115,7 +115,7 @@ func hexToSeqByte*(hexStr: string): seq[byte]
|
|||
for i in 0 ..< N:
|
||||
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
|
||||
## Output is in lowercase
|
||||
## 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+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
|
||||
## Output is in lowercase
|
||||
## No "endianness" reordering is done.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
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
|
||||
## `content` completely to the file and closes the file afterwards.
|
||||
## Raises an IO exception in case of an error.
|
||||
|
|
|
@ -20,7 +20,7 @@ template init*(lvalue: var auto, a1, a2, a3: auto) =
|
|||
when not declared(default):
|
||||
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
|
||||
copyMem(addr result[0], unsafeAddr data[0], N)
|
||||
|
||||
|
|
|
@ -20,15 +20,15 @@ proc toMemRange*(x: string): MemRange =
|
|||
result.start = x.cstring.pointer
|
||||
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)
|
||||
result.start = unsafeAddr x[fromIdx]
|
||||
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)
|
||||
|
||||
proc toMemRange*[T](x: openarray[T]): MemRange {.inline.} =
|
||||
proc toMemRange*[T](x: openArray[T]): MemRange {.inline.} =
|
||||
toMemRange(x, 0, x.high)
|
||||
|
||||
template toMemRange*(mr: MemRange): MemRange = mr
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import ../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.
|
||||
# As much as I believe in the power of optimizing compilers, it turned
|
||||
|
|
|
@ -36,31 +36,31 @@ proc toImmutableRange[T](a: seq[T]): Range[T] =
|
|||
result.mLen = a.len
|
||||
|
||||
when unsafeAPIEnabled:
|
||||
proc toImmutableRangeNoGCHold[T](a: openarray[T]): Range[T] =
|
||||
proc toImmutableRangeNoGCHold[T](a: openArray[T]): Range[T] =
|
||||
if a.len != 0:
|
||||
result.start = unsafeAddr a[0]
|
||||
result.mLen = a.len
|
||||
|
||||
proc toImmutableRange[T](a: openarray[T]): Range[T] {.inline.} =
|
||||
proc toImmutableRange[T](a: openArray[T]): Range[T] {.inline.} =
|
||||
toImmutableRangeNoGCHold(a)
|
||||
|
||||
proc toRange*[T](a: var seq[T]): MutRange[T] {.inline.} =
|
||||
MutRange[T](toImmutableRange(a))
|
||||
|
||||
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))
|
||||
|
||||
template initStackRange*[T](sz: static[int]): MutRange[T] =
|
||||
var data: array[sz, T]
|
||||
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))
|
||||
|
||||
proc unsafeRangeConstruction*[T](a: openarray[T]): Range[T] {.inline.} =
|
||||
proc unsafeRangeConstruction*[T](a: openArray[T]): Range[T] {.inline.} =
|
||||
toImmutableRange(a)
|
||||
|
||||
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.} =
|
||||
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 b = r ^^ s.b
|
||||
let L = b - a + 1
|
||||
|
|
|
@ -36,7 +36,7 @@ proc writeMacroResultsNow* {.compileTime.} =
|
|||
file.add newCommentStmtNode("Generated at line " & $location.line)
|
||||
file.add macroOutput
|
||||
|
||||
for i in 0..< macroLocations.len:
|
||||
for i in 0 ..< macroLocations.len:
|
||||
addToFile files.mgetOrPut(macroLocations[i].filename, nil),
|
||||
macroLocations[i], macroOutputs[i]
|
||||
|
||||
|
@ -408,7 +408,7 @@ macro unpackArgs*(callee: untyped, args: untyped): untyped =
|
|||
for arg in args:
|
||||
let arg = if arg.kind == nnkHiddenStdConv: arg[1]
|
||||
else: arg
|
||||
if arg.kind == nnkArgList:
|
||||
if arg.kind == nnkArglist:
|
||||
for subarg in arg:
|
||||
result.add subarg
|
||||
else:
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
proc add*(str: var string, chars: openarray[char]) =
|
||||
proc add*(str: var string, chars: openArray[char]) =
|
||||
for c in chars:
|
||||
str.add c
|
||||
|
||||
|
|
|
@ -310,7 +310,7 @@ proc verify*[K,V](sl: var SortedSet[K,V]):
|
|||
## proc `$`*[V](data: V): string {.gcsafe,raises:[Defect,CatchableError].}
|
||||
##
|
||||
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))
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
proc add*(s: var string, data: openarray[char]) =
|
||||
proc add*(s: var string, data: openArray[char]) =
|
||||
if data.len > 0:
|
||||
let prevEnd = s.len
|
||||
s.setLen(prevEnd + data.len)
|
||||
|
|
|
@ -144,7 +144,7 @@ func getResult*[IntType, F](p: VarintParser[IntType, F]): IntType {.inline.} =
|
|||
else:
|
||||
p.res
|
||||
|
||||
func readVarint*(input: openarray[byte],
|
||||
func readVarint*(input: openArray[byte],
|
||||
outVal: var SomeInteger,
|
||||
flavour: static VarintFlavour = ProtoBuf): int =
|
||||
## Reads a varint from a buffer and stores it in `outVal`.
|
||||
|
|
|
@ -3,7 +3,7 @@ import ../stew/base58
|
|||
|
||||
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))
|
||||
var i = 0
|
||||
var k = 0
|
||||
|
|
|
@ -50,13 +50,13 @@ proc `$`(rc: KeyedQueuePair[uint,uint]): string =
|
|||
|
||||
proc `$`(rc: Result[KeyedQueuePair[uint,uint],void]): string =
|
||||
result = "<"
|
||||
if rc.isOK:
|
||||
if rc.isOk:
|
||||
result &= $rc.value.key & "," & $rc.value.data
|
||||
result &= ">"
|
||||
|
||||
proc `$`(rc: Result[uint,void]): string =
|
||||
result = "<"
|
||||
if rc.isOK:
|
||||
if rc.isOk:
|
||||
result &= $rc.value
|
||||
result &= ">"
|
||||
|
||||
|
@ -93,7 +93,7 @@ proc lruValue(lru: var LruCache; n: int): uint =
|
|||
let
|
||||
key = n.toKey
|
||||
rc = lru.q.lruFetch(key)
|
||||
if rc.isOK:
|
||||
if rc.isOk:
|
||||
return rc.value
|
||||
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":
|
||||
var seen: seq[int]
|
||||
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
|
||||
key = keyList[n].toKey
|
||||
canDeleteOk = (key.fromKey notin seen)
|
||||
|
@ -285,12 +285,12 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
seen: seq[int]
|
||||
all: seq[uint]
|
||||
rc = rq.first
|
||||
while rc.isOK:
|
||||
while rc.isOk:
|
||||
let key = rc.value.key
|
||||
all.add key
|
||||
rc = rq.next(key)
|
||||
rq.addOrFlushGroupwise(groupLen, seen, key.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all
|
||||
|
@ -303,7 +303,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
for w in rq.nextKeys:
|
||||
all.add w
|
||||
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all
|
||||
|
@ -315,7 +315,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
for w in rq.nextPairs:
|
||||
all.add w.key
|
||||
rq.addOrFlushGroupwise(groupLen, seen, w.key.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all
|
||||
|
@ -328,7 +328,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
let w = v.fromValue.toKey
|
||||
all.add w
|
||||
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all
|
||||
|
@ -340,12 +340,12 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
seen: seq[int]
|
||||
all: seq[uint]
|
||||
rc = rq.last
|
||||
while rc.isOK:
|
||||
while rc.isOk:
|
||||
let key = rc.value.key
|
||||
all.add key
|
||||
rc = rq.prev(key)
|
||||
rq.addOrFlushGroupwise(groupLen, seen, key.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all.reversed
|
||||
|
@ -358,7 +358,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
for w in rq.prevKeys:
|
||||
all.add w
|
||||
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all.reversed
|
||||
|
@ -370,7 +370,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
for w in rq.prevPairs:
|
||||
all.add w.key
|
||||
rq.addOrFlushGroupwise(groupLen, seen, w.key.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all.reversed
|
||||
|
@ -383,7 +383,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
let w = v.fromValue.toKey
|
||||
all.add w
|
||||
rq.addOrFlushGroupwise(groupLen, seen, w.fromKey, veryNoisy)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check seen.len == rq.len
|
||||
check seen.len < groupLen
|
||||
check uniqueKeys == all.reversed
|
||||
|
@ -400,7 +400,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
check uniqueKeys[count] == rc.value
|
||||
rc = rq.nextKey(rc.value)
|
||||
count.inc
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check count == uniqueKeys.len
|
||||
block:
|
||||
var
|
||||
|
@ -411,7 +411,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
check uniqueKeys[count] == rc.value.data.fromValue.toKey
|
||||
rc = rq.next(rc.value.key)
|
||||
count.inc
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check count == uniqueKeys.len
|
||||
|
||||
# reverse ...
|
||||
|
@ -424,7 +424,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
count.dec
|
||||
check uniqueKeys[count] == rc.value
|
||||
rc = rq.prevKey(rc.value)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check count == 0
|
||||
block:
|
||||
var
|
||||
|
@ -435,7 +435,7 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
count.dec
|
||||
check uniqueKeys[count] == rc.value.data.fromValue.toKey
|
||||
rc = rq.prev(rc.value.key)
|
||||
check rq.verify.isOK
|
||||
check rq.verify.isOk
|
||||
check count == 0
|
||||
|
||||
# --------------------------------------
|
||||
|
@ -450,8 +450,8 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
break
|
||||
let key = rc.value
|
||||
check rq.second.value.data == rq[key]
|
||||
check rq.delete(key).isOK
|
||||
check rq.verify.isOK
|
||||
check rq.delete(key).isOk
|
||||
check rq.verify.isOk
|
||||
check rq.len == 1
|
||||
|
||||
block:
|
||||
|
@ -463,8 +463,8 @@ suite "KeyedQueue: Data queue with keyed random access":
|
|||
break
|
||||
let key = rc.value
|
||||
check rq.beforeLast.value.data == rq[key]
|
||||
check rq.delete(key).isOK
|
||||
check rq.verify.isOK
|
||||
check rq.delete(key).isOk
|
||||
check rq.verify.isOk
|
||||
check rq.len == 1
|
||||
|
||||
# --------------------------------------
|
||||
|
@ -499,7 +499,7 @@ suite "KeyedQueue: Data queue as LRU cache":
|
|||
values = toSeq(cache.q.nextPairs).mapIt(it.data)
|
||||
infoPfx = if reSched: ">>> rotate" else: "+++ append"
|
||||
noisy.say infoPfx, &"{value} => {queue}"
|
||||
check cache.q.verify.isOK
|
||||
check cache.q.verify.isOk
|
||||
check queue.mapIt($it) == values.mapIt($it.fromValue)
|
||||
check item.toKey == cache.q.lastKey.value
|
||||
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.lruValue(77) == 77.toValue
|
||||
|
||||
check c1.q.verify.isOK
|
||||
check c2.q.verify.isOK
|
||||
check c1.q.verify.isOk
|
||||
check c2.q.verify.isOk
|
||||
|
||||
noisy.say &"c1Specs: {c1.size} {c1.q.firstKey} {c1.q.lastKey} ..."
|
||||
noisy.say &"c2Specs: {c2.size} {c2.q.firstKey} {c2.q.lastKey} ..."
|
||||
|
|
|
@ -115,7 +115,7 @@ suite "SortedSet: Sorted list based on red-black tree":
|
|||
test "Delete items":
|
||||
var seen: seq[int]
|
||||
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
|
||||
key = keyList[n]
|
||||
canDeleteOk = (key notin seen)
|
||||
|
|
Loading…
Reference in New Issue