rodutils.nim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Serialization utilities for the compiler.
  10. import strutils, math
  11. when defined(nimPreviewSlimSystem):
  12. import std/assertions
  13. # bcc on windows doesn't have C99 functions
  14. when defined(windows) and defined(bcc):
  15. {.emit: """#if defined(_MSC_VER) && _MSC_VER < 1900
  16. #include <stdarg.h>
  17. static int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
  18. int count = -1;
  19. if (size != 0) count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
  20. if (count == -1) count = _vscprintf(format, ap);
  21. return count;
  22. }
  23. int snprintf(char *outBuf, size_t size, const char *format, ...) {
  24. int count;
  25. va_list ap;
  26. va_start(ap, format);
  27. count = c99_vsnprintf(outBuf, size, format, ap);
  28. va_end(ap);
  29. return count;
  30. }
  31. #endif
  32. """.}
  33. proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "<stdio.h>", nodecl, varargs.}
  34. when not declared(signbit):
  35. proc c_signbit(x: SomeFloat): cint {.importc: "signbit", header: "<math.h>".}
  36. proc signbit*(x: SomeFloat): bool {.inline.} =
  37. result = c_signbit(x) != 0
  38. import std/formatfloat
  39. proc toStrMaxPrecision*(f: BiggestFloat | float32): string =
  40. const literalPostfix = when f is float32: "f" else: ""
  41. case classify(f)
  42. of fcNan:
  43. if signbit(f):
  44. result = "-NAN"
  45. else:
  46. result = "NAN"
  47. of fcNegZero:
  48. result = "-0.0" & literalPostfix
  49. of fcZero:
  50. result = "0.0" & literalPostfix
  51. of fcInf:
  52. result = "INF"
  53. of fcNegInf:
  54. result = "-INF"
  55. else:
  56. result.addFloatRoundtrip(f)
  57. result.add literalPostfix
  58. proc encodeStr*(s: string, result: var string) =
  59. for i in 0..<s.len:
  60. case s[i]
  61. of 'a'..'z', 'A'..'Z', '0'..'9', '_': result.add(s[i])
  62. else: result.add('\\' & toHex(ord(s[i]), 2))
  63. proc hexChar(c: char, xi: var int) =
  64. case c
  65. of '0'..'9': xi = (xi shl 4) or (ord(c) - ord('0'))
  66. of 'a'..'f': xi = (xi shl 4) or (ord(c) - ord('a') + 10)
  67. of 'A'..'F': xi = (xi shl 4) or (ord(c) - ord('A') + 10)
  68. else: discard
  69. proc decodeStr*(s: cstring, pos: var int): string =
  70. var i = pos
  71. result = ""
  72. while true:
  73. case s[i]
  74. of '\\':
  75. inc(i, 3)
  76. var xi = 0
  77. hexChar(s[i-2], xi)
  78. hexChar(s[i-1], xi)
  79. result.add(chr(xi))
  80. of 'a'..'z', 'A'..'Z', '0'..'9', '_':
  81. result.add(s[i])
  82. inc(i)
  83. else: break
  84. pos = i
  85. const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  86. {.push overflowChecks: off.}
  87. # since negative numbers require a leading '-' they use up 1 byte. Thus we
  88. # subtract/add `vintDelta` here to save space for small negative numbers
  89. # which are common in ROD files:
  90. const vintDelta = 5
  91. template encodeIntImpl(self) =
  92. var d: char
  93. var v = x
  94. var rem = v mod 190
  95. if rem < 0:
  96. result.add('-')
  97. v = - (v div 190)
  98. rem = - rem
  99. else:
  100. v = v div 190
  101. var idx = int(rem)
  102. if idx < 62: d = chars[idx]
  103. else: d = chr(idx - 62 + 128)
  104. if v != 0: self(v, result)
  105. result.add(d)
  106. proc encodeVBiggestIntAux(x: BiggestInt, result: var string) =
  107. ## encode a biggest int as a variable length base 190 int.
  108. encodeIntImpl(encodeVBiggestIntAux)
  109. proc encodeVBiggestInt*(x: BiggestInt, result: var string) =
  110. ## encode a biggest int as a variable length base 190 int.
  111. encodeVBiggestIntAux(x +% vintDelta, result)
  112. # encodeIntImpl(encodeVBiggestInt)
  113. proc encodeVIntAux(x: int, result: var string) =
  114. ## encode an int as a variable length base 190 int.
  115. encodeIntImpl(encodeVIntAux)
  116. proc encodeVInt*(x: int, result: var string) =
  117. ## encode an int as a variable length base 190 int.
  118. encodeVIntAux(x +% vintDelta, result)
  119. template decodeIntImpl() =
  120. var i = pos
  121. var sign = - 1
  122. assert(s[i] in {'a'..'z', 'A'..'Z', '0'..'9', '-', '\x80'..'\xFF'})
  123. if s[i] == '-':
  124. inc(i)
  125. sign = 1
  126. result = 0
  127. while true:
  128. case s[i]
  129. of '0'..'9': result = result * 190 - (ord(s[i]) - ord('0'))
  130. of 'a'..'z': result = result * 190 - (ord(s[i]) - ord('a') + 10)
  131. of 'A'..'Z': result = result * 190 - (ord(s[i]) - ord('A') + 36)
  132. of '\x80'..'\xFF': result = result * 190 - (ord(s[i]) - 128 + 62)
  133. else: break
  134. inc(i)
  135. result = result * sign -% vintDelta
  136. pos = i
  137. proc decodeVInt*(s: cstring, pos: var int): int =
  138. decodeIntImpl()
  139. proc decodeVBiggestInt*(s: cstring, pos: var int): BiggestInt =
  140. decodeIntImpl()
  141. {.pop.}
  142. iterator decodeVIntArray*(s: cstring): int =
  143. var i = 0
  144. while s[i] != '\0':
  145. yield decodeVInt(s, i)
  146. if s[i] == ' ': inc i
  147. iterator decodeStrArray*(s: cstring): string =
  148. var i = 0
  149. while s[i] != '\0':
  150. yield decodeStr(s, i)
  151. if s[i] == ' ': inc i