nogcFormat

Same as nogcFormatTo, but it internally uses static malloc buffer to write formatted string to. So be careful that next call replaces internal buffer data and previous result isn't valid anymore.

const(char)[]
nogcFormat
(
string fmt = "%s"
ARGS...
)
(
auto ref ARGS args
)

Examples

1 import bc.core.memory;
2 import std.algorithm : filter;
3 import std.range : chunks;
4 
5 assert(nogcFormat!"abcd abcd" == "abcd abcd");
6 assert(nogcFormat!"123456789a" == "123456789a");
7 version (D_NoBoundsChecks) {}
8 else version (D_Exceptions)
9 {
10     () @trusted
11     {
12         import core.exception : RangeError;
13         import std.exception : assertThrown;
14         char[5] buf;
15         assertThrown!RangeError(buf.nogcFormatTo!"123412341234");
16     }();
17 }
18 
19 // literal escape
20 assert(nogcFormat!"123 %%" == "123 %");
21 assert(nogcFormat!"%%%%" == "%%");
22 
23 // %d
24 assert(nogcFormat!"%d"(1234) == "1234");
25 assert(nogcFormat!"%4d"(42) == "  42");
26 assert(nogcFormat!"%04d"(42) == "0042");
27 assert(nogcFormat!"%04d"(-42) == "-042");
28 assert(nogcFormat!"ab%dcd"(1234) == "ab1234cd");
29 assert(nogcFormat!"ab%d%d"(1234, 56) == "ab123456");
30 
31 // %x
32 assert(nogcFormat!"0x%x"(0x1234) == "0x1234");
33 
34 // %p
35 assert(nogcFormat!("%p")(0x1234) == "0000000000001234");
36 
37 // %s
38 assert(nogcFormat!"12345%s"("12345") == "1234512345");
39 assert(nogcFormat!"12345%s"(12345) == "1234512345");
40 enum Floop {XXX, YYY, ZZZ}
41 assert(nogcFormat!"12345%s"(Floop.YYY) == "12345YYY");
42 char[4] str = "foo\0";
43 assert(() @trusted { return nogcFormat!"%s"(str.ptr); }() == "foo");
44 
45 version (D_BetterC) {}
46 else
47 {
48     assert(nogcFormat!"%s"(
49         UUID([138, 179, 6, 14, 44, 186, 79, 35, 183, 76, 181, 45, 179, 189, 251, 70]))
50         == "8ab3060e-2cba-4f23-b74c-b52db3bdfb46");
51 }
52 
53 // array format
54 version (D_BetterC)
55 {
56     int[] arr = () @trusted { return (cast(int*)enforceMalloc(int.sizeof*10))[0..10]; }();
57     foreach (i; 0..10) arr[i] = i;
58     scope (exit) () @trusted { pureFree(arr.ptr); }();
59 }
60 else auto arr = [0,1,2,3,4,5,6,7,8,9];
61 
62 assert(nogcFormat!"foo %(%d %)"(arr[1..4]) == "foo 1 2 3");
63 assert(nogcFormat!"foo %-(%d %)"(arr[1..4]) == "foo 1 2 3");
64 assert(nogcFormat!"foo %(-%d-%|, %)"(arr[1..4]) == "foo -1-, -2-, -3-");
65 assert(nogcFormat!"%(0x%02x %)"(arr[1..4]) == "0x01 0x02 0x03");
66 assert(nogcFormat!"%(%(%d %)\n%)"(arr[1..$].chunks(3)) == "1 2 3\n4 5 6\n7 8 9");
67 
68 // range format
69 auto r = arr.filter!(a => a < 5);
70 assert(nogcFormat!"%s"(r) == "[0, 1, 2, 3, 4]");
71 
72 // Arg num
73 assert(!__traits(compiles, nogcFormat!"abc"(5)));
74 assert(!__traits(compiles, nogcFormat!"%d"()));
75 assert(!__traits(compiles, nogcFormat!"%d a %d"(5)));
76 
77 // Format error
78 assert(!__traits(compiles, nogcFormat!"%"()));
79 assert(!__traits(compiles, nogcFormat!"abcd%d %"(15)));
80 assert(!__traits(compiles, nogcFormat!"%$"(1)));
81 assert(!__traits(compiles, nogcFormat!"%d"("hello")));
82 assert(!__traits(compiles, nogcFormat!"%x"("hello")));
83 
84 assert(nogcFormat!"Hello %s"(5) == "Hello 5");
85 
86 struct Foo { int x, y; }
87 assert(nogcFormat!("Hello %s")(Foo(1, 2)) == "Hello Foo(x=1, y=2)");
88 
89 version (D_BetterC)
90 {
91     struct Nullable(T) // can't be instanciated in betterC - fake just for the UT
92     {
93         T get() { return T.init; }
94         bool isNull() { return true; }
95         void nullify() {}
96     }
97 }
98 else import std.typecons : Nullable;
99 
100 struct Msg { Nullable!string foo; }
101 assert(nogcFormat!"%s"(Msg.init) == "Msg(foo=null)");
102 
103 RCString s = "abcd";
104 assert(nogcFormat!"%s"(s) == "abcd");
{
    alias T = Tuple!(int, "foo", bool);
    T t = T(42, true);
    assert(nogcFormat(t) == "Tuple(foo=42, true)");
}

{
    alias T = Tuple!(int, "foo", string, "bar", char, "baz");
    T t = T(42, "bar", 'z');
    assert(nogcFormat(t) == "Tuple(foo=42, bar=bar, baz=z)");
}
static struct Custom
{
    int foo = 42;
    void toString(S)(ref S sink) const
    {
        sink.put("custom: ");
        sink.nogcFormatTo!"foo=%d"(foo);
    }
}

Custom c;
assert(nogcFormat(c) == "custom: foo=42");
assert(getFormatSize(c) == "custom: foo=42".length);

Meta