dollars.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ##
  35. ## .. code-block:: Nim
  36. ## assert $'c' == "c"
  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. ## .. code-block:: Nim
  57. ## doAssert $(typeof(42)) == "int"
  58. ## doAssert $(typeof("Foo")) == "string"
  59. ## static: doAssert $(typeof(@['A', 'B'])) == "seq[char]"
  60. proc `$`*[T: tuple](x: T): string =
  61. ## Generic `$` operator for tuples that is lifted from the components
  62. ## of `x`. Example:
  63. ##
  64. ## .. code-block:: Nim
  65. ## $(23, 45) == "(23, 45)"
  66. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  67. ## $() == "()"
  68. tupleObjectDollar(result, x)
  69. when not defined(nimPreviewSlimSystem):
  70. import std/objectdollar
  71. export objectdollar
  72. proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
  73. result = prefix
  74. var firstElement = true
  75. for value in items(x):
  76. if firstElement:
  77. firstElement = false
  78. else:
  79. result.add(separator)
  80. when value isnot string and value isnot seq and compiles(value.isNil):
  81. # this branch should not be necessary
  82. if value.isNil:
  83. result.add "nil"
  84. else:
  85. result.addQuoted(value)
  86. else:
  87. result.addQuoted(value)
  88. result.add(suffix)
  89. proc `$`*[T](x: set[T]): string =
  90. ## Generic `$` operator for sets that is lifted from the components
  91. ## of `x`. Example:
  92. ##
  93. ## .. code-block:: Nim
  94. ## ${23, 45} == "{23, 45}"
  95. collectionToString(x, "{", ", ", "}")
  96. proc `$`*[T](x: seq[T]): string =
  97. ## Generic `$` operator for seqs that is lifted from the components
  98. ## of `x`. Example:
  99. ##
  100. ## .. code-block:: Nim
  101. ## $(@[23, 45]) == "@[23, 45]"
  102. collectionToString(x, "@[", ", ", "]")
  103. proc `$`*[T, U](x: HSlice[T, U]): string =
  104. ## Generic `$` operator for slices that is lifted from the components
  105. ## of `x`. Example:
  106. ##
  107. ## .. code-block:: Nim
  108. ## $(1 .. 5) == "1 .. 5"
  109. result = $x.a
  110. result.add(" .. ")
  111. result.add($x.b)
  112. when not defined(nimNoArrayToString):
  113. proc `$`*[T, IDX](x: array[IDX, T]): string =
  114. ## Generic `$` operator for arrays that is lifted from the components.
  115. collectionToString(x, "[", ", ", "]")
  116. proc `$`*[T](x: openArray[T]): string =
  117. ## Generic `$` operator for openarrays that is lifted from the components
  118. ## of `x`. Example:
  119. ##
  120. ## .. code-block:: Nim
  121. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  122. collectionToString(x, "[", ", ", "]")