rodutils.nim 4.6 KB

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