dollars.nim 4.5 KB

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