strs_v2.nim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2017 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Default new string implementation used by Nim's core.
  10. type
  11. NimStrPayloadBase = object
  12. cap: int
  13. NimStrPayload {.core.} = object
  14. cap: int
  15. data: UncheckedArray[char]
  16. NimStringV2 {.core.} = object
  17. len: int
  18. p: ptr NimStrPayload ## can be nil if len == 0.
  19. const nimStrVersion {.core.} = 2
  20. template isLiteral(s): bool = (s.p == nil) or (s.p.cap and strlitFlag) == strlitFlag
  21. template contentSize(cap): int = cap + 1 + sizeof(NimStrPayloadBase)
  22. template frees(s) =
  23. if not isLiteral(s):
  24. when compileOption("threads"):
  25. deallocShared(s.p)
  26. else:
  27. dealloc(s.p)
  28. proc resize(old: int): int {.inline.} =
  29. if old <= 0: result = 4
  30. elif old < 65536: result = old * 2
  31. else: result = old * 3 div 2 # for large arrays * 3/2 is better
  32. proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} =
  33. if isLiteral(s):
  34. let oldP = s.p
  35. # can't mutate a literal, so we need a fresh copy here:
  36. when compileOption("threads"):
  37. s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len + addlen)))
  38. else:
  39. s.p = cast[ptr NimStrPayload](alloc0(contentSize(s.len + addlen)))
  40. s.p.cap = s.len + addlen
  41. if s.len > 0:
  42. # we are about to append, so there is no need to copy the \0 terminator:
  43. copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len)
  44. else:
  45. let oldCap = s.p.cap and not strlitFlag
  46. if s.len + addlen > oldCap:
  47. let newCap = max(s.len + addlen, resize(oldCap))
  48. when compileOption("threads"):
  49. s.p = cast[ptr NimStrPayload](reallocShared0(s.p, contentSize(oldCap), contentSize(newCap)))
  50. else:
  51. s.p = cast[ptr NimStrPayload](realloc0(s.p, contentSize(oldCap), contentSize(newCap)))
  52. s.p.cap = newCap
  53. proc nimAddCharV1(s: var NimStringV2; c: char) {.compilerRtl, inline.} =
  54. #if (s.p == nil) or (s.len+1 > s.p.cap and not strlitFlag):
  55. prepareAdd(s, 1)
  56. s.p.data[s.len] = c
  57. s.p.data[s.len+1] = '\0'
  58. inc s.len
  59. proc toNimStr(str: cstring, len: int): NimStringV2 {.compilerproc.} =
  60. if len <= 0:
  61. result = NimStringV2(len: 0, p: nil)
  62. else:
  63. when compileOption("threads"):
  64. var p = cast[ptr NimStrPayload](allocShared0(contentSize(len)))
  65. else:
  66. var p = cast[ptr NimStrPayload](alloc0(contentSize(len)))
  67. p.cap = len
  68. if len > 0:
  69. # we are about to append, so there is no need to copy the \0 terminator:
  70. copyMem(unsafeAddr p.data[0], str, len)
  71. result = NimStringV2(len: len, p: p)
  72. proc cstrToNimstr(str: cstring): NimStringV2 {.compilerRtl.} =
  73. if str == nil: toNimStr(str, 0)
  74. else: toNimStr(str, str.len)
  75. proc nimToCStringConv(s: NimStringV2): cstring {.compilerproc, nonReloadable, inline.} =
  76. if s.len == 0: result = cstring""
  77. else: result = cstring(unsafeAddr s.p.data)
  78. proc appendString(dest: var NimStringV2; src: NimStringV2) {.compilerproc, inline.} =
  79. if src.len > 0:
  80. # also copy the \0 terminator:
  81. copyMem(unsafeAddr dest.p.data[dest.len], unsafeAddr src.p.data[0], src.len+1)
  82. inc dest.len, src.len
  83. proc appendChar(dest: var NimStringV2; c: char) {.compilerproc, inline.} =
  84. dest.p.data[dest.len] = c
  85. dest.p.data[dest.len+1] = '\0'
  86. inc dest.len
  87. proc rawNewString(space: int): NimStringV2 {.compilerproc.} =
  88. # this is also 'system.newStringOfCap'.
  89. if space <= 0:
  90. result = NimStringV2(len: 0, p: nil)
  91. else:
  92. when compileOption("threads"):
  93. var p = cast[ptr NimStrPayload](allocShared0(contentSize(space)))
  94. else:
  95. var p = cast[ptr NimStrPayload](alloc0(contentSize(space)))
  96. p.cap = space
  97. result = NimStringV2(len: 0, p: p)
  98. proc mnewString(len: int): NimStringV2 {.compilerproc.} =
  99. if len <= 0:
  100. result = NimStringV2(len: 0, p: nil)
  101. else:
  102. when compileOption("threads"):
  103. var p = cast[ptr NimStrPayload](allocShared0(contentSize(len)))
  104. else:
  105. var p = cast[ptr NimStrPayload](alloc0(contentSize(len)))
  106. p.cap = len
  107. result = NimStringV2(len: len, p: p)
  108. proc setLengthStrV2(s: var NimStringV2, newLen: int) {.compilerRtl.} =
  109. if newLen == 0:
  110. discard "do not free the buffer here, pattern 's.setLen 0' is common for avoiding allocations"
  111. else:
  112. if newLen > s.len or isLiteral(s):
  113. prepareAdd(s, newLen - s.len)
  114. s.p.data[newLen] = '\0'
  115. s.len = newLen
  116. proc nimAsgnStrV2(a: var NimStringV2, b: NimStringV2) {.compilerRtl.} =
  117. if a.p == b.p: return
  118. if isLiteral(b):
  119. # we can shallow copy literals:
  120. frees(a)
  121. a.len = b.len
  122. a.p = b.p
  123. else:
  124. if isLiteral(a) or (a.p.cap and not strlitFlag) < b.len:
  125. # we have to allocate the 'cap' here, consider
  126. # 'let y = newStringOfCap(); var x = y'
  127. # on the other hand... These get turned into moves now.
  128. frees(a)
  129. when compileOption("threads"):
  130. a.p = cast[ptr NimStrPayload](allocShared0(contentSize(b.len)))
  131. else:
  132. a.p = cast[ptr NimStrPayload](alloc0(contentSize(b.len)))
  133. a.p.cap = b.len
  134. a.len = b.len
  135. copyMem(unsafeAddr a.p.data[0], unsafeAddr b.p.data[0], b.len+1)
  136. proc nimPrepareStrMutationImpl(s: var NimStringV2) =
  137. let oldP = s.p
  138. # can't mutate a literal, so we need a fresh copy here:
  139. when compileOption("threads"):
  140. s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len)))
  141. else:
  142. s.p = cast[ptr NimStrPayload](alloc0(contentSize(s.len)))
  143. s.p.cap = s.len
  144. copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len+1)
  145. proc nimPrepareStrMutationV2(s: var NimStringV2) {.compilerRtl, inline.} =
  146. if s.p != nil and (s.p.cap and strlitFlag) == strlitFlag:
  147. nimPrepareStrMutationImpl(s)