diff options
author | Miran <narimiran@disroot.org> | 2019-03-13 15:59:09 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-03-13 15:59:09 +0100 |
commit | 84d3f3d448aa59c86084c94b9a71435529ba48cd (patch) | |
tree | ccf33b235c8ad5ff4fdb8aaa57bc7f0776f896ab /lib/system | |
parent | b270917de1d5fbda2f5d4127e1513793a88391d3 (diff) | |
download | Nim-84d3f3d448aa59c86084c94b9a71435529ba48cd.tar.gz |
move system.dollars in a separate file (#10829)
Diffstat (limited to 'lib/system')
-rw-r--r-- | lib/system/assertions.nim | 16 | ||||
-rw-r--r-- | lib/system/dollars.nim | 158 | ||||
-rw-r--r-- | lib/system/helpers.nim | 20 | ||||
-rw-r--r-- | lib/system/iterators.nim | 2 | ||||
-rw-r--r-- | lib/system/strmantle.nim | 4 |
5 files changed, 176 insertions, 24 deletions
diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim index e0e901a97..090f4096c 100644 --- a/lib/system/assertions.nim +++ b/lib/system/assertions.nim @@ -1,8 +1,20 @@ -include "system/helpers" - when not declared(sysFatal): include "system/fatal" +# --------------------------------------------------------------------------- +# helpers + +type InstantiationInfo = tuple[filename: string, line: int, column: int] + +proc `$`(x: int): string {.magic: "IntToStr", noSideEffect.} + +proc `$`(info: InstantiationInfo): string = + # The +1 is needed here + # instead of overriding `$` (and changing its meaning), consider explicit name. + info.fileName & "(" & $info.line & ", " & $(info.column+1) & ")" + +# --------------------------------------------------------------------------- + proc raiseAssert*(msg: string) {.noinline, noReturn.} = sysFatal(AssertionError, msg) diff --git a/lib/system/dollars.nim b/lib/system/dollars.nim new file mode 100644 index 000000000..6fa57ca03 --- /dev/null +++ b/lib/system/dollars.nim @@ -0,0 +1,158 @@ +proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.} + ## The stringify operator for an integer argument. Returns `x` + ## converted to a decimal string. ``$`` is Nim's general way of + ## spelling `toString`:idx:. + +proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.} + ## The stringify operator for an integer argument. Returns `x` + ## converted to a decimal string. + +proc `$`*(x: float): string {.magic: "FloatToStr", noSideEffect.} + ## The stringify operator for a float argument. Returns `x` + ## converted to a decimal string. + +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. + ## + ## .. code-block:: 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>`_. + ## + ## .. code-block:: Nim + ## doAssert $(type(42)) == "int" + ## doAssert $(type("Foo")) == "string" + ## static: doAssert $(type(@['A', 'B'])) == "seq[char]" + + +proc isNamedTuple(T: typedesc): bool = + # Taken from typetraits. + when T isnot tuple: result = false + else: + var t: T + for name, _ in t.fieldPairs: + when name == "Field0": + return compiles(t.Field0) + else: + return true + return false + +proc `$`*[T: tuple|object](x: T): string = + ## Generic ``$`` operator for tuples that is lifted from the components + ## of `x`. Example: + ## + ## .. code-block:: Nim + ## $(23, 45) == "(23, 45)" + ## $(a: 23, b: 45) == "(a: 23, b: 45)" + ## $() == "()" + result = "(" + var firstElement = true + const isNamed = T is object or isNamedTuple(T) + when not isNamed: + var count = 0 + for name, value in fieldPairs(x): + if not firstElement: result.add(", ") + when isNamed: + result.add(name) + result.add(": ") + else: + count.inc + when compiles($value): + when value isnot string and value isnot seq and compiles(value.isNil): + if value.isNil: result.add "nil" + else: result.addQuoted(value) + else: + result.addQuoted(value) + firstElement = false + else: + result.add("...") + firstElement = false + when not isNamed: + if count == 1: + result.add(",") # $(1,) should print as the semantically legal (1,) + result.add(")") + + +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: + ## + ## .. code-block:: 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: + ## + ## .. code-block:: 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: + ## + ## .. code-block:: 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: + ## + ## .. code-block:: Nim + ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]" + collectionToString(x, "[", ", ", "]") diff --git a/lib/system/helpers.nim b/lib/system/helpers.nim deleted file mode 100644 index fda38156b..000000000 --- a/lib/system/helpers.nim +++ /dev/null @@ -1,20 +0,0 @@ -# TODO: remove this file. It causes "declared but not used" warnings everywhere. - -type InstantiationInfo = tuple[filename: string, line: int, column: int] - -proc `$`(info: InstantiationInfo): string = - info.fileName & "(" & $info.line & ", " & $(info.column+1) & ")" - -proc isNamedTuple(T: type): bool = - ## return true for named tuples, false for any other type. - when T isnot tuple: result = false - else: - var t: T - for name, _ in t.fieldPairs: - when name == "Field0": - return compiles(t.Field0) - else: - return true - # empty tuple should be un-named, - # see https://github.com/nim-lang/Nim/issues/8861#issue-356631191 - return false diff --git a/lib/system/iterators.nim b/lib/system/iterators.nim index 1054d1e0c..0bdd7a2e2 100644 --- a/lib/system/iterators.nim +++ b/lib/system/iterators.nim @@ -199,7 +199,7 @@ iterator fields*[S:tuple|object, T:tuple|object](x: S, y: T): tuple[a,b: untyped magic: "Fields", noSideEffect.} ## Iterates over every field of `x` and `y`. ## - ## **Warning**: This is really transforms the 'for' and unrolls the loop. + ## **Warning**: This really transforms the 'for' and unrolls the loop. ## The current implementation also has a bug that affects symbol binding ## in the loop body. iterator fieldPairs*[T: tuple|object](x: T): RootObj {. diff --git a/lib/system/strmantle.nim b/lib/system/strmantle.nim index 548dacd22..727c08da3 100644 --- a/lib/system/strmantle.nim +++ b/lib/system/strmantle.nim @@ -293,7 +293,9 @@ proc nimCharToStr(x: char): string {.compilerRtl.} = result = newString(1) result[0] = x -proc `$`(x: uint64): string = +proc `$`*(x: uint64): string {.noSideEffect.} = + ## The stringify operator for an unsigned integer argument. Returns `x` + ## converted to a decimal string. if x == 0: result = "0" else: |