strmantle.nim 7.0 KB

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