dollars.nim 5.5 KB

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