repr_v2.nim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
  2. ## imported from typetraits
  3. proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
  4. ## imported from typetraits
  5. proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.}
  6. proc repr*(x: int): string {.magic: "IntToStr", noSideEffect.}
  7. ## repr for an integer argument. Returns `x`
  8. ## converted to a decimal string.
  9. proc repr*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
  10. ## repr for an integer argument. Returns `x`
  11. ## converted to a decimal string.
  12. proc repr*(x: float): string {.magic: "FloatToStr", noSideEffect.}
  13. ## repr for a float argument. Returns `x`
  14. ## converted to a decimal string.
  15. proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
  16. ## repr for a boolean argument. Returns `x`
  17. ## converted to the string "false" or "true".
  18. proc repr*(x: char): string {.magic: "CharToStr", noSideEffect.}
  19. ## repr for a character argument. Returns `x`
  20. ## converted to a string.
  21. ##
  22. ## .. code-block:: Nim
  23. ## assert $'c' == "c"
  24. proc repr*(x: cstring): string {.magic: "CStrToStr", noSideEffect.}
  25. ## repr for a CString argument. Returns `x`
  26. ## converted to a string.
  27. proc repr*(x: string): string {.magic: "StrToStr", noSideEffect.}
  28. ## repr for a string argument. Returns `x`
  29. ## as it is. This operator is useful for generic code, so
  30. ## that ``$expr`` also works if ``expr`` is already a string.
  31. proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
  32. ## repr for an enumeration argument. This works for
  33. ## any enumeration type thanks to compiler magic.
  34. ##
  35. ## If a `repr` operator for a concrete enumeration is provided, this is
  36. ## used instead. (In other words: *Overwriting* is possible.)
  37. proc repr*(p: pointer): string =
  38. ## repr of pointer as its hexadecimal value
  39. if p == nil:
  40. result = "nil"
  41. else:
  42. when nimvm:
  43. result = "ptr"
  44. else:
  45. const HexChars = "0123456789ABCDEF"
  46. const len = sizeof(pointer) * 2
  47. var n = cast[uint](p)
  48. result = newString(len)
  49. for j in countdown(len-1, 0):
  50. result[j] = HexChars[n and 0xF]
  51. n = n shr 4
  52. template repr*(x: distinct): string =
  53. repr(distinctBase(typeof(x))(x))
  54. template repr*(t: typedesc): string = $t
  55. proc reprObject[T: tuple|object](res: var string, x: T) =
  56. res.add '('
  57. var firstElement = true
  58. const isNamed = T is object or isNamedTuple(T)
  59. when not isNamed:
  60. var count = 0
  61. for name, value in fieldPairs(x):
  62. if not firstElement: res.add(", ")
  63. when isNamed:
  64. res.add(name)
  65. res.add(": ")
  66. else:
  67. count.inc
  68. res.add repr(value)
  69. firstElement = false
  70. when not isNamed:
  71. if count == 1:
  72. res.add(',') # $(1,) should print as the semantically legal (1,)
  73. res.add(')')
  74. proc repr*[T: tuple|object](x: T): string =
  75. ## Generic `repr` operator for tuples that is lifted from the components
  76. ## of `x`. Example:
  77. ##
  78. ## .. code-block:: Nim
  79. ## $(23, 45) == "(23, 45)"
  80. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  81. ## $() == "()"
  82. when T is object:
  83. result = $typeof(x)
  84. reprObject(result, x)
  85. proc repr*[T](x: ref T | ptr T): string =
  86. if isNil(x): return "nil"
  87. result = $typeof(x)
  88. reprObject(result, x[])
  89. proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string =
  90. result = prefix
  91. var firstElement = true
  92. for value in items(x):
  93. if firstElement:
  94. firstElement = false
  95. else:
  96. result.add(separator)
  97. result.add repr(value)
  98. result.add(suffix)
  99. proc repr*[T](x: set[T]): string =
  100. ## Generic `repr` operator for sets that is lifted from the components
  101. ## of `x`. Example:
  102. ##
  103. ## .. code-block:: Nim
  104. ## ${23, 45} == "{23, 45}"
  105. collectionToRepr(x, "{", ", ", "}")
  106. proc repr*[T](x: seq[T]): string =
  107. ## Generic `repr` operator for seqs that is lifted from the components
  108. ## of `x`. Example:
  109. ##
  110. ## .. code-block:: Nim
  111. ## $(@[23, 45]) == "@[23, 45]"
  112. collectionToRepr(x, "@[", ", ", "]")
  113. proc repr*[T, U](x: HSlice[T, U]): string =
  114. ## Generic `repr` operator for slices that is lifted from the components
  115. ## of `x`. Example:
  116. ##
  117. ## .. code-block:: Nim
  118. ## $(1 .. 5) == "1 .. 5"
  119. result = repr(x.a)
  120. result.add(" .. ")
  121. result.add(repr(x.b))
  122. proc repr*[T, IDX](x: array[IDX, T]): string =
  123. ## Generic `repr` operator for arrays that is lifted from the components.
  124. collectionToRepr(x, "[", ", ", "]")
  125. proc repr*[T](x: openArray[T]): string =
  126. ## Generic `repr` operator for openarrays that is lifted from the components
  127. ## of `x`. Example:
  128. ##
  129. ## .. code-block:: Nim
  130. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  131. collectionToRepr(x, "[", ", ", "]")