bugfix: +1 unnecessary in resizeString; caused annoying reallocations for strings created with 'newStringOfCap'

This commit is contained in:
Araq 2012-01-02 23:53:07 +01:00
commit b5aafb4cf3
3 changed files with 5 additions and 6 deletions

View file

@ -1,7 +1,7 @@
# #
# #
# Nimrod's Runtime Library # Nimrod's Runtime Library
# (c) Copyright 2011 Andreas Rumpf # (c) Copyright 2012 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.

View file

@ -50,7 +50,7 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.compilerProc.} =
result = rawNewString(len) result = rawNewString(len)
result.len = len result.len = len
c_memcpy(result.data, addr(s.data[start]), len * sizeof(Char)) c_memcpy(result.data, addr(s.data[start]), len * sizeof(Char))
result.data[len] = '\0' #result.data[len] = '\0'
else: else:
result = rawNewString(len) result = rawNewString(len)
@ -61,7 +61,7 @@ proc toNimStr(str: CString, len: int): NimString {.compilerProc.} =
result = rawNewString(len) result = rawNewString(len)
result.len = len result.len = len
c_memcpy(result.data, str, (len+1) * sizeof(Char)) c_memcpy(result.data, str, (len+1) * sizeof(Char))
result.data[len] = '\0' # readline relies on this! #result.data[len] = '\0' # readline relies on this!
proc cstrToNimstr(str: CString): NimString {.compilerProc.} = proc cstrToNimstr(str: CString): NimString {.compilerProc.} =
result = toNimstr(str, c_strlen(str)) result = toNimstr(str, c_strlen(str))
@ -146,10 +146,10 @@ proc addChar(s: NimString, c: char): NimString =
# s = rawNewString(0); # s = rawNewString(0);
proc resizeString(dest: NimString, addlen: int): NimString {.compilerproc.} = proc resizeString(dest: NimString, addlen: int): NimString {.compilerproc.} =
if dest.len + addLen + 1 <= dest.space: if dest.len + addLen <= dest.space:
result = dest result = dest
else: # slow path: else: # slow path:
var sp = max(resize(dest.space), dest.len + addLen + 1) var sp = max(resize(dest.space), dest.len + addLen)
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + result = cast[NimString](growObj(dest, sizeof(TGenericSeq) +
(sp+1) * sizeof(Char))) (sp+1) * sizeof(Char)))
result.space = sp result.space = sp

View file

@ -1,7 +1,6 @@
version 0.8.14 version 0.8.14
============== ==============
- check resizeString and fix +1 bug
- BUG: type TX = TTable[string, int] - BUG: type TX = TTable[string, int]
- BUG: len(openArray) breaks symbol files - BUG: len(openArray) breaks symbol files
- BUG: temp3.nim triggers weird compiler bug - BUG: temp3.nim triggers weird compiler bug