rodutils.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
  33. case classify(f)
  34. of fcNan:
  35. result = "NAN"
  36. of fcNegZero:
  37. result = "-0.0" & literalPostfix
  38. of fcZero:
  39. result = "0.0" & literalPostfix
  40. of fcInf:
  41. result = "INF"
  42. of fcNegInf:
  43. result = "-INF"
  44. else:
  45. result = newString(81)
  46. let n = c_snprintf(result.cstring, result.len.uint, "%#.16e%s", f, literalPostfix.cstring)
  47. setLen(result, n)
  48. proc encodeStr*(s: string, result: var string) =
  49. for i in 0..<s.len:
  50. case s[i]
  51. of 'a'..'z', 'A'..'Z', '0'..'9', '_': result.add(s[i])
  52. else: result.add('\\' & toHex(ord(s[i]), 2))
  53. proc hexChar(c: char, xi: var int) =
  54. case c
  55. of '0'..'9': xi = (xi shl 4) or (ord(c) - ord('0'))
  56. of 'a'..'f': xi = (xi shl 4) or (ord(c) - ord('a') + 10)
  57. of 'A'..'F': xi = (xi shl 4) or (ord(c) - ord('A') + 10)
  58. else: discard
  59. proc decodeStr*(s: cstring, pos: var int): string =
  60. var i = pos
  61. result = ""
  62. while true:
  63. case s[i]
  64. of '\\':
  65. inc(i, 3)
  66. var xi = 0
  67. hexChar(s[i-2], xi)
  68. hexChar(s[i-1], xi)
  69. result.add(chr(xi))
  70. of 'a'..'z', 'A'..'Z', '0'..'9', '_':
  71. result.add(s[i])
  72. inc(i)
  73. else: break
  74. pos = i
  75. const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  76. {.push overflowChecks: off.}
  77. # since negative numbers require a leading '-' they use up 1 byte. Thus we
  78. # subtract/add `vintDelta` here to save space for small negative numbers
  79. # which are common in ROD files:
  80. const vintDelta = 5
  81. template encodeIntImpl(self) =
  82. var d: char
  83. var v = x
  84. var rem = v mod 190
  85. if rem < 0:
  86. result.add('-')
  87. v = - (v div 190)
  88. rem = - rem
  89. else:
  90. v = v div 190
  91. var idx = int(rem)
  92. if idx < 62: d = chars[idx]
  93. else: d = chr(idx - 62 + 128)
  94. if v != 0: self(v, result)
  95. result.add(d)
  96. proc encodeVBiggestIntAux(x: BiggestInt, result: var string) =
  97. ## encode a biggest int as a variable length base 190 int.
  98. encodeIntImpl(encodeVBiggestIntAux)
  99. proc encodeVBiggestInt*(x: BiggestInt, result: var string) =
  100. ## encode a biggest int as a variable length base 190 int.
  101. encodeVBiggestIntAux(x +% vintDelta, result)
  102. # encodeIntImpl(encodeVBiggestInt)
  103. proc encodeVIntAux(x: int, result: var string) =
  104. ## encode an int as a variable length base 190 int.
  105. encodeIntImpl(encodeVIntAux)
  106. proc encodeVInt*(x: int, result: var string) =
  107. ## encode an int as a variable length base 190 int.
  108. encodeVIntAux(x +% vintDelta, result)
  109. template decodeIntImpl() =
  110. var i = pos
  111. var sign = - 1
  112. assert(s[i] in {'a'..'z', 'A'..'Z', '0'..'9', '-', '\x80'..'\xFF'})
  113. if s[i] == '-':
  114. inc(i)
  115. sign = 1
  116. result = 0
  117. while true:
  118. case s[i]
  119. of '0'..'9': result = result * 190 - (ord(s[i]) - ord('0'))
  120. of 'a'..'z': result = result * 190 - (ord(s[i]) - ord('a') + 10)
  121. of 'A'..'Z': result = result * 190 - (ord(s[i]) - ord('A') + 36)
  122. of '\x80'..'\xFF': result = result * 190 - (ord(s[i]) - 128 + 62)
  123. else: break
  124. inc(i)
  125. result = result * sign -% vintDelta
  126. pos = i
  127. proc decodeVInt*(s: cstring, pos: var int): int =
  128. decodeIntImpl()
  129. proc decodeVBiggestInt*(s: cstring, pos: var int): BiggestInt =
  130. decodeIntImpl()
  131. {.pop.}
  132. iterator decodeVIntArray*(s: cstring): int =
  133. var i = 0
  134. while s[i] != '\0':
  135. yield decodeVInt(s, i)
  136. if s[i] == ' ': inc i
  137. iterator decodeStrArray*(s: cstring): string =
  138. var i = 0
  139. while s[i] != '\0':
  140. yield decodeStr(s, i)
  141. if s[i] == ' ': inc i