strmantle.nim 7.6 KB

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