strmantle.nim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2018 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Compilerprocs for strings that do not depend on the string implementation.
  10. const digitsTable = "0001020304050607080910111213141516171819" &
  11. "2021222324252627282930313233343536373839" &
  12. "4041424344454647484950515253545556575859" &
  13. "6061626364656667686970717273747576777879" &
  14. "8081828384858687888990919293949596979899"
  15. # Inspired by https://engineering.fb.com/2013/03/15/developer-tools/three-optimization-tips-for-c
  16. # Generates:
  17. # .. code-block:: nim
  18. # var res = ""
  19. # for i in 0 .. 99:
  20. # if i < 10:
  21. # res.add "0" & $i
  22. # else:
  23. # res.add $i
  24. # doAssert res == digitsTable
  25. func digits10(num: uint64): int {.noinline.} =
  26. if num < 10'u64:
  27. result = 1
  28. elif num < 100'u64:
  29. result = 2
  30. elif num < 1_000'u64:
  31. result = 3
  32. elif num < 10_000'u64:
  33. result = 4
  34. elif num < 100_000'u64:
  35. result = 5
  36. elif num < 1_000_000'u64:
  37. result = 6
  38. elif num < 10_000_000'u64:
  39. result = 7
  40. elif num < 100_000_000'u64:
  41. result = 8
  42. elif num < 1_000_000_000'u64:
  43. result = 9
  44. elif num < 10_000_000_000'u64:
  45. result = 10
  46. elif num < 100_000_000_000'u64:
  47. result = 11
  48. elif num < 1_000_000_000_000'u64:
  49. result = 12
  50. else:
  51. result = 12 + digits10(num div 1_000_000_000_000'u64)
  52. template numToString(result: var string, origin: uint64, length: int) =
  53. var num = origin
  54. var next = length - 1
  55. const nbatch = 100
  56. while num >= nbatch:
  57. let originNum = num
  58. num = num div nbatch
  59. let index = (originNum - num * nbatch) shl 1
  60. result[next] = digitsTable[index + 1]
  61. result[next - 1] = digitsTable[index]
  62. dec(next, 2)
  63. # process last 1-2 digits
  64. if num < 10:
  65. result[next] = chr(ord('0') + num)
  66. else:
  67. let index = num * 2
  68. result[next] = digitsTable[index + 1]
  69. result[next - 1] = digitsTable[index]
  70. proc cmpStrings(a, b: string): int {.inline, compilerproc.} =
  71. let alen = a.len
  72. let blen = b.len
  73. let minlen = min(alen, blen)
  74. if minlen > 0:
  75. result = c_memcmp(unsafeAddr a[0], unsafeAddr b[0], cast[csize_t](minlen))
  76. if result == 0:
  77. result = alen - blen
  78. else:
  79. result = alen - blen
  80. proc eqStrings(a, b: string): bool {.inline, compilerproc.} =
  81. let alen = a.len
  82. let blen = b.len
  83. if alen == blen:
  84. if alen == 0: return true
  85. return equalMem(unsafeAddr(a[0]), unsafeAddr(b[0]), alen)
  86. proc hashString(s: string): int {.compilerproc.} =
  87. # the compiler needs exactly the same hash function!
  88. # this used to be used for efficient generation of string case statements
  89. var h : uint = 0
  90. for i in 0..len(s)-1:
  91. h = h + uint(s[i])
  92. h = h + h shl 10
  93. h = h xor (h shr 6)
  94. h = h + h shl 3
  95. h = h xor (h shr 11)
  96. h = h + h shl 15
  97. result = cast[int](h)
  98. proc addInt*(result: var string; x: int64) =
  99. ## Converts integer to its string representation and appends it to `result`.
  100. ##
  101. ## .. code-block:: Nim
  102. ## var
  103. ## a = "123"
  104. ## b = 45
  105. ## a.addInt(b) # a <- "12345"
  106. let base = result.len
  107. var length: int
  108. var num: uint64
  109. if x < 0:
  110. if x == low(int64):
  111. num = uint64(x)
  112. else:
  113. num = uint64(-x)
  114. length = base + digits10(num) + 1
  115. setLen(result, length)
  116. result[base] = '-'
  117. else:
  118. num = uint64(x)
  119. length = base + digits10(num)
  120. setLen(result, length)
  121. numToString(result, num, length)
  122. proc nimIntToStr(x: int): string {.compilerRtl.} =
  123. result = newStringOfCap(sizeof(x)*4)
  124. result.addInt x
  125. proc addCstringN(result: var string, buf: cstring; buflen: int) =
  126. # no nimvm support needed, so it doesn't need to be fast here either
  127. let oldLen = result.len
  128. let newLen = oldLen + buflen
  129. result.setLen newLen
  130. copyMem(result[oldLen].addr, buf, buflen)
  131. import formatfloat
  132. proc addFloat*(result: var string; x: float) =
  133. ## Converts float to its string representation and appends it to `result`.
  134. ##
  135. ## .. code-block:: Nim
  136. ## var
  137. ## a = "123"
  138. ## b = 45.67
  139. ## a.addFloat(b) # a <- "12345.67"
  140. when nimvm:
  141. result.add $x
  142. else:
  143. var buffer {.noinit.}: array[65, char]
  144. let n = writeFloatToBuffer(buffer, x)
  145. result.addCstringN(cstring(buffer[0].addr), n)
  146. proc nimFloatToStr(f: float): string {.compilerproc.} =
  147. result = newStringOfCap(8)
  148. result.addFloat f
  149. proc c_strtod(buf: cstring, endptr: ptr cstring): float64 {.
  150. importc: "strtod", header: "<stdlib.h>", noSideEffect.}
  151. const
  152. IdentChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
  153. powtens = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  154. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  155. 1e20, 1e21, 1e22]
  156. when defined(nimHasInvariant):
  157. {.push staticBoundChecks: off.}
  158. proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
  159. start = 0): int {.compilerproc.} =
  160. # This routine attempt to parse float that can parsed quickly.
  161. # i.e. whose integer part can fit inside a 53bits integer.
  162. # their real exponent must also be <= 22. If the float doesn't follow
  163. # these restrictions, transform the float into this form:
  164. # INTEGER * 10 ^ exponent and leave the work to standard `strtod()`.
  165. # This avoid the problems of decimal character portability.
  166. # see: http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
  167. var
  168. i = start
  169. sign = 1.0
  170. kdigits, fdigits = 0
  171. exponent = 0
  172. integer = uint64(0)
  173. fracExponent = 0
  174. expSign = 1
  175. firstDigit = -1
  176. hasSign = false
  177. # Sign?
  178. if i < s.len and (s[i] == '+' or s[i] == '-'):
  179. hasSign = true
  180. if s[i] == '-':
  181. sign = -1.0
  182. inc(i)
  183. # NaN?
  184. if i+2 < s.len and (s[i] == 'N' or s[i] == 'n'):
  185. if s[i+1] == 'A' or s[i+1] == 'a':
  186. if s[i+2] == 'N' or s[i+2] == 'n':
  187. if i+3 >= s.len or s[i+3] notin IdentChars:
  188. number = NaN
  189. return i+3 - start
  190. return 0
  191. # Inf?
  192. if i+2 < s.len and (s[i] == 'I' or s[i] == 'i'):
  193. if s[i+1] == 'N' or s[i+1] == 'n':
  194. if s[i+2] == 'F' or s[i+2] == 'f':
  195. if i+3 >= s.len or s[i+3] notin IdentChars:
  196. number = Inf*sign
  197. return i+3 - start
  198. return 0
  199. if i < s.len and s[i] in {'0'..'9'}:
  200. firstDigit = (s[i].ord - '0'.ord)
  201. # Integer part?
  202. while i < s.len and s[i] in {'0'..'9'}:
  203. inc(kdigits)
  204. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  205. inc(i)
  206. while i < s.len and s[i] == '_': inc(i)
  207. # Fractional part?
  208. if i < s.len and s[i] == '.':
  209. inc(i)
  210. # if no integer part, Skip leading zeros
  211. if kdigits <= 0:
  212. while i < s.len and s[i] == '0':
  213. inc(fracExponent)
  214. inc(i)
  215. while i < s.len and s[i] == '_': inc(i)
  216. if firstDigit == -1 and i < s.len and s[i] in {'0'..'9'}:
  217. firstDigit = (s[i].ord - '0'.ord)
  218. # get fractional part
  219. while i < s.len and s[i] in {'0'..'9'}:
  220. inc(fdigits)
  221. inc(fracExponent)
  222. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  223. inc(i)
  224. while i < s.len and s[i] == '_': inc(i)
  225. # if has no digits: return error
  226. if kdigits + fdigits <= 0 and
  227. (i == start or # no char consumed (empty string).
  228. (i == start + 1 and hasSign)): # or only '+' or '-
  229. return 0
  230. if i+1 < s.len and s[i] in {'e', 'E'}:
  231. inc(i)
  232. if s[i] == '+' or s[i] == '-':
  233. if s[i] == '-':
  234. expSign = -1
  235. inc(i)
  236. if s[i] notin {'0'..'9'}:
  237. return 0
  238. while i < s.len and s[i] in {'0'..'9'}:
  239. exponent = exponent * 10 + (ord(s[i]) - ord('0'))
  240. inc(i)
  241. while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
  242. var realExponent = expSign*exponent - fracExponent
  243. let expNegative = realExponent < 0
  244. var absExponent = abs(realExponent)
  245. # if exponent greater than can be represented: +/- zero or infinity
  246. if absExponent > 999:
  247. if expNegative:
  248. number = 0.0*sign
  249. else:
  250. number = Inf*sign
  251. return i - start
  252. # if integer is representable in 53 bits: fast path
  253. # max fast path integer is 1<<53 - 1 or 8999999999999999 (16 digits)
  254. let digits = kdigits + fdigits
  255. if digits <= 15 or (digits <= 16 and firstDigit <= 8):
  256. # max float power of ten with set bits above the 53th bit is 10^22
  257. if absExponent <= 22:
  258. if expNegative:
  259. number = sign * integer.float / powtens[absExponent]
  260. else:
  261. number = sign * integer.float * powtens[absExponent]
  262. return i - start
  263. # if exponent is greater try to fit extra exponent above 22 by multiplying
  264. # integer part is there is space left.
  265. let slop = 15 - kdigits - fdigits
  266. if absExponent <= 22 + slop and not expNegative:
  267. number = sign * integer.float * powtens[slop] * powtens[absExponent-slop]
  268. return i - start
  269. # if failed: slow path with strtod.
  270. var t: array[500, char] # flaviu says: 325 is the longest reasonable literal
  271. var ti = 0
  272. let maxlen = t.high - "e+000".len # reserve enough space for exponent
  273. result = i - start
  274. i = start
  275. # re-parse without error checking, any error should be handled by the code above.
  276. if i < s.len and s[i] == '.': i.inc
  277. while i < s.len and s[i] in {'0'..'9','+','-'}:
  278. if ti < maxlen:
  279. t[ti] = s[i]; inc(ti)
  280. inc(i)
  281. while i < s.len and s[i] in {'.', '_'}: # skip underscore and decimal point
  282. inc(i)
  283. # insert exponent
  284. t[ti] = 'E'
  285. inc(ti)
  286. t[ti] = if expNegative: '-' else: '+'
  287. inc(ti, 4)
  288. # insert adjusted exponent
  289. t[ti-1] = ('0'.ord + absExponent mod 10).char
  290. absExponent = absExponent div 10
  291. t[ti-2] = ('0'.ord + absExponent mod 10).char
  292. absExponent = absExponent div 10
  293. t[ti-3] = ('0'.ord + absExponent mod 10).char
  294. number = c_strtod(addr t, nil)
  295. when defined(nimHasInvariant):
  296. {.pop.} # staticBoundChecks
  297. proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
  298. result = newStringOfCap(sizeof(x)*4)
  299. result.addInt x
  300. proc nimBoolToStr(x: bool): string {.compilerRtl.} =
  301. return if x: "true" else: "false"
  302. proc nimCharToStr(x: char): string {.compilerRtl.} =
  303. result = newString(1)
  304. result[0] = x
  305. when defined(gcDestructors):
  306. proc GC_getStatistics*(): string =
  307. result = "[GC] total memory: "
  308. result.addInt getTotalMem()
  309. result.add "\n[GC] occupied memory: "
  310. result.addInt getOccupiedMem()
  311. result.add '\n'
  312. #"[GC] cycle collections: " & $gch.stat.cycleCollections & "\n" &