mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-31 10:22:15 +00:00
setLen doesn't crash on nil strings/seqs anymore
This commit is contained in:
@@ -1500,13 +1500,13 @@ proc genSetLengthSeq(p: BProc, e: PNode, d: var TLoc) =
|
||||
initLocExpr(p, e.sons[2], b)
|
||||
let t = skipTypes(e.sons[1].typ, {tyVar})
|
||||
let setLenPattern = if not p.module.compileToCpp:
|
||||
"$1 = ($3) #setLengthSeq(&($1)->Sup, sizeof($4), $2);$n"
|
||||
"$1 = ($3) #setLengthSeqV2(&($1)->Sup, $4, $2);$n"
|
||||
else:
|
||||
"$1 = ($3) #setLengthSeq($1, sizeof($4), $2);$n"
|
||||
"$1 = ($3) #setLengthSeqV2($1, $4, $2);$n"
|
||||
|
||||
lineCg(p, cpsStmts, setLenPattern, [
|
||||
rdLoc(a), rdLoc(b), getTypeDesc(p.module, t),
|
||||
getTypeDesc(p.module, t.skipTypes(abstractInst).sons[0])])
|
||||
genTypeInfo(p.module, t.skipTypes(abstractInst), e.info)])
|
||||
gcUsage(e)
|
||||
|
||||
proc genSetLengthStr(p: BProc, e: PNode, d: var TLoc) =
|
||||
|
||||
@@ -226,8 +226,8 @@ proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =
|
||||
|
||||
proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} =
|
||||
var n = max(newLen, 0)
|
||||
if wasMoved(s):
|
||||
result = newOwnedString(s, n)
|
||||
if s == nil:
|
||||
result = mnewString(newLen)
|
||||
elif n <= s.space:
|
||||
result = s
|
||||
else:
|
||||
@@ -313,6 +313,13 @@ proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
|
||||
(newLen*%elemSize)), (result.len-%newLen) *% elemSize)
|
||||
result.len = newLen
|
||||
|
||||
proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
|
||||
compilerRtl.} =
|
||||
if s == nil:
|
||||
result = cast[PGenericSeq](newSeq(typ, newLen))
|
||||
else:
|
||||
result = setLengthSeq(s, typ.base.size, newLen)
|
||||
|
||||
# --------------- other string routines ----------------------------------
|
||||
proc add*(result: var string; x: int64) =
|
||||
let base = result.len
|
||||
|
||||
Reference in New Issue
Block a user