Stringinterpolation /format inspired by Python's f-strings.
You can use eitherfmt or the unary& operator for formatting. The difference between them is subtle but important.
Thefmt"{expr}" syntax is more aesthetically pleasing, but it hides a small gotcha. The string is ageneralized raw string literal. This has some surprising effects:
Example:
importstd/strformatletmsg="hello"assertfmt"{msg}\n"=="hello\\n"
Because the literal is a raw string literal, the\n is not interpreted as an escape sequence.
There are multiple ways to get around this, including the use of the& operator:
Example:
importstd/strformatletmsg="hello"assert&"{msg}\n"=="hello\n"assertfmt"{msg}{'\n'}"=="hello\n"assertfmt("{msg}\n")=="hello\n"assert"{msg}\n".fmt=="hello\n"The choice of style is up to you.
Example:
importstd/strformatassert&"""{"abc":>4}"""==" abc"assert&"""{"abc":<4}"""=="abc "
Example:
importstd/strformatassertfmt"{-12345:08}"=="-0012345"assertfmt"{-1:3}"==" -1"assertfmt"{-1:03}"=="-01"assertfmt"{16:#X}"=="0x10"assertfmt"{123.456}"=="123.456"assertfmt"{123.456:>9.3f}"==" 123.456"assertfmt"{123.456:9.3f}"==" 123.456"assertfmt"{123.456:9.4f}"==" 123.4560"assertfmt"{123.456:>9.0f}"==" 123."assertfmt"{123.456:<9.4f}"=="123.4560 "assertfmt"{123.456:e}"=="1.234560e+02"assertfmt"{123.456:>13e}"==" 1.234560e+02"assertfmt"{123.456:13e}"==" 1.234560e+02"
Example:
importstd/strformatletx=3.14assertfmt"{(if x!=0: 1.0/x else: 0):.5}"=="0.31847"assertfmt"""{(block: var res: string for i in 1..15: res.add (if i mod 15 == 0: "FizzBuzz" elif i mod 5 == 0: "Buzz" elif i mod 3 == 0: "Fizz" else: $i) & " " res)}"""=="1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz "
fmt"{expr=}" expands tofmt"expr={expr}" namely the text of the expression, an equal sign and the results of evaluated expression.
Example:
importstd/strformatassertfmt"{123.456=}"=="123.456=123.456"assertfmt"{123.456=:>9.3f}"=="123.456= 123.456"letx="hello"assertfmt"{x=}"=="x=hello"assertfmt"{x =}"=="x =hello"lety=3.1415926assertfmt"{y=:.2f}"==fmt"y={y:.2f}"assertfmt"{y=}"==fmt"y={y}"assertfmt"{y = : <8}"==fmt"y = 3.14159 "prochello(a:string,b:float):int=12assertfmt"{hello(x, y) = }"=="hello(x, y) = 12"assertfmt"{x.hello(y) = }"=="x.hello(y) = 12"assertfmt"{hello x, y = }"=="hello x, y = 12"Note that it is space sensitive:
Example:
importstd/strformatletx="12"assertfmt"{x=}"=="x=12"assertfmt"{x =:}"=="x =12"assertfmt"{x =}"=="x =12"assertfmt"{x= :}"=="x= 12"assertfmt"{x= }"=="x= 12"assertfmt"{x = :}"=="x = 12"assertfmt"{x = }"=="x = 12"assertfmt"{x = :}"=="x = 12"assertfmt"{x = }"=="x = 12"
An expression like&"{key} is {value:arg} {{z}}" is transformed into:
vartemp=newStringOfCap(educatedCapGuess)temp.formatValue(key,"")temp.add(" is ")temp.formatValue(value,arg)temp.add(" {z}")temp
Parts of the string that are enclosed in the curly braces are interpreted as Nim code. To escape a{ or}, double it.
Within a curly expression, however,{,}, must be escaped with a backslash.
To enable evaluating Nim expressions within curlies, colons inside parentheses do not need to be escaped.
Example:
importstd/strformatletx="hello"assertfmt"""{ "\{(" & x & ")\}" }"""=="{(hello)}"assertfmt"""{{({ x })}}"""=="{(hello)}"assertfmt"""{ $(\{x:1,"world":2\}) }"""=="""[("hello", 1), ("world", 2)]"""
& delegates most of the work to an open overloaded set offormatValue procs. The required signature for a typeT that supports formatting is usuallyprocformatValue(result:varstring;x:T;specifier:string).
The subexpression after the colon (arg in&"{key} is {value:arg} {{z}}") is optional. It will be passed as the last argument toformatValue. When the colon with the subexpression it is left out, an empty string will be taken instead.
For strings and numeric types the optional argument is a so-called "standard format specifier".
The general form of a standard format specifier is:
[[fill]align][sign][#][0][minimumwidth][.precision][type]
The square brackets[] indicate an optional element.
The optionalalign flag can be one of the following:
Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case.
The optionalfill character defines the character to be used to pad the field to the minimum width. The fill character, if present, must be followed by an alignment flag.
Thesign option is only valid for numeric types, and can be one of the following:
Sign | Meaning |
---|---|
+ | Indicates that a sign should be used for both positive as well as negative numbers. |
- | Indicates that a sign should be used only for negative numbers (this is the default behavior). |
(space) | Indicates that a leading space should be used on positive numbers. |
If the# character is present, integers use the 'alternate form' for formatting. This means that binary, octal and hexadecimal output will be prefixed with0b,0o and0x, respectively.
width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.
If the width field is preceded by a zero (0) character, this enables zero-padding.
Theprecision is a decimal number indicating how many digits should be displayed after the decimal point in a floating point conversion. For non-numeric types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is ignored for integer conversions.
Finally, thetype determines how the data should be presented.
The available integer presentation types are:
Type | Result |
---|---|
b | Binary. Outputs the number in base 2. |
d | Decimal Integer. Outputs the number in base 10. |
o | Octal format. Outputs the number in base 8. |
x | Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9. |
X | Hex format. Outputs the number in base 16, using uppercase letters for the digits above 9. |
(None) | The same asd. |
The available floating point presentation types are:
Type | Result |
---|---|
e | Exponent notation. Prints the number in scientific notation using the lettere to indicate the exponent. |
E | Exponent notation. Same ase except it converts the number to uppercase. |
f | Fixed point. Displays the number as a fixed-point number. |
F | Fixed point. Same asf except it converts the number to uppercase. |
g | General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches toe exponent notation. |
G | General format. Same asg except it switches toE if the number gets to large. |
i | Complex General format. This is only supported for complex numbers, which it prints using the mathematical (RE+IMj) format. The real and imaginary parts are printed using the general formatg by default, but it is possible to combine this format with one of the other formats (e.gjf). |
(None) | Similar tog, except that it prints at least one digit after the decimal point. |
Because of the well defined order how templates and macros are expanded, strformat cannot expand template arguments:
templatemyTemplate(arg:untyped):untyped=echo"arg is: ",argecho&"--- {arg} ---"letx="abc"myTemplate(x)
First the templatemyTemplate is expanded, where every identifierarg is substituted with its argument. Thearg inside the format string is not seen by this process, because it is part of a quoted string literal. It is not an identifier yet. Then the strformat macro creates thearg identifier from the string literal, an identifier that cannot be resolved anymore.
The workaround for this is to bind the template argument to a new local variable.
templatemyTemplate(arg:untyped):untyped=block:letarg1{.inject.}=argecho"arg is: ",arg1echo&"--- {arg1} ---"
The use of{.inject.} here is necessary again because of template expansion order and hygienic templates. But since we generally want to keep the hygiene ofmyTemplate, and we do not wantarg1 to be injected into the context wheremyTemplate is expanded, everything is wrapped in ablock.
A curly expression with commas in it like{x,argA,argB} could be transformed toformatValue(result,x,argA,argB) in order to support formatters that do not need to parse a custom language within a custom language but instead prefer to use Nim's existing syntax. This would also help with readability, since there is only so much you can cram into single letter DSLs.
StandardFormatSpecifier=objectfill*,align*:char## Desired fill and alignment.sign*:char## Desired sign.alternateForm*:bool## Whether to prefix binary, octal and hex numbers## with `0b`, `0o`, `0x`.padWithZero*:bool## Whether to pad with zeros rather than spaces.minimumWidth*,precision*:int## Desired minimum width and precision.typ*:char## Type like 'f', 'g' or 'd'.endPosition*:int## End position in the format specifier after## `parseStandardFormatSpecifier` returned.
procformatValue(result:varstring;value:SomeFloat;specifier:staticstring)
procformatValue(result:varstring;value:SomeFloat;specifier:string)
procformatValue(result:varstring;value:string;specifier:staticstring)
procformatValue(result:varstring;value:string;specifier:string) {....raises:[ValueError],tags:[],forbids:[].}
procformatValue[T:SomeInteger](result:varstring;value:T;specifier:staticstring)
procformatValue[T:SomeInteger](result:varstring;value:T;specifier:string)
procparseStandardFormatSpecifier(s:string;start=0;ignoreUnknownSuffix=false):StandardFormatSpecifier {....raises:[ValueError],tags:[],forbids:[].}
[[fill]align][sign][#][0][minimumwidth][.precision][type]
This is only of interest if you want to write a customformat proc that should support the standard format specifiers. IfignoreUnknownSuffix is true, an unknown suffix after thetype field is not an error.
Source Edittemplate`&`(pattern:string{lit}):string {.callsite.}
Example:
letx=7assert&"{x}\n"=="7\n"# regular string literalassert&"{x}\n"=="{x}\n".fmt# `fmt` can be used insteadassert&"{x}\n"!=fmt"{x}\n"# see `fmt` docs, this would use a raw string literalSource Edit
templatefmt(pattern:staticstring;openChar:staticchar;closeChar:staticchar):string {.callsite.}
Example:
letx=7assert"var is {x * 2}".fmt=="var is 14"assert"var is {{x}}".fmt=="var is {x}"# escape via doublingconsts="foo: {x}"asserts.fmt=="foo: 7"# also works with const stringsassertfmt"\n"==r"\n"# raw string literalassert"\n".fmt=="\n"# regular literal (likewise with `fmt("\n")` or `fmt "\n"`)
Example:
# custom `openChar`, `closeChar`letx=7assert"<x>".fmt('<', '>') == "7"assert"<<<x>>>".fmt('<', '>') == "<7>"assert"`x`".fmt('`','`')=="7"Source Edit