This commit is contained in:
Andreas Rumpf 2019-12-31 09:42:18 +01:00 • committed by GitHub
commit ce40ed18bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View file

@ -37,16 +37,17 @@ proc resize(old: int): int {.inline.} =
else: result = old * 3 div 2 # for large arrays * 3/2 is better else: result = old * 3 div 2 # for large arrays * 3/2 is better
proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} = proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} =
if isLiteral(s) and addlen > 0: if isLiteral(s):
let oldP = s.p if addlen > 0:
# can't mutate a literal, so we need a fresh copy here: let oldP = s.p
let allocator = getLocalAllocator() # can't mutate a literal, so we need a fresh copy here:
s.p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(s.len + addlen))) let allocator = getLocalAllocator()
s.p.allocator = allocator s.p = cast[ptr NimStrPayload](allocator.alloc(allocator, contentSize(s.len + addlen)))
s.p.cap = s.len + addlen s.p.allocator = allocator
if s.len > 0: s.p.cap = s.len + addlen
# we are about to append, so there is no need to copy the \0 terminator: if s.len > 0:
copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len) # 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)
elif s.len + addlen > s.p.cap: elif s.len + addlen > s.p.cap:
let cap = max(s.len + addlen, resize(s.p.cap)) let cap = max(s.len + addlen, resize(s.p.cap))
s.p = cast[ptr NimStrPayload](s.p.allocator.realloc(s.p.allocator, s.p, s.p = cast[ptr NimStrPayload](s.p.allocator.realloc(s.p.allocator, s.p,

View file

@ -210,5 +210,9 @@ proc staticTests =
nonStaticTests() nonStaticTests()
staticTests() staticTests()
# bug #12965
let xaa = @[""].join()
let xbb = @["", ""].join()
let (a, d) = allocCounters() let (a, d) = allocCounters()
discard cprintf("%ld %ld\n", a, d) discard cprintf("%ld %ld\n", a, d)