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