strmantle.nim 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. proc cmpStrings(a, b: string): int {.inline, compilerproc.} =
  11. let alen = a.len
  12. let blen = b.len
  13. let minlen = min(alen, blen)
  14. if minlen > 0:
  15. result = c_memcmp(unsafeAddr a[0], unsafeAddr b[0], minlen.csize)
  16. if result == 0:
  17. result = alen - blen
  18. else:
  19. result = alen - blen
  20. proc eqStrings(a, b: string): bool {.inline, compilerproc.} =
  21. let alen = a.len
  22. let blen = b.len
  23. if alen == blen:
  24. if alen == 0: return true
  25. return equalMem(unsafeAddr(a[0]), unsafeAddr(b[0]), alen)
  26. proc hashString(s: string): int {.compilerproc.} =
  27. # the compiler needs exactly the same hash function!
  28. # this used to be used for efficient generation of string case statements
  29. var h : uint = 0
  30. for i in 0..len(s)-1:
  31. h = h + uint(s[i])
  32. h = h + h shl 10
  33. h = h xor (h shr 6)
  34. h = h + h shl 3
  35. h = h xor (h shr 11)
  36. h = h + h shl 15
  37. result = cast[int](h)
  38. proc addInt*(result: var string; x: int64) =
  39. ## Converts integer to its string representation and appends it to `result`.
  40. ##
  41. ## .. code-block:: Nim
  42. ## var
  43. ## a = "123"
  44. ## b = 45
  45. ## a.addInt(b) # a <- "12345"
  46. let base = result.len
  47. setLen(result, base + sizeof(x)*4)
  48. var i = 0
  49. var y = x
  50. while true:
  51. var d = y div 10
  52. result[base+i] = chr(abs(int(y - d*10)) + ord('0'))
  53. inc(i)
  54. y = d
  55. if y == 0: break
  56. if x < 0:
  57. result[base+i] = '-'
  58. inc(i)
  59. setLen(result, base+i)
  60. # mirror the string:
  61. for j in 0..i div 2 - 1:
  62. swap(result[base+j], result[base+i-j-1])
  63. proc add*(result: var string; x: int64) {.deprecated:
  64. "Deprecated since v0.20, use 'addInt'".} =
  65. addInt(result, x)
  66. proc nimIntToStr(x: int): string {.compilerRtl.} =
  67. result = newStringOfCap(sizeof(x)*4)
  68. result.addInt x
  69. proc addFloat*(result: var string; x: float) =
  70. ## Converts float to its string representation and appends it to `result`.
  71. ##
  72. ## .. code-block:: Nim
  73. ## var
  74. ## a = "123"
  75. ## b = 45.67
  76. ## a.addFloat(b) # a <- "12345.67"
  77. when nimvm:
  78. result.add $x
  79. else:
  80. var buf: array[0..64, char]
  81. when defined(nimNoArrayToCstringConversion):
  82. var n: int = c_sprintf(addr buf, "%.16g", x)
  83. else:
  84. var n: int = c_sprintf(buf, "%.16g", x)
  85. var hasDot = false
  86. for i in 0..n-1:
  87. if buf[i] == ',':
  88. buf[i] = '.'
  89. hasDot = true
  90. elif buf[i] in {'a'..'z', 'A'..'Z', '.'}:
  91. hasDot = true
  92. if not hasDot:
  93. buf[n] = '.'
  94. buf[n+1] = '0'
  95. buf[n+2] = '\0'
  96. # On Windows nice numbers like '1.#INF', '-1.#INF' or '1.#NAN'
  97. # of '-1.#IND' are produced.
  98. # We want to get rid of these here:
  99. if buf[n-1] in {'n', 'N', 'D', 'd'}:
  100. result.add "nan"
  101. elif buf[n-1] == 'F':
  102. if buf[0] == '-':
  103. result.add "-inf"
  104. else:
  105. result.add "inf"
  106. else:
  107. var i = 0
  108. while buf[i] != '\0':
  109. result.add buf[i]
  110. inc i
  111. proc add*(result: var string; x: float) {.deprecated:
  112. "Deprecated since v0.20, use 'addFloat'".} =
  113. addFloat(result, x)
  114. proc nimFloatToStr(f: float): string {.compilerproc.} =
  115. result = newStringOfCap(8)
  116. result.addFloat f
  117. proc c_strtod(buf: cstring, endptr: ptr cstring): float64 {.
  118. importc: "strtod", header: "<stdlib.h>", noSideEffect.}
  119. const
  120. IdentChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
  121. powtens = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  122. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  123. 1e20, 1e21, 1e22]
  124. proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
  125. start = 0): int {.compilerproc.} =
  126. # This routine attempt to parse float that can parsed quickly.
  127. # ie whose integer part can fit inside a 53bits integer.
  128. # their real exponent must also be <= 22. If the float doesn't follow
  129. # these restrictions, transform the float into this form:
  130. # INTEGER * 10 ^ exponent and leave the work to standard `strtod()`.
  131. # This avoid the problems of decimal character portability.
  132. # see: http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
  133. var
  134. i = start
  135. sign = 1.0
  136. kdigits, fdigits = 0
  137. exponent: int
  138. integer: uint64
  139. fracExponent = 0
  140. expSign = 1
  141. firstDigit = -1
  142. hasSign = false
  143. # Sign?
  144. if i < s.len and (s[i] == '+' or s[i] == '-'):
  145. hasSign = true
  146. if s[i] == '-':
  147. sign = -1.0
  148. inc(i)
  149. # NaN?
  150. if i+2 < s.len and (s[i] == 'N' or s[i] == 'n'):
  151. if s[i+1] == 'A' or s[i+1] == 'a':
  152. if s[i+2] == 'N' or s[i+2] == 'n':
  153. if i+3 >= s.len or s[i+3] notin IdentChars:
  154. number = NaN
  155. return i+3 - start
  156. return 0
  157. # Inf?
  158. if i+2 < s.len and (s[i] == 'I' or s[i] == 'i'):
  159. if s[i+1] == 'N' or s[i+1] == 'n':
  160. if s[i+2] == 'F' or s[i+2] == 'f':
  161. if i+3 >= s.len or s[i+3] notin IdentChars:
  162. number = Inf*sign
  163. return i+3 - start
  164. return 0
  165. if i < s.len and s[i] in {'0'..'9'}:
  166. firstDigit = (s[i].ord - '0'.ord)
  167. # Integer part?
  168. while i < s.len and s[i] in {'0'..'9'}:
  169. inc(kdigits)
  170. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  171. inc(i)
  172. while i < s.len and s[i] == '_': inc(i)
  173. # Fractional part?
  174. if i < s.len and s[i] == '.':
  175. inc(i)
  176. # if no integer part, Skip leading zeros
  177. if kdigits <= 0:
  178. while i < s.len and s[i] == '0':
  179. inc(fracExponent)
  180. inc(i)
  181. while i < s.len and s[i] == '_': inc(i)
  182. if firstDigit == -1 and i < s.len and s[i] in {'0'..'9'}:
  183. firstDigit = (s[i].ord - '0'.ord)
  184. # get fractional part
  185. while i < s.len and s[i] in {'0'..'9'}:
  186. inc(fdigits)
  187. inc(fracExponent)
  188. integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
  189. inc(i)
  190. while i < s.len and s[i] == '_': inc(i)
  191. # if has no digits: return error
  192. if kdigits + fdigits <= 0 and
  193. (i == start or # no char consumed (empty string).
  194. (i == start + 1 and hasSign)): # or only '+' or '-
  195. return 0
  196. if i+1 < s.len and s[i] in {'e', 'E'}:
  197. inc(i)
  198. if s[i] == '+' or s[i] == '-':
  199. if s[i] == '-':
  200. expSign = -1
  201. inc(i)
  202. if s[i] notin {'0'..'9'}:
  203. return 0
  204. while i < s.len and s[i] in {'0'..'9'}:
  205. exponent = exponent * 10 + (ord(s[i]) - ord('0'))
  206. inc(i)
  207. while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
  208. var realExponent = expSign*exponent - fracExponent
  209. let expNegative = realExponent < 0
  210. var absExponent = abs(realExponent)
  211. # if exponent greater than can be represented: +/- zero or infinity
  212. if absExponent > 999:
  213. if expNegative:
  214. number = 0.0*sign
  215. else:
  216. number = Inf*sign
  217. return i - start
  218. # if integer is representable in 53 bits: fast path
  219. # max fast path integer is 1<<53 - 1 or 8999999999999999 (16 digits)
  220. let digits = kdigits + fdigits
  221. if digits <= 15 or (digits <= 16 and firstDigit <= 8):
  222. # max float power of ten with set bits above the 53th bit is 10^22
  223. if absExponent <= 22:
  224. if expNegative:
  225. number = sign * integer.float / powtens[absExponent]
  226. else:
  227. number = sign * integer.float * powtens[absExponent]
  228. return i - start
  229. # if exponent is greater try to fit extra exponent above 22 by multiplying
  230. # integer part is there is space left.
  231. let slop = 15 - kdigits - fdigits
  232. if absExponent <= 22 + slop and not expNegative:
  233. number = sign * integer.float * powtens[slop] * powtens[absExponent-slop]
  234. return i - start
  235. # if failed: slow path with strtod.
  236. var t: array[500, char] # flaviu says: 325 is the longest reasonable literal
  237. var ti = 0
  238. let maxlen = t.high - "e+000".len # reserve enough space for exponent
  239. result = i - start
  240. i = start
  241. # re-parse without error checking, any error should be handled by the code above.
  242. if i < s.len and s[i] == '.': i.inc
  243. while i < s.len and s[i] in {'0'..'9','+','-'}:
  244. if ti < maxlen:
  245. t[ti] = s[i]; inc(ti)
  246. inc(i)
  247. while i < s.len and s[i] in {'.', '_'}: # skip underscore and decimal point
  248. inc(i)
  249. # insert exponent
  250. t[ti] = 'E'
  251. inc(ti)
  252. t[ti] = if expNegative: '-' else: '+'
  253. inc(ti, 4)
  254. # insert adjusted exponent
  255. t[ti-1] = ('0'.ord + absExponent mod 10).char
  256. absExponent = absExponent div 10
  257. t[ti-2] = ('0'.ord + absExponent mod 10).char
  258. absExponent = absExponent div 10
  259. t[ti-3] = ('0'.ord + absExponent mod 10).char
  260. when defined(nimNoArrayToCstringConversion):
  261. number = c_strtod(addr t, nil)
  262. else:
  263. number = c_strtod(t, nil)
  264. proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
  265. result = newStringOfCap(sizeof(x)*4)
  266. result.addInt x
  267. proc nimBoolToStr(x: bool): string {.compilerRtl.} =
  268. return if x: "true" else: "false"
  269. proc nimCharToStr(x: char): string {.compilerRtl.} =
  270. result = newString(1)
  271. result[0] = x
  272. proc `$`*(x: uint64): string {.noSideEffect, raises: [].} =
  273. ## The stringify operator for an unsigned integer argument. Returns `x`
  274. ## converted to a decimal string.
  275. if x == 0:
  276. result = "0"
  277. else:
  278. result = newString(60)
  279. var i = 0
  280. var n = x
  281. while n != 0:
  282. let nn = n div 10'u64
  283. result[i] = char(n - 10'u64 * nn + ord('0'))
  284. inc i
  285. n = nn
  286. result.setLen i
  287. let half = i div 2
  288. # Reverse
  289. for t in 0 .. half-1: swap(result[t], result[i-t-1])