dollars.nim 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ## `$` is Nim's general way of spelling `toString`:idx:.
  2. runnableExamples:
  3. assert $0.1 == "0.1"
  4. assert $(-2*3) == "-6"
  5. import std/private/[digitsutils, miscdollars]
  6. when not defined(nimPreviewSlimSystem):
  7. import std/formatfloat
  8. export addFloat
  9. func `$`*(x: float | float32): string =
  10. ## Outplace version of `addFloat`.
  11. result = ""
  12. result.addFloat(x)
  13. template addIntAlias(T: typedesc) =
  14. proc `$`*(x: T): string {.raises: [].} =
  15. ## Outplace version of `addInt`.
  16. result = ""
  17. result.addInt(x)
  18. # need to declare for bit types as well to not clash with converters:
  19. addIntAlias int
  20. addIntAlias int8
  21. addIntAlias int16
  22. addIntAlias int32
  23. addIntAlias int64
  24. addIntAlias uint
  25. addIntAlias uint8
  26. addIntAlias uint16
  27. addIntAlias uint32
  28. addIntAlias uint64
  29. # same as old `ctfeWhitelist` behavior, whether or not this is a good idea.
  30. template gen(T) =
  31. # xxx simplify this by supporting this in compiler: int{lit} | uint64{lit} | int64{lit}
  32. func `$`*(x: T{lit}): string {.compileTime.} =
  33. result = ""
  34. result.addInt(x)
  35. gen(int)
  36. gen(uint64)
  37. gen(int64)
  38. proc `$`*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
  39. ## The stringify operator for a boolean argument. Returns `x`
  40. ## converted to the string "false" or "true".
  41. proc `$`*(x: char): string {.magic: "CharToStr", noSideEffect.}
  42. ## The stringify operator for a character argument. Returns `x`
  43. ## converted to a string.
  44. ## ```Nim
  45. ## assert $'c' == "c"
  46. ## ```
  47. proc `$`*(x: cstring): string {.magic: "CStrToStr", noSideEffect.}
  48. ## The stringify operator for a CString argument. Returns `x`
  49. ## converted to a string.
  50. proc `$`*(x: string): string {.magic: "StrToStr", noSideEffect.}
  51. ## The stringify operator for a string argument. Returns `x`
  52. ## as it is. This operator is useful for generic code, so
  53. ## that `$expr` also works if `expr` is already a string.
  54. proc `$`*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
  55. ## The stringify operator for an enumeration argument. This works for
  56. ## any enumeration type thanks to compiler magic.
  57. ##
  58. ## If a `$` operator for a concrete enumeration is provided, this is
  59. ## used instead. (In other words: *Overwriting* is possible.)
  60. proc `$`*(t: typedesc): string {.magic: "TypeTrait".}
  61. ## Returns the name of the given type.
  62. ##
  63. ## For more procedures dealing with `typedesc`, see
  64. ## `typetraits module <typetraits.html>`_.
  65. ##
  66. ## ```Nim
  67. ## doAssert $(typeof(42)) == "int"
  68. ## doAssert $(typeof("Foo")) == "string"
  69. ## static: doAssert $(typeof(@['A', 'B'])) == "seq[char]"
  70. ## ```
  71. proc `$`*[T: tuple](x: T): string =
  72. ## Generic `$` operator for tuples that is lifted from the components
  73. ## of `x`. Example:
  74. ## ```Nim
  75. ## $(23, 45) == "(23, 45)"
  76. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  77. ## $() == "()"
  78. ## ```
  79. tupleObjectDollar(result, x)
  80. when not defined(nimPreviewSlimSystem):
  81. import std/objectdollar
  82. export objectdollar
  83. proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
  84. result = prefix
  85. var firstElement = true
  86. for value in items(x):
  87. if firstElement:
  88. firstElement = false
  89. else:
  90. result.add(separator)
  91. when value isnot string and value isnot seq and compiles(value.isNil):
  92. # this branch should not be necessary
  93. if value.isNil:
  94. result.add "nil"
  95. else:
  96. result.addQuoted(value)
  97. else:
  98. result.addQuoted(value)
  99. result.add(suffix)
  100. proc `$`*[T](x: set[T]): string =
  101. ## Generic `$` operator for sets that is lifted from the components
  102. ## of `x`. Example:
  103. ## ```Nim
  104. ## ${23, 45} == "{23, 45}"
  105. ## ```
  106. collectionToString(x, "{", ", ", "}")
  107. proc `$`*[T](x: seq[T]): string =
  108. ## Generic `$` operator for seqs that is lifted from the components
  109. ## of `x`. Example:
  110. ## ```Nim
  111. ## $(@[23, 45]) == "@[23, 45]"
  112. ## ```
  113. collectionToString(x, "@[", ", ", "]")
  114. proc `$`*[T, U](x: HSlice[T, U]): string =
  115. ## Generic `$` operator for slices that is lifted from the components
  116. ## of `x`. Example:
  117. ## ```Nim
  118. ## $(1 .. 5) == "1 .. 5"
  119. ## ```
  120. result = $x.a
  121. result.add(" .. ")
  122. result.add($x.b)
  123. when not defined(nimNoArrayToString):
  124. proc `$`*[T, IDX](x: array[IDX, T]): string =
  125. ## Generic `$` operator for arrays that is lifted from the components.
  126. collectionToString(x, "[", ", ", "]")
  127. proc `$`*[T](x: openArray[T]): string =
  128. ## Generic `$` operator for openarrays that is lifted from the components
  129. ## of `x`. Example:
  130. ## ```Nim
  131. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  132. ## ```
  133. collectionToString(x, "[", ", ", "]")