Make $ on 0-length MemSlice produce Nim "" as per DMisener idea (#24015)

in https://forum.nim-lang.org/t/12463 which seems reasonable enough to
me.

stdlib has several dozens of places doing result.setLen now, but if
`experimental:strictDefs` move sot on by default and there is no easy
way to locally suppress the warning we can revisit this.
This commit is contained in:
c-blake
2024-09-04 05:01:55 -04:00
committed by GitHub
parent 080b0a03bd
commit 6908fb4011

View File

@@ -412,7 +412,7 @@ proc `==`*(x, y: MemSlice): bool =
proc `$`*(ms: MemSlice): string {.inline.} =
## Return a Nim string built from a MemSlice.
result.setLen(ms.size)
copyMem(addr(result[0]), ms.data, ms.size)
copyMem(result.cstring, ms.data, ms.size)
iterator memSlices*(mfile: MemFile, delim = '\l', eat = '\r'): MemSlice {.inline.} =
## Iterates over \[optional `eat`] `delim`-delimited slices in MemFile `mfile`.