strformat: introduce 'fmt' as an alias for '%'; ensure overloading resolution produces a clash between strformat.'%' and json.'%'
This commit is contained in:
parent
425f221440
commit
66634fe1a0
1 changed files with 18 additions and 1 deletions
|
|
@ -226,7 +226,7 @@ template callFormatOption(res, arg, option) {.dirty.} =
|
||||||
else:
|
else:
|
||||||
format($arg, option, res)
|
format($arg, option, res)
|
||||||
|
|
||||||
macro `%`*(pattern: string{lit}): untyped =
|
macro `%`*(pattern: string): untyped =
|
||||||
## For a specification of the ``%`` macro, see the module level documentation.
|
## For a specification of the ``%`` macro, see the module level documentation.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
template check(actual, expected: string) =
|
template check(actual, expected: string) =
|
||||||
|
|
@ -407,6 +407,18 @@ macro `%`*(pattern: string{lit}): untyped =
|
||||||
when defined(debugFmtDsl):
|
when defined(debugFmtDsl):
|
||||||
echo repr result
|
echo repr result
|
||||||
|
|
||||||
|
template fmt*(pattern: string): untyped =
|
||||||
|
## An alias for ``%``. Helps to avoid conflicts with ``json``'s ``%`` operator.
|
||||||
|
## **Examples:**
|
||||||
|
##
|
||||||
|
## .. code-block:: nim
|
||||||
|
## import json
|
||||||
|
## import strformat except `%`
|
||||||
|
##
|
||||||
|
## let example = "oh, look no conflicts anymore"
|
||||||
|
## echo fmt"{example}"
|
||||||
|
%pattern
|
||||||
|
|
||||||
proc mkDigit(v: int, typ: char): string {.inline.} =
|
proc mkDigit(v: int, typ: char): string {.inline.} =
|
||||||
assert(v < 26)
|
assert(v < 26)
|
||||||
if v < 10:
|
if v < 10:
|
||||||
|
|
@ -618,3 +630,8 @@ proc format*(value: string; specifier: string; res: var string) =
|
||||||
"invalid type in format string for string, expected 's', but got " &
|
"invalid type in format string for string, expected 's', but got " &
|
||||||
spec.typ)
|
spec.typ)
|
||||||
res.add alignString(value, spec.minimumWidth, spec.align, spec.fill)
|
res.add alignString(value, spec.minimumWidth, spec.align, spec.fill)
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
import json
|
||||||
|
|
||||||
|
doAssert fmt"{'a'} {'b'}" == "a b"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue