dollars.nim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.}
  2. ## The stringify operator for an integer argument. Returns `x`
  3. ## converted to a decimal string. ``$`` is Nim's general way of
  4. ## spelling `toString`:idx:.
  5. proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
  6. ## The stringify operator for an integer argument. Returns `x`
  7. ## converted to a decimal string.
  8. proc `$`*(x: float): string {.magic: "FloatToStr", noSideEffect.}
  9. ## The stringify operator for a float argument. Returns `x`
  10. ## converted to a decimal string.
  11. proc `$`*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
  12. ## The stringify operator for a boolean argument. Returns `x`
  13. ## converted to the string "false" or "true".
  14. proc `$`*(x: char): string {.magic: "CharToStr", noSideEffect.}
  15. ## The stringify operator for a character argument. Returns `x`
  16. ## converted to a string.
  17. ##
  18. ## .. code-block:: Nim
  19. ## assert $'c' == "c"
  20. proc `$`*(x: cstring): string {.magic: "CStrToStr", noSideEffect.}
  21. ## The stringify operator for a CString argument. Returns `x`
  22. ## converted to a string.
  23. proc `$`*(x: string): string {.magic: "StrToStr", noSideEffect.}
  24. ## The stringify operator for a string argument. Returns `x`
  25. ## as it is. This operator is useful for generic code, so
  26. ## that ``$expr`` also works if ``expr`` is already a string.
  27. proc `$`*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
  28. ## The stringify operator for an enumeration argument. This works for
  29. ## any enumeration type thanks to compiler magic.
  30. ##
  31. ## If a ``$`` operator for a concrete enumeration is provided, this is
  32. ## used instead. (In other words: *Overwriting* is possible.)
  33. proc `$`*(t: typedesc): string {.magic: "TypeTrait".}
  34. ## Returns the name of the given type.
  35. ##
  36. ## For more procedures dealing with ``typedesc``, see
  37. ## `typetraits module <typetraits.html>`_.
  38. ##
  39. ## .. code-block:: Nim
  40. ## doAssert $(type(42)) == "int"
  41. ## doAssert $(type("Foo")) == "string"
  42. ## static: doAssert $(type(@['A', 'B'])) == "seq[char]"
  43. proc isNamedTuple(T: typedesc): bool =
  44. # Taken from typetraits.
  45. when T isnot tuple: result = false
  46. else:
  47. var t: T
  48. for name, _ in t.fieldPairs:
  49. when name == "Field0":
  50. return compiles(t.Field0)
  51. else:
  52. return true
  53. return false
  54. proc `$`*[T: tuple|object](x: T): string =
  55. ## Generic ``$`` operator for tuples that is lifted from the components
  56. ## of `x`. Example:
  57. ##
  58. ## .. code-block:: Nim
  59. ## $(23, 45) == "(23, 45)"
  60. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  61. ## $() == "()"
  62. result = "("
  63. var firstElement = true
  64. const isNamed = T is object or isNamedTuple(T)
  65. when not isNamed:
  66. var count = 0
  67. for name, value in fieldPairs(x):
  68. if not firstElement: result.add(", ")
  69. when isNamed:
  70. result.add(name)
  71. result.add(": ")
  72. else:
  73. count.inc
  74. when compiles($value):
  75. when value isnot string and value isnot seq and compiles(value.isNil):
  76. if value.isNil: result.add "nil"
  77. else: result.addQuoted(value)
  78. else:
  79. result.addQuoted(value)
  80. firstElement = false
  81. else:
  82. result.add("...")
  83. firstElement = false
  84. when not isNamed:
  85. if count == 1:
  86. result.add(",") # $(1,) should print as the semantically legal (1,)
  87. result.add(")")
  88. proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
  89. result = prefix
  90. var firstElement = true
  91. for value in items(x):
  92. if firstElement:
  93. firstElement = false
  94. else:
  95. result.add(separator)
  96. when value isnot string and value isnot seq and compiles(value.isNil):
  97. # this branch should not be necessary
  98. if value.isNil:
  99. result.add "nil"
  100. else:
  101. result.addQuoted(value)
  102. else:
  103. result.addQuoted(value)
  104. result.add(suffix)
  105. proc `$`*[T](x: set[T]): string =
  106. ## Generic ``$`` operator for sets that is lifted from the components
  107. ## of `x`. Example:
  108. ##
  109. ## .. code-block:: Nim
  110. ## ${23, 45} == "{23, 45}"
  111. collectionToString(x, "{", ", ", "}")
  112. proc `$`*[T](x: seq[T]): string =
  113. ## Generic ``$`` operator for seqs that is lifted from the components
  114. ## of `x`. Example:
  115. ##
  116. ## .. code-block:: Nim
  117. ## $(@[23, 45]) == "@[23, 45]"
  118. collectionToString(x, "@[", ", ", "]")
  119. proc `$`*[T, U](x: HSlice[T, U]): string =
  120. ## Generic ``$`` operator for slices that is lifted from the components
  121. ## of `x`. Example:
  122. ##
  123. ## .. code-block:: Nim
  124. ## $(1 .. 5) == "1 .. 5"
  125. result = $x.a
  126. result.add(" .. ")
  127. result.add($x.b)
  128. when not defined(nimNoArrayToString):
  129. proc `$`*[T, IDX](x: array[IDX, T]): string =
  130. ## Generic ``$`` operator for arrays that is lifted from the components.
  131. collectionToString(x, "[", ", ", "]")
  132. proc `$`*[T](x: openarray[T]): string =
  133. ## Generic ``$`` operator for openarrays that is lifted from the components
  134. ## of `x`. Example:
  135. ##
  136. ## .. code-block:: Nim
  137. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  138. collectionToString(x, "[", ", ", "]")