strmantle.nim 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. import std/private/digitsutils
  11. proc cmpStrings(a, b: string): int {.inline, compilerproc.} =
  12. let alen = a.len
  13. let blen = b.len
  14. let minlen = min(alen, blen)
  15. if minlen > 0:
  16. result = c_memcmp(unsafeAddr a[0], unsafeAddr b[0], cast[csize_t](minlen)).int
  17. if result == 0:
  18. result = alen - blen
  19. else:
  20. result = alen - blen
  21. proc leStrings(a, b: string): bool {.inline, compilerproc.} =
  22. # required by upcoming backends (NIR).
  23. cmpStrings(a, b) <= 0
  24. proc ltStrings(a, b: string): bool {.inline, compilerproc.} =
  25. # required by upcoming backends (NIR).
  26. cmpStrings(a, b) < 0
  27. proc eqStrings(a, b: string): bool {.inline, compilerproc.} =
  28. let alen = a.len
  29. let blen = b.len
  30. if alen == blen:
  31. if alen == 0: return true
  32. return equalMem(unsafeAddr(a[0]), unsafeAddr(b[0]), alen)
  33. proc hashString(s: string): int {.compilerproc.} =
  34. # the compiler needs exactly the same hash function!
  35. # this used to be used for efficient generation of string case statements
  36. var h = 0'u
  37. for i in 0..len(s)-1:
  38. h = h + uint(s[i])
  39. h = h + h shl 10
  40. h = h xor (h shr 6)
  41. h = h + h shl 3
  42. h = h xor (h shr 11)
  43. h = h + h shl 15
  44. result = cast[int](h)
  45. proc eqCstrings(a, b: cstring): bool {.inline, compilerproc.} =
  46. if pointer(a) == pointer(b): result = true
  47. elif a.isNil or b.isNil: result = false
  48. else: result = c_strcmp(a, b) == 0
  49. proc hashCstring(s: cstring): int {.compilerproc.} =
  50. # the compiler needs exactly the same hash function!
  51. # this used to be used for efficient generation of cstring case statements
  52. if s.isNil: return 0
  53. var h : uint = 0
  54. var i = 0
  55. while true:
  56. let c = s[i]
  57. if c == '\0': break
  58. h = h + uint(c)
  59. h = h + h shl 10
  60. h = h xor (h shr 6)
  61. inc i
  62. h = h + h shl 3
  63. h = h xor (h shr 11)
  64. h = h + h shl 15
  65. result = cast[int](h)
  66. proc c_strtod(buf: cstring, endptr: ptr cstring): float64 {.
  67. importc: "strtod", header: "<stdlib.h>", noSideEffect.}
  68. const
  69. IdentChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
  70. powtens = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  71. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  72. 1e20, 1e21, 1e22]
  73. {.push staticBoundChecks: off.}
  74. proc nimParseBiggestFloat(s: openArray[char], number: var BiggestFloat,
  75. ): int {.compilerproc.} =
  76. # This routine attempt to parse float that can parsed quickly.
  77. # i.e. whose integer part can fit inside a 53bits integer.
  78. # their real exponent must also be <= 22. If the float doesn't follow
  79. # these restrictions, transform the float into this form:
  80. # INTEGER * 10 ^ exponent and leave the work to standard `strtod()`.
  81. # This avoid the problems of decimal character portability.
  82. # see: http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
  83. var
  84. i = 0
  85. sign = 1.0
  86. kdigits, fdigits = 0
  87. exponent = 0
  88. integer = uint64(0)
  89. fracExponent = 0
  90. expSign = 1
  91. firstDigit = -1
  92. hasSign = false
  93. # Sign?
  94. if i < s.len and (s[i] == '+' or s[i] == '-'):
  95. hasSign = true
  96. if s[i] == '-':
  97. sign = -1.0
  98. inc(i)
  99. # NaN?
  100. if i+2 < s.len and (s[i] == 'N' or s[i] == 'n'):
  101. if s[i+1] == 'A' or s[i+1] == 'a':
  102. if s[i+2] == 'N' or s[i+2] == 'n':
  103. if i+3 >= s.len or s[i+3] notin IdentChars:
  104. number = NaN
  105. return i+3
  106. return 0
  107. # Inf?
  108. if i+2 < s.len and (s[i] == 'I' or s[i] == 'i'):
  109. if s[i+1] == 'N' or s[i+1] == 'n':
  110. if s[i+2] == 'F' or s[i+2] == 'f':
  111. if i+3 >= s.len or s[i+3] notin IdentChars:
  112. number = Inf*sign
  113. return i+3
  114. return 0
  115. if i < s.len and s[i] in {'0'..'9'}:
  116. firstDigit = (s[i].ord - '0'.ord)
  117. # Integer part?
  118. while i < s.len and s[i] in {'0'..'9'}:
  119. inc(kdigits)
  120. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  121. inc(i)
  122. while i < s.len and s[i] == '_': inc(i)
  123. # Fractional part?
  124. if i < s.len and s[i] == '.':
  125. inc(i)
  126. # if no integer part, Skip leading zeros
  127. if kdigits <= 0:
  128. while i < s.len and s[i] == '0':
  129. inc(fracExponent)
  130. inc(i)
  131. while i < s.len and s[i] == '_': inc(i)
  132. if firstDigit == -1 and i < s.len and s[i] in {'0'..'9'}:
  133. firstDigit = (s[i].ord - '0'.ord)
  134. # get fractional part
  135. while i < s.len and s[i] in {'0'..'9'}:
  136. inc(fdigits)
  137. inc(fracExponent)
  138. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  139. inc(i)
  140. while i < s.len and s[i] == '_': inc(i)
  141. # if has no digits: return error
  142. if kdigits + fdigits <= 0 and
  143. (i == 0 or # no char consumed (empty string).
  144. (i == 1 and hasSign)): # or only '+' or '-
  145. return 0
  146. if i+1 < s.len and s[i] in {'e', 'E'}:
  147. inc(i)
  148. if s[i] == '+' or s[i] == '-':
  149. if s[i] == '-':
  150. expSign = -1
  151. inc(i)
  152. if s[i] notin {'0'..'9'}:
  153. return 0
  154. while i < s.len and s[i] in {'0'..'9'}:
  155. exponent = exponent * 10 + (ord(s[i]) - ord('0'))
  156. inc(i)
  157. while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
  158. var realExponent = expSign*exponent - fracExponent
  159. let expNegative = realExponent < 0
  160. var absExponent = abs(realExponent)
  161. # if exponent greater than can be represented: +/- zero or infinity
  162. if absExponent > 999:
  163. if integer == 0:
  164. number = 0.0
  165. elif expNegative:
  166. number = 0.0*sign
  167. else:
  168. number = Inf*sign
  169. return i
  170. # if integer is representable in 53 bits: fast path
  171. # max fast path integer is 1<<53 - 1 or 8999999999999999 (16 digits)
  172. let digits = kdigits + fdigits
  173. if digits <= 15 or (digits <= 16 and firstDigit <= 8):
  174. # max float power of ten with set bits above the 53th bit is 10^22
  175. if absExponent <= 22:
  176. if expNegative:
  177. number = sign * integer.float / powtens[absExponent]
  178. else:
  179. number = sign * integer.float * powtens[absExponent]
  180. return i
  181. # if exponent is greater try to fit extra exponent above 22 by multiplying
  182. # integer part is there is space left.
  183. let slop = 15 - kdigits - fdigits
  184. if absExponent <= 22 + slop and not expNegative:
  185. number = sign * integer.float * powtens[slop] * powtens[absExponent-slop]
  186. return i
  187. # if failed: slow path with strtod.
  188. var t: array[500, char] # flaviu says: 325 is the longest reasonable literal
  189. var ti = 0
  190. let maxlen = t.high - "e+000".len # reserve enough space for exponent
  191. let endPos = i
  192. result = endPos
  193. i = 0
  194. # re-parse without error checking, any error should be handled by the code above.
  195. if i < endPos and s[i] == '.': i.inc
  196. while i < endPos and s[i] in {'0'..'9','+','-'}:
  197. if ti < maxlen:
  198. t[ti] = s[i]; inc(ti)
  199. inc(i)
  200. while i < endPos and s[i] in {'.', '_'}: # skip underscore and decimal point
  201. inc(i)
  202. # insert exponent
  203. t[ti] = 'E'
  204. inc(ti)
  205. t[ti] = if expNegative: '-' else: '+'
  206. inc(ti, 4)
  207. # insert adjusted exponent
  208. t[ti-1] = ('0'.ord + absExponent mod 10).char
  209. absExponent = absExponent div 10
  210. t[ti-2] = ('0'.ord + absExponent mod 10).char
  211. absExponent = absExponent div 10
  212. t[ti-3] = ('0'.ord + absExponent mod 10).char
  213. number = c_strtod(cast[cstring](addr t), nil)
  214. {.pop.} # staticBoundChecks
  215. proc nimBoolToStr(x: bool): string {.compilerRtl.} =
  216. return if x: "true" else: "false"
  217. proc nimCharToStr(x: char): string {.compilerRtl.} =
  218. result = newString(1)
  219. result[0] = x
  220. when defined(gcDestructors):
  221. proc GC_getStatistics*(): string =
  222. result = "[GC] total memory: "
  223. result.addInt getTotalMem()
  224. result.add "\n[GC] occupied memory: "
  225. result.addInt getOccupiedMem()
  226. result.add '\n'
  227. #"[GC] cycle collections: " & $gch.stat.cycleCollections & "\n" &