dollars.nim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. when defined(nimHasIsNamedTuple):
  44. proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
  45. else:
  46. # for bootstrap; remove after release 1.2
  47. proc isNamedTuple(T: typedesc): bool =
  48. # Taken from typetraits.
  49. when T isnot tuple: result = false
  50. else:
  51. var t: T
  52. for name, _ in t.fieldPairs:
  53. when name == "Field0":
  54. return compiles(t.Field0)
  55. else:
  56. return true
  57. return false
  58. proc `$`*[T: tuple|object](x: T): string =
  59. ## Generic ``$`` operator for tuples that is lifted from the components
  60. ## of `x`. Example:
  61. ##
  62. ## .. code-block:: Nim
  63. ## $(23, 45) == "(23, 45)"
  64. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  65. ## $() == "()"
  66. result = "("
  67. var firstElement = true
  68. const isNamed = T is object or isNamedTuple(T)
  69. when not isNamed:
  70. var count = 0
  71. for name, value in fieldPairs(x):
  72. if not firstElement: result.add(", ")
  73. when isNamed:
  74. result.add(name)
  75. result.add(": ")
  76. else:
  77. count.inc
  78. when compiles($value):
  79. when value isnot string and value isnot seq and compiles(value.isNil):
  80. if value.isNil: result.add "nil"
  81. else: result.addQuoted(value)
  82. else:
  83. result.addQuoted(value)
  84. firstElement = false
  85. else:
  86. result.add("...")
  87. firstElement = false
  88. when not isNamed:
  89. if count == 1:
  90. result.add(",") # $(1,) should print as the semantically legal (1,)
  91. result.add(")")
  92. proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
  93. result = prefix
  94. var firstElement = true
  95. for value in items(x):
  96. if firstElement:
  97. firstElement = false
  98. else:
  99. result.add(separator)
  100. when value isnot string and value isnot seq and compiles(value.isNil):
  101. # this branch should not be necessary
  102. if value.isNil:
  103. result.add "nil"
  104. else:
  105. result.addQuoted(value)
  106. else:
  107. result.addQuoted(value)
  108. result.add(suffix)
  109. proc `$`*[T](x: set[T]): string =
  110. ## Generic ``$`` operator for sets that is lifted from the components
  111. ## of `x`. Example:
  112. ##
  113. ## .. code-block:: Nim
  114. ## ${23, 45} == "{23, 45}"
  115. collectionToString(x, "{", ", ", "}")
  116. proc `$`*[T](x: seq[T]): string =
  117. ## Generic ``$`` operator for seqs that is lifted from the components
  118. ## of `x`. Example:
  119. ##
  120. ## .. code-block:: Nim
  121. ## $(@[23, 45]) == "@[23, 45]"
  122. collectionToString(x, "@[", ", ", "]")
  123. proc `$`*[T, U](x: HSlice[T, U]): string =
  124. ## Generic ``$`` operator for slices that is lifted from the components
  125. ## of `x`. Example:
  126. ##
  127. ## .. code-block:: Nim
  128. ## $(1 .. 5) == "1 .. 5"
  129. result = $x.a
  130. result.add(" .. ")
  131. result.add($x.b)
  132. when not defined(nimNoArrayToString):
  133. proc `$`*[T, IDX](x: array[IDX, T]): string =
  134. ## Generic ``$`` operator for arrays that is lifted from the components.
  135. collectionToString(x, "[", ", ", "]")
  136. proc `$`*[T](x: openArray[T]): string =
  137. ## Generic ``$`` operator for openarrays that is lifted from the components
  138. ## of `x`. Example:
  139. ##
  140. ## .. code-block:: Nim
  141. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  142. collectionToString(x, "[", ", ", "]")