strmantle.nim 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 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 = 0'u
  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. {.push staticBoundChecks: off.}
  68. proc nimParseBiggestFloat(s: openArray[char], number: var BiggestFloat,
  69. ): int {.compilerproc.} =
  70. # This routine attempt to parse float that can parsed quickly.
  71. # i.e. whose integer part can fit inside a 53bits integer.
  72. # their real exponent must also be <= 22. If the float doesn't follow
  73. # these restrictions, transform the float into this form:
  74. # INTEGER * 10 ^ exponent and leave the work to standard `strtod()`.
  75. # This avoid the problems of decimal character portability.
  76. # see: http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
  77. var
  78. i = 0
  79. sign = 1.0
  80. kdigits, fdigits = 0
  81. exponent = 0
  82. integer = uint64(0)
  83. fracExponent = 0
  84. expSign = 1
  85. firstDigit = -1
  86. hasSign = false
  87. # Sign?
  88. if i < s.len and (s[i] == '+' or s[i] == '-'):
  89. hasSign = true
  90. if s[i] == '-':
  91. sign = -1.0
  92. inc(i)
  93. # NaN?
  94. if i+2 < s.len and (s[i] == 'N' or s[i] == 'n'):
  95. if s[i+1] == 'A' or s[i+1] == 'a':
  96. if s[i+2] == 'N' or s[i+2] == 'n':
  97. if i+3 >= s.len or s[i+3] notin IdentChars:
  98. number = NaN
  99. return i+3
  100. return 0
  101. # Inf?
  102. if i+2 < s.len and (s[i] == 'I' or s[i] == 'i'):
  103. if s[i+1] == 'N' or s[i+1] == 'n':
  104. if s[i+2] == 'F' or s[i+2] == 'f':
  105. if i+3 >= s.len or s[i+3] notin IdentChars:
  106. number = Inf*sign
  107. return i+3
  108. return 0
  109. if i < s.len and s[i] in {'0'..'9'}:
  110. firstDigit = (s[i].ord - '0'.ord)
  111. # Integer part?
  112. while i < s.len and s[i] in {'0'..'9'}:
  113. inc(kdigits)
  114. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  115. inc(i)
  116. while i < s.len and s[i] == '_': inc(i)
  117. # Fractional part?
  118. if i < s.len and s[i] == '.':
  119. inc(i)
  120. # if no integer part, Skip leading zeros
  121. if kdigits <= 0:
  122. while i < s.len and s[i] == '0':
  123. inc(fracExponent)
  124. inc(i)
  125. while i < s.len and s[i] == '_': inc(i)
  126. if firstDigit == -1 and i < s.len and s[i] in {'0'..'9'}:
  127. firstDigit = (s[i].ord - '0'.ord)
  128. # get fractional part
  129. while i < s.len and s[i] in {'0'..'9'}:
  130. inc(fdigits)
  131. inc(fracExponent)
  132. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  133. inc(i)
  134. while i < s.len and s[i] == '_': inc(i)
  135. # if has no digits: return error
  136. if kdigits + fdigits <= 0 and
  137. (i == 0 or # no char consumed (empty string).
  138. (i == 1 and hasSign)): # or only '+' or '-
  139. return 0
  140. if i+1 < s.len and s[i] in {'e', 'E'}:
  141. inc(i)
  142. if s[i] == '+' or s[i] == '-':
  143. if s[i] == '-':
  144. expSign = -1
  145. inc(i)
  146. if s[i] notin {'0'..'9'}:
  147. return 0
  148. while i < s.len and s[i] in {'0'..'9'}:
  149. exponent = exponent * 10 + (ord(s[i]) - ord('0'))
  150. inc(i)
  151. while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
  152. var realExponent = expSign*exponent - fracExponent
  153. let expNegative = realExponent < 0
  154. var absExponent = abs(realExponent)
  155. # if exponent greater than can be represented: +/- zero or infinity
  156. if absExponent > 999:
  157. if integer == 0:
  158. number = 0.0
  159. elif expNegative:
  160. number = 0.0*sign
  161. else:
  162. number = Inf*sign
  163. return i
  164. # if integer is representable in 53 bits: fast path
  165. # max fast path integer is 1<<53 - 1 or 8999999999999999 (16 digits)
  166. let digits = kdigits + fdigits
  167. if digits <= 15 or (digits <= 16 and firstDigit <= 8):
  168. # max float power of ten with set bits above the 53th bit is 10^22
  169. if absExponent <= 22:
  170. if expNegative:
  171. number = sign * integer.float / powtens[absExponent]
  172. else:
  173. number = sign * integer.float * powtens[absExponent]
  174. return i
  175. # if exponent is greater try to fit extra exponent above 22 by multiplying
  176. # integer part is there is space left.
  177. let slop = 15 - kdigits - fdigits
  178. if absExponent <= 22 + slop and not expNegative:
  179. number = sign * integer.float * powtens[slop] * powtens[absExponent-slop]
  180. return i
  181. # if failed: slow path with strtod.
  182. var t: array[500, char] # flaviu says: 325 is the longest reasonable literal
  183. var ti = 0
  184. let maxlen = t.high - "e+000".len # reserve enough space for exponent
  185. let endPos = i
  186. result = endPos
  187. i = 0
  188. # re-parse without error checking, any error should be handled by the code above.
  189. if i < endPos and s[i] == '.': i.inc
  190. while i < endPos and s[i] in {'0'..'9','+','-'}:
  191. if ti < maxlen:
  192. t[ti] = s[i]; inc(ti)
  193. inc(i)
  194. while i < endPos and s[i] in {'.', '_'}: # skip underscore and decimal point
  195. inc(i)
  196. # insert exponent
  197. t[ti] = 'E'
  198. inc(ti)
  199. t[ti] = if expNegative: '-' else: '+'
  200. inc(ti, 4)
  201. # insert adjusted exponent
  202. t[ti-1] = ('0'.ord + absExponent mod 10).char
  203. absExponent = absExponent div 10
  204. t[ti-2] = ('0'.ord + absExponent mod 10).char
  205. absExponent = absExponent div 10
  206. t[ti-3] = ('0'.ord + absExponent mod 10).char
  207. number = c_strtod(cast[cstring](addr t), nil)
  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" &