digitsutils.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const
  2. trailingZeros100: array[100, int8] = [2'i8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
  4. 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
  5. 0, 0, 0, 0, 0, 0]
  6. digits100: array[200, char] = ['0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5',
  7. '0', '6', '0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4',
  8. '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0', '2', '1', '2', '2', '2', '3',
  9. '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', '3', '0', '3', '1', '3', '2',
  10. '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1',
  11. '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', '5', '0',
  12. '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
  13. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8',
  14. '6', '9', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6', '7', '7',
  15. '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', '5', '8', '6',
  16. '8', '7', '8', '8', '8', '9', '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5',
  17. '9', '6', '9', '7', '9', '8', '9', '9']
  18. # Inspired by https://engineering.fb.com/2013/03/15/developer-tools/three-optimization-tips-for-c
  19. # Generates:
  20. # ```nim
  21. # var res = ""
  22. # for i in 0 .. 99:
  23. # if i < 10:
  24. # res.add "0" & $i
  25. # else:
  26. # res.add $i
  27. # doAssert res == digits100
  28. # ```
  29. proc utoa2Digits*(buf: var openArray[char]; pos: int; digits: uint32) {.inline.} =
  30. buf[pos] = digits100[2 * digits]
  31. buf[pos+1] = digits100[2 * digits + 1]
  32. #copyMem(buf, unsafeAddr(digits100[2 * digits]), 2 * sizeof((char)))
  33. proc trailingZeros2Digits*(digits: uint32): int {.inline.} =
  34. trailingZeros100[digits]
  35. when defined(js):
  36. proc numToString(a: SomeInteger): cstring {.importjs: "((#) + \"\")".}
  37. func addChars[T](result: var string, x: T, start: int, n: int) {.inline.} =
  38. let old = result.len
  39. result.setLen old + n
  40. template impl =
  41. for i in 0..<n: result[old + i] = x[start + i]
  42. when nimvm: impl
  43. else:
  44. when defined(js) or defined(nimscript): impl
  45. else:
  46. {.noSideEffect.}:
  47. copyMem result[old].addr, x[start].unsafeAddr, n
  48. func addChars[T](result: var string, x: T) {.inline.} =
  49. addChars(result, x, 0, x.len)
  50. func addIntImpl(result: var string, x: uint64) {.inline.} =
  51. var tmp {.noinit.}: array[24, char]
  52. var num = x
  53. var next = tmp.len - 1
  54. const nbatch = 100
  55. while num >= nbatch:
  56. let originNum = num
  57. num = num div nbatch
  58. let index = int16((originNum - num * nbatch) shl 1)
  59. tmp[next] = digits100[index + 1]
  60. tmp[next - 1] = digits100[index]
  61. dec(next, 2)
  62. # process last 1-2 digits
  63. if num < 10:
  64. tmp[next] = chr(ord('0') + num.uint8)
  65. else:
  66. let index = num * 2
  67. tmp[next] = digits100[index + 1]
  68. tmp[next - 1] = digits100[index]
  69. dec next
  70. addChars(result, tmp, next, tmp.len - next)
  71. when not defined(nimHasEnforceNoRaises):
  72. {.pragma: enforceNoRaises.}
  73. func addInt*(result: var string, x: uint64) {.enforceNoRaises.} =
  74. when nimvm: addIntImpl(result, x)
  75. else:
  76. when not defined(js): addIntImpl(result, x)
  77. else:
  78. addChars(result, numToString(x))
  79. proc addInt*(result: var string; x: int64) {.enforceNoRaises.} =
  80. ## Converts integer to its string representation and appends it to `result`.
  81. runnableExamples:
  82. var s = "foo"
  83. s.addInt(45)
  84. assert s == "foo45"
  85. template impl =
  86. var num: uint64
  87. if x < 0:
  88. if x == low(int64):
  89. num = cast[uint64](x)
  90. else:
  91. num = uint64(-x)
  92. result.add '-'
  93. else:
  94. num = uint64(x)
  95. addInt(result, num)
  96. when nimvm: impl()
  97. else:
  98. when defined(js):
  99. addChars(result, numToString(x))
  100. else: impl()
  101. proc addInt*(result: var string; x: int) {.inline, enforceNoRaises.} =
  102. addInt(result, int64(x))