This commit is contained in:
Andreas Rumpf 2020-02-21 21:06:41 +01:00 • committed by GitHub
commit 0d219d2c67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View file

@ -38,14 +38,13 @@ proc resize(old: int): int {.inline.} =
proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} = proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} =
if isLiteral(s): if isLiteral(s):
if addlen > 0: let oldP = s.p
let oldP = s.p # can't mutate a literal, so we need a fresh copy here:
# can't mutate a literal, so we need a fresh copy here: s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len + addlen)))
s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len + addlen))) s.p.cap = s.len + addlen
s.p.cap = s.len + addlen if s.len > 0:
if s.len > 0: # we are about to append, so there is no need to copy the \0 terminator:
# we are about to append, so there is no need to copy the \0 terminator: copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len)
copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len)
else: else:
let oldCap = s.p.cap and not strlitFlag let oldCap = s.p.cap and not strlitFlag
if s.len + addlen > oldCap: if s.len + addlen > oldCap:
@ -110,8 +109,10 @@ proc setLengthStrV2(s: var NimStringV2, newLen: int) {.compilerRtl.} =
if newLen == 0: if newLen == 0:
frees(s) frees(s)
s.p = nil s.p = nil
elif newLen > s.len or isLiteral(s): else:
prepareAdd(s, newLen - s.len) if newLen > s.len or isLiteral(s):
prepareAdd(s, newLen - s.len)
s.p.data[newLen] = '\0'
s.len = newLen s.len = newLen
proc nimAsgnStrV2(a: var NimStringV2, b: NimStringV2) {.compilerRtl.} = proc nimAsgnStrV2(a: var NimStringV2, b: NimStringV2) {.compilerRtl.} =

View file

@ -7,7 +7,8 @@ axc
... ...
destroying GenericObj[T] GenericObj[system.int] destroying GenericObj[T] GenericObj[system.int]
test test
(allocCount: 17, deallocCount: 15)''' (allocCount: 17, deallocCount: 15)
3'''
""" """
import system / ansi_c import system / ansi_c
@ -132,3 +133,9 @@ proc xx(xml: string): MyObject =
discard xx("test") discard xx("test")
echo getAllocStats() - s1 echo getAllocStats() - s1
# bug #13457
var s = "abcde"
s.setLen(3)
echo s.cstring.len