mirror of
https://github.com/status-im/nim-stew.git
synced 2025-01-09 19:56:09 +00:00
byteutils: lexicographical less than
This commit is contained in:
parent
e9d75c05f6
commit
9c18a1cc55
@ -129,3 +129,12 @@ func toBytes*(s: string): seq[byte] =
|
|||||||
## nim essentially are byte sequences without any particular encoding, this
|
## nim essentially are byte sequences without any particular encoding, this
|
||||||
## is almost a noop
|
## is almost a noop
|
||||||
cast[seq[byte]](s)
|
cast[seq[byte]](s)
|
||||||
|
|
||||||
|
func `<`*(a, b: openArray[byte]): bool =
|
||||||
|
## Lexicographical compare of two byte arrays
|
||||||
|
let minlen = min(a.len, b.len)
|
||||||
|
|
||||||
|
for i in 0..<minlen:
|
||||||
|
if a[i] != b[i]: return a[i] < b[i]
|
||||||
|
|
||||||
|
a.len < b.len
|
||||||
|
@ -59,3 +59,25 @@ suite "Byte utils":
|
|||||||
block:
|
block:
|
||||||
expect AssertionError:
|
expect AssertionError:
|
||||||
let a = hexToPaddedByteArray[2]("0x12345")
|
let a = hexToPaddedByteArray[2]("0x12345")
|
||||||
|
|
||||||
|
test "lessThan":
|
||||||
|
let
|
||||||
|
a = [0'u8, 1, 2]
|
||||||
|
b = [2'u8, 1, 0]
|
||||||
|
c = [0'u8, 1, 2, 3]
|
||||||
|
d = [0'u8, 1, 3, 3]
|
||||||
|
|
||||||
|
check:
|
||||||
|
not (a < a)
|
||||||
|
|
||||||
|
a < b
|
||||||
|
not (b < a)
|
||||||
|
|
||||||
|
c < b
|
||||||
|
not (b < c)
|
||||||
|
|
||||||
|
a < c
|
||||||
|
not (c < a)
|
||||||
|
|
||||||
|
c < d
|
||||||
|
not (d < c)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user