Skip to content

Commit 9a60eae

Browse files
committed
fixes #7078
1 parent bba6d62 commit 9a60eae

File tree

3 files changed

+114
-114
lines changed

3 files changed

+114
-114
lines changed

lib/pure/strformat.nim

Lines changed: 111 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ Examples:
1515
1616
.. code-block:: nim
1717
18-
doAssert %"""{"abc":>4}""" == " abc"
19-
doAssert %"""{"abc":<4}""" == "abc "
18+
doAssert &"""{"abc":>4}""" == " abc"
19+
doAssert &"""{"abc":<4}""" == "abc "
2020
21-
doAssert %"{-12345:08}" == "-0012345"
22-
doAssert %"{-1:3}" == " -1"
23-
doAssert %"{-1:03}" == "-01"
24-
doAssert %"{16:#X}" == "0x10"
21+
doAssert &"{-12345:08}" == "-0012345"
22+
doAssert &"{-1:3}" == " -1"
23+
doAssert &"{-1:03}" == "-01"
24+
doAssert &"{16:#X}" == "0x10"
2525
26-
doAssert %"{123.456}" == "123.456"
27-
doAssert %"{123.456:>9.3f}" == " 123.456"
28-
doAssert %"{123.456:9.3f}" == " 123.456"
29-
doAssert %"{123.456:9.4f}" == " 123.4560"
30-
doAssert %"{123.456:>9.0f}" == " 123."
31-
doAssert %"{123.456:<9.4f}" == "123.4560 "
26+
doAssert &"{123.456}" == "123.456"
27+
doAssert &"{123.456:>9.3f}" == " 123.456"
28+
doAssert &"{123.456:9.3f}" == " 123.456"
29+
doAssert &"{123.456:9.4f}" == " 123.4560"
30+
doAssert &"{123.456:>9.0f}" == " 123."
31+
doAssert &"{123.456:<9.4f}" == "123.4560 "
3232
33-
doAssert %"{123.456:e}" == "1.234560e+02"
34-
doAssert %"{123.456:>13e}" == " 1.234560e+02"
35-
doAssert %"{123.456:13e}" == " 1.234560e+02"
33+
doAssert &"{123.456:e}" == "1.234560e+02"
34+
doAssert &"{123.456:>13e}" == " 1.234560e+02"
35+
doAssert &"{123.456:13e}" == " 1.234560e+02"
3636
3737
38-
An expression like ``%"{key} is {value:arg} {{z}}"`` is transformed into:
38+
An expression like ``&"{key} is {value:arg} {{z}}"`` is transformed into:
3939
4040
.. code-block:: nim
4141
var temp = newStringOfCap(educatedCapGuess)
@@ -48,13 +48,13 @@ An expression like ``%"{key} is {value:arg} {{z}}"`` is transformed into:
4848
Parts of the string that are enclosed in the curly braces are interpreted
4949
as Nim code, to escape an ``{`` or ``}`` double it.
5050
51-
``%`` delegates most of the work to an open overloaded set
51+
``&`` delegates most of the work to an open overloaded set
5252
of ``format`` procs. The required signature for a type ``T`` that supports
5353
formatting is usually ``proc format(x: T; result: var string)`` for efficiency
5454
but can also be ``proc format(x: T): string``. ``add`` and ``$`` procs are
5555
used as the fallback implementation.
5656
57-
This is the concrete lookup algorithm that ``%`` uses:
57+
This is the concrete lookup algorithm that ``&`` uses:
5858
5959
.. code-block:: nim
6060
@@ -69,7 +69,7 @@ This is the concrete lookup algorithm that ``%`` uses:
6969
7070
7171
The subexpression after the colon
72-
(``arg`` in ``%"{key} is {value:arg} {{z}}"``) is an optional argument
72+
(``arg`` in ``&"{key} is {value:arg} {{z}}"``) is an optional argument
7373
passed to ``format``.
7474
7575
If an optional argument is present the following lookup algorithm is used:
@@ -226,8 +226,8 @@ template callFormatOption(res, arg, option) {.dirty.} =
226226
else:
227227
format($arg, option, res)
228228

229-
macro `%`*(pattern: string): untyped =
230-
## For a specification of the ``%`` macro, see the module level documentation.
229+
macro `&`*(pattern: string): untyped =
230+
## For a specification of the ``&`` macro, see the module level documentation.
231231
runnableExamples:
232232
template check(actual, expected: string) =
233233
doAssert actual == expected
@@ -236,113 +236,113 @@ macro `%`*(pattern: string): untyped =
236236

237237
# Basic tests
238238
let s = "string"
239-
check %"{0} {s}", "0 string"
240-
check %"{s[0..2].toUpperAscii}", "STR"
241-
check %"{-10:04}", "-010"
242-
check %"{-10:<04}", "-010"
243-
check %"{-10:>04}", "-010"
244-
check %"0x{10:02X}", "0x0A"
239+
check &"{0} {s}", "0 string"
240+
check &"{s[0..2].toUpperAscii}", "STR"
241+
check &"{-10:04}", "-010"
242+
check &"{-10:<04}", "-010"
243+
check &"{-10:>04}", "-010"
244+
check &"0x{10:02X}", "0x0A"
245245

246-
check %"{10:#04X}", "0x0A"
246+
check &"{10:#04X}", "0x0A"
247247

248-
check %"""{"test":#>5}""", "#test"
249-
check %"""{"test":>5}""", " test"
248+
check &"""{"test":#>5}""", "#test"
249+
check &"""{"test":>5}""", " test"
250250

251-
check %"""{"test":#^7}""", "#test##"
251+
check &"""{"test":#^7}""", "#test##"
252252

253-
check %"""{"test": <5}""", "test "
254-
check %"""{"test":<5}""", "test "
255-
check %"{1f:.3f}", "1.000"
256-
check %"Hello, {s}!", "Hello, string!"
253+
check &"""{"test": <5}""", "test "
254+
check &"""{"test":<5}""", "test "
255+
check &"{1f:.3f}", "1.000"
256+
check &"Hello, {s}!", "Hello, string!"
257257

258258
# Tests for identifers without parenthesis
259-
check %"{s} works{s}", "string worksstring"
260-
check %"{s:>7}", " string"
261-
doAssert(not compiles(%"{s_works}")) # parsed as identifier `s_works`
259+
check &"{s} works{s}", "string worksstring"
260+
check &"{s:>7}", " string"
261+
doAssert(not compiles(&"{s_works}")) # parsed as identifier `s_works`
262262

263263
# Misc general tests
264-
check %"{{}}", "{}"
265-
check %"{0}%", "0%"
266-
check %"{0}%asdf", "0%asdf"
267-
check %("\n{\"\\n\"}\n"), "\n\n\n"
268-
check %"""{"abc"}s""", "abcs"
264+
check &"{{}}", "{}"
265+
check &"{0}%", "0%"
266+
check &"{0}%asdf", "0%asdf"
267+
check &("\n{\"\\n\"}\n"), "\n\n\n"
268+
check &"""{"abc"}s""", "abcs"
269269

270270
# String tests
271-
check %"""{"abc"}""", "abc"
272-
check %"""{"abc":>4}""", " abc"
273-
check %"""{"abc":<4}""", "abc "
274-
check %"""{"":>4}""", " "
275-
check %"""{"":<4}""", " "
271+
check &"""{"abc"}""", "abc"
272+
check &"""{"abc":>4}""", " abc"
273+
check &"""{"abc":<4}""", "abc "
274+
check &"""{"":>4}""", " "
275+
check &"""{"":<4}""", " "
276276

277277
# Int tests
278-
check %"{12345}", "12345"
279-
check %"{ - 12345}", "-12345"
280-
check %"{12345:6}", " 12345"
281-
check %"{12345:>6}", " 12345"
282-
check %"{12345:4}", "12345"
283-
check %"{12345:08}", "00012345"
284-
check %"{-12345:08}", "-0012345"
285-
check %"{0:0}", "0"
286-
check %"{0:02}", "00"
287-
check %"{-1:3}", " -1"
288-
check %"{-1:03}", "-01"
289-
check %"{10}", "10"
290-
check %"{16:#X}", "0x10"
291-
check %"{16:^#7X}", " 0x10 "
292-
check %"{16:^+#7X}", " +0x10 "
278+
check &"{12345}", "12345"
279+
check &"{ - 12345}", "-12345"
280+
check &"{12345:6}", " 12345"
281+
check &"{12345:>6}", " 12345"
282+
check &"{12345:4}", "12345"
283+
check &"{12345:08}", "00012345"
284+
check &"{-12345:08}", "-0012345"
285+
check &"{0:0}", "0"
286+
check &"{0:02}", "00"
287+
check &"{-1:3}", " -1"
288+
check &"{-1:03}", "-01"
289+
check &"{10}", "10"
290+
check &"{16:#X}", "0x10"
291+
check &"{16:^#7X}", " 0x10 "
292+
check &"{16:^+#7X}", " +0x10 "
293293

294294
# Hex tests
295-
check %"{0:x}", "0"
296-
check %"{-0:x}", "0"
297-
check %"{255:x}", "ff"
298-
check %"{255:X}", "FF"
299-
check %"{-255:x}", "-ff"
300-
check %"{-255:X}", "-FF"
301-
check %"{255:x} uNaffeCteD CaSe", "ff uNaffeCteD CaSe"
302-
check %"{255:X} uNaffeCteD CaSe", "FF uNaffeCteD CaSe"
303-
check %"{255:4x}", " ff"
304-
check %"{255:04x}", "00ff"
305-
check %"{-255:4x}", " -ff"
306-
check %"{-255:04x}", "-0ff"
295+
check &"{0:x}", "0"
296+
check &"{-0:x}", "0"
297+
check &"{255:x}", "ff"
298+
check &"{255:X}", "FF"
299+
check &"{-255:x}", "-ff"
300+
check &"{-255:X}", "-FF"
301+
check &"{255:x} uNaffeCteD CaSe", "ff uNaffeCteD CaSe"
302+
check &"{255:X} uNaffeCteD CaSe", "FF uNaffeCteD CaSe"
303+
check &"{255:4x}", " ff"
304+
check &"{255:04x}", "00ff"
305+
check &"{-255:4x}", " -ff"
306+
check &"{-255:04x}", "-0ff"
307307

308308
# Float tests
309-
check %"{123.456}", "123.456"
310-
check %"{-123.456}", "-123.456"
311-
check %"{123.456:.3f}", "123.456"
312-
check %"{123.456:+.3f}", "+123.456"
313-
check %"{-123.456:+.3f}", "-123.456"
314-
check %"{-123.456:.3f}", "-123.456"
315-
check %"{123.456:1g}", "123.456"
316-
check %"{123.456:.1f}", "123.5"
317-
check %"{123.456:.0f}", "123."
318-
#check %"{123.456:.0f}", "123."
319-
check %"{123.456:>9.3f}", " 123.456"
320-
check %"{123.456:9.3f}", " 123.456"
321-
check %"{123.456:>9.4f}", " 123.4560"
322-
check %"{123.456:>9.0f}", " 123."
323-
check %"{123.456:<9.4f}", "123.4560 "
309+
check &"{123.456}", "123.456"
310+
check &"{-123.456}", "-123.456"
311+
check &"{123.456:.3f}", "123.456"
312+
check &"{123.456:+.3f}", "+123.456"
313+
check &"{-123.456:+.3f}", "-123.456"
314+
check &"{-123.456:.3f}", "-123.456"
315+
check &"{123.456:1g}", "123.456"
316+
check &"{123.456:.1f}", "123.5"
317+
check &"{123.456:.0f}", "123."
318+
#check &"{123.456:.0f}", "123."
319+
check &"{123.456:>9.3f}", " 123.456"
320+
check &"{123.456:9.3f}", " 123.456"
321+
check &"{123.456:>9.4f}", " 123.4560"
322+
check &"{123.456:>9.0f}", " 123."
323+
check &"{123.456:<9.4f}", "123.4560 "
324324

325325
# Float (scientific) tests
326-
check %"{123.456:e}", "1.234560e+02"
327-
check %"{123.456:>13e}", " 1.234560e+02"
328-
check %"{123.456:<13e}", "1.234560e+02 "
329-
check %"{123.456:.1e}", "1.2e+02"
330-
check %"{123.456:.2e}", "1.23e+02"
331-
check %"{123.456:.3e}", "1.235e+02"
326+
check &"{123.456:e}", "1.234560e+02"
327+
check &"{123.456:>13e}", " 1.234560e+02"
328+
check &"{123.456:<13e}", "1.234560e+02 "
329+
check &"{123.456:.1e}", "1.2e+02"
330+
check &"{123.456:.2e}", "1.23e+02"
331+
check &"{123.456:.3e}", "1.235e+02"
332332

333333
# Note: times.format adheres to the format protocol. Test that this
334334
# works:
335335
import times
336336

337337
var nullTime: DateTime
338-
check %"{nullTime:yyyy-mm-dd}", "0000-00-00"
338+
check &"{nullTime:yyyy-mm-dd}", "0000-00-00"
339339

340340
# Unicode string tests
341-
check %"""{"αβγ"}""", "αβγ"
342-
check %"""{"αβγ":>5}""", " αβγ"
343-
check %"""{"αβγ":<5}""", "αβγ "
344-
check %"""a{"a"}α{"α"}€{"€"}𐍈{"𐍈"}""", "aaαα€€𐍈𐍈"
345-
check %"""a{"a":2}α{"α":2}€{"€":2}𐍈{"𐍈":2}""", "aa αα €€ 𐍈𐍈 "
341+
check &"""{"αβγ"}""", "αβγ"
342+
check &"""{"αβγ":>5}""", " αβγ"
343+
check &"""{"αβγ":<5}""", "αβγ "
344+
check &"""a{"a"}α{"α"}{""}𐍈{"𐍈"}""", "aaαα€€𐍈𐍈"
345+
check &"""a{"a":2}α{"α":2}{"":2}𐍈{"𐍈":2}""", "aa αα €€ 𐍈𐍈 "
346346
# Invalid unicode sequences should be handled as plain strings.
347347
# Invalid examples taken from: https://stackoverflow.com/a/3886015/1804173
348348
let invalidUtf8 = [
@@ -351,10 +351,10 @@ macro `%`*(pattern: string): untyped =
351351
"\xf0\x28\x8c\xbc", "\xf0\x90\x28\xbc", "\xf0\x28\x8c\x28"
352352
]
353353
for s in invalidUtf8:
354-
check %"{s:>5}", repeat(" ", 5-s.len) & s
354+
check &"{s:>5}", repeat(" ", 5-s.len) & s
355355

356356
if pattern.kind notin {nnkStrLit..nnkTripleStrLit}:
357-
error "% only works with string literals", pattern
357+
error "& only works with string literals", pattern
358358
let f = pattern.strVal
359359
var i = 0
360360
let res = genSym(nskVar, "fmtRes")
@@ -408,17 +408,17 @@ macro `%`*(pattern: string): untyped =
408408
echo repr result
409409

410410
template fmt*(pattern: string): untyped =
411-
## An alias for ``%``. Helps to avoid conflicts with ``json``'s ``%`` operator.
411+
## An alias for ``&``.
412412
## **Examples:**
413413
##
414414
## .. code-block:: nim
415415
## import json
416-
## import strformat except `%`
416+
## import strformat except `&`
417417
##
418418
## let example = "oh, look no conflicts anymore"
419419
## echo fmt"{example}"
420-
bind `%`
421-
%pattern
420+
bind `&`
421+
&pattern
422422

423423
proc mkDigit(v: int, typ: char): string {.inline.} =
424424
assert(v < 26)
@@ -573,7 +573,7 @@ proc parseStandardFormatSpecifier*(s: string; start = 0;
573573
proc format*(value: SomeInteger; specifier: string; res: var string) =
574574
## Standard format implementation for ``SomeInteger``. It makes little
575575
## sense to call this directly, but it is required to exist
576-
## by the ``%`` macro.
576+
## by the ``&`` macro.
577577
let spec = parseStandardFormatSpecifier(specifier)
578578
var radix = 10
579579
case spec.typ
@@ -590,7 +590,7 @@ proc format*(value: SomeInteger; specifier: string; res: var string) =
590590
proc format*(value: SomeReal; specifier: string; res: var string) =
591591
## Standard format implementation for ``SomeReal``. It makes little
592592
## sense to call this directly, but it is required to exist
593-
## by the ``%`` macro.
593+
## by the ``&`` macro.
594594
let spec = parseStandardFormatSpecifier(specifier)
595595

596596
var fmode = ffDefault
@@ -622,7 +622,7 @@ proc format*(value: SomeReal; specifier: string; res: var string) =
622622
proc format*(value: string; specifier: string; res: var string) =
623623
## Standard format implementation for ``string``. It makes little
624624
## sense to call this directly, but it is required to exist
625-
## by the ``%`` macro.
625+
## by the ``&`` macro.
626626
let spec = parseStandardFormatSpecifier(specifier)
627627
case spec.typ
628628
of 's', '\0': discard

tests/stdlib/tfrexp1.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ proc frexp_test(lo, hi, step: float64) =
2222
doAssert(abs(rslt - x) < eps)
2323

2424
when manualTest:
25-
echo %("x: {x:10.3f} exp: {exp:4d} frac: {frac:24.20f} check: {$(abs(rslt - x) < eps):-5s} {rslt: 9.3f}")
25+
echo fmt("x: {x:10.3f} exp: {exp:4d} frac: {frac:24.20f} check: {$(abs(rslt - x) < eps):-5s} {rslt: 9.3f}")
2626
x += step
2727

2828
when manualTest:

tests/stdlib/tstrformat.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ type Obj = object
99
proc `$`(o: Obj): string = "foobar"
1010

1111
var o: Obj
12-
doAssert %"{o}" == "foobar"
13-
doAssert %"{o:10}" == "foobar "
12+
doAssert fmt"{o}" == "foobar"
13+
doAssert fmt"{o:10}" == "foobar "

0 commit comments

Comments
 (0)