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