dollars.nim 4.4 KB

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