| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.format; |
| 4 |
|
| 5 |
// Easiest way is to use `%s` everywhere: |
| 6 |
assert(format("I got %s %s for %s euros.", 30, "eggs", 5.27) == "I got 30 eggs for 5.27 euros."); |
| 7 |
|
| 8 |
// Other format characters provide more control: |
| 9 |
assert(format("I got %b %(%X%) for %f euros.", 30, "eggs", 5.27) == "I got 11110 65676773 for 5.270000 euros."); |
| 10 |
} |
| 11 |
|
| 12 |
@safe unittest |
| 13 |
{ |
| 14 |
import std.format; |
| 15 |
|
| 16 |
/* |
| 17 |
The trailing end of the sub-format string following the specifier for |
| 18 |
each item is interpreted as the array delimiter, and is therefore |
| 19 |
omitted following the last array item: |
| 20 |
*/ |
| 21 |
assert(format("My items are %(%s %).", [1,2,3]) == "My items are 1 2 3."); |
| 22 |
assert(format("My items are %(%s, %).", [1,2,3]) == "My items are 1, 2, 3."); |
| 23 |
|
| 24 |
/* |
| 25 |
The "%|" delimiter specifier may be used to indicate where the |
| 26 |
delimiter begins, so that the portion of the format string prior to |
| 27 |
it will be retained in the last array element: |
| 28 |
*/ |
| 29 |
assert(format("My items are %(-%s-%|, %).", [1,2,3]) == "My items are -1-, -2-, -3-."); |
| 30 |
|
| 31 |
/* |
| 32 |
These compound format specifiers may be nested in the case of a |
| 33 |
nested array argument: |
| 34 |
*/ |
| 35 |
auto mat = [[1, 2, 3], |
| 36 |
[4, 5, 6], |
| 37 |
[7, 8, 9]]; |
| 38 |
|
| 39 |
assert(format("%(%(%d %) - %)", mat), "1 2 3 - 4 5 6 - 7 8 9"); |
| 40 |
assert(format("[%(%(%d %) - %)]", mat), "[1 2 3 - 4 5 6 - 7 8 9]"); |
| 41 |
assert(format("[%([%(%d %)]%| - %)]", mat), "[1 2 3] - [4 5 6] - [7 8 9]"); |
| 42 |
|
| 43 |
/* |
| 44 |
Strings and characters are escaped automatically inside compound |
| 45 |
format specifiers. To avoid this behavior, use "%-(" instead of "%(": |
| 46 |
*/ |
| 47 |
assert(format("My friends are %s.", ["John", "Nancy"]) == `My friends are ["John", "Nancy"].`); |
| 48 |
assert(format("My friends are %(%s, %).", ["John", "Nancy"]) == `My friends are "John", "Nancy".`); |
| 49 |
assert(format("My friends are %-(%s, %).", ["John", "Nancy"]) == `My friends are John, Nancy.`); |
| 50 |
} |
| 51 |
|
| 52 |
@safe unittest |
| 53 |
{ |
| 54 |
import std.format; |
| 55 |
|
| 56 |
// Flags can be used to influence to outcome: |
| 57 |
assert(format("%g != %+#g", 3.14, 3.14) == "3.14 != +3.14000"); |
| 58 |
|
| 59 |
// Width and precision help to arrange the formatted result: |
| 60 |
assert(format(">%10.2f<", 1234.56789) == "> 1234.57<"); |
| 61 |
|
| 62 |
// Numbers can be grouped: |
| 63 |
assert(format("%,4d", int.max) == "21,4748,3647"); |
| 64 |
|
| 65 |
// It's possible to specify the position of an argument: |
| 66 |
assert(format("%3$s %1$s", 3, 17, 5) == "5 3"); |
| 67 |
} |
| 68 |
|
| 69 |
@safe unittest |
| 70 |
{ |
| 71 |
import std.format; |
| 72 |
|
| 73 |
// Width as argument |
| 74 |
assert(format(">%*s<", 10, "abc") == "> abc<"); |
| 75 |
|
| 76 |
// Precision as argument |
| 77 |
assert(format(">%.*f<", 5, 123.2) == ">123.20000<"); |
| 78 |
|
| 79 |
// Grouping as argument |
| 80 |
assert(format("%,*d", 1, int.max) == "2,1,4,7,4,8,3,6,4,7"); |
| 81 |
|
| 82 |
// Grouping separator as argument |
| 83 |
assert(format("%,3?d", '_', int.max) == "2_147_483_647"); |
| 84 |
|
| 85 |
// All at once |
| 86 |
assert(format("%*.*,*?d", 20, 15, 6, '/', int.max) == " 000/002147/483647"); |
| 87 |
} |
| 88 |
|
| 89 |
@safe unittest |
| 90 |
{ |
| 91 |
import std.format; |
| 92 |
|
| 93 |
import std.exception : assertThrown; |
| 94 |
|
| 95 |
assertThrown!FormatException(format("%d", "foo")); |
| 96 |
} |
| 97 |
|
| 98 |
@safe pure unittest |
| 99 |
{ |
| 100 |
import std.format; |
| 101 |
|
| 102 |
assert(format("Here are %d %s.", 3, "apples") == "Here are 3 apples."); |
| 103 |
|
| 104 |
assert("Increase: %7.2f %%".format(17.4285) == "Increase: 17.43 %"); |
| 105 |
} |
| 106 |
|
| 107 |
@safe pure unittest |
| 108 |
{ |
| 109 |
import std.format; |
| 110 |
|
| 111 |
auto s = format!"%s is %s"("Pi", 3.14); |
| 112 |
assert(s == "Pi is 3.14"); |
| 113 |
|
| 114 |
// This line doesn't compile, because 3.14 cannot be formatted with %d: |
| 115 |
// s = format!"%s is %d"("Pi", 3.14); |
| 116 |
} |
| 117 |
|
| 118 |
@safe pure unittest |
| 119 |
{ |
| 120 |
import std.format; |
| 121 |
|
| 122 |
char[20] buf; |
| 123 |
assert(sformat(buf[], "Here are %d %s.", 3, "apples") == "Here are 3 apples."); |
| 124 |
|
| 125 |
assert(buf[].sformat("Increase: %7.2f %%", 17.4285) == "Increase: 17.43 %"); |
| 126 |
} |
| 127 |
|
| 128 |
@safe pure unittest |
| 129 |
{ |
| 130 |
import std.format; |
| 131 |
|
| 132 |
char[20] buf; |
| 133 |
|
| 134 |
assert(sformat!"Here are %d %s."(buf[], 3, "apples") == "Here are 3 apples."); |
| 135 |
|
| 136 |
// This line doesn't compile, because 3.14 cannot be formatted with %d: |
| 137 |
// writeln(sformat!"Here are %d %s."(buf[], 3.14, "apples")); |
| 138 |
} |
| 139 |
|