fix fromBytes for zero length arrays
This commit is contained in:
parent
ca8fb7ebd0
commit
76beeb769e
|
@ -145,8 +145,9 @@ func toBytes*(s: string): seq[byte] =
|
|||
@(s.toOpenArrayByte(0, s.high))
|
||||
|
||||
func fromBytes*(T: type string, v: openArray[byte]): string =
|
||||
result = newString(v.len)
|
||||
copyMem(addr result[0], unsafeAddr v[0], v.len)
|
||||
if v.len > 0:
|
||||
result = newString(v.len)
|
||||
copyMem(addr result[0], unsafeAddr v[0], v.len)
|
||||
|
||||
func `<`*(a, b: openArray[byte]): bool =
|
||||
## Lexicographical compare of two byte arrays
|
||||
|
|
|
@ -88,6 +88,9 @@ suite "Byte utils":
|
|||
string.fromBytes([byte(ord('a'))]) == "a"
|
||||
cast[ptr UncheckedArray[byte]](cstring(string.fromBytes([byte(ord('a'))])))[1] == byte(0)
|
||||
|
||||
"".toBytes().len() == 0
|
||||
string.fromBytes([]) == ""
|
||||
|
||||
test "slices":
|
||||
var a: array[4, byte]
|
||||
a[0..<2] = [2'u8, 3]
|
||||
|
|
Loading…
Reference in New Issue