Add compile-time checked variants of writef and writefln to std.outbuffer.

This commit is contained in:
Brandon Mitchell 2021-08-09 14:21:54 -07:00
parent ccecbda25f
commit 68c9430f19

View file

@ -13,6 +13,7 @@ Serialize data to `ubyte` arrays.
module std.outbuffer; module std.outbuffer;
import core.stdc.stdarg; import core.stdc.stdarg;
import std.traits : isSomeString;
/********************************************* /*********************************************
* OutBuffer provides a way to build up an array of bytes out * OutBuffer provides a way to build up an array of bytes out
@ -323,6 +324,25 @@ class OutBuffer
assert(b.toString() == "a16b"); assert(b.toString() == "a16b");
} }
/// ditto
void writef(alias fmt, A...)(A args)
if (isSomeString!(typeof(fmt)))
{
import std.format : checkFormatException;
alias e = checkFormatException!(fmt, A);
static assert(!e, e.msg);
return this.writef(fmt, args);
}
///
@safe unittest
{
OutBuffer b = new OutBuffer();
b.writef!"a%sb"(16);
assert(b.toString() == "a16b");
}
/** /**
* Formats and writes its arguments in text format to the OutBuffer, * Formats and writes its arguments in text format to the OutBuffer,
* followed by a newline. * followed by a newline.
@ -350,6 +370,25 @@ class OutBuffer
assert(b.toString() == "a16b\n"); assert(b.toString() == "a16b\n");
} }
/// ditto
void writefln(alias fmt, A...)(A args)
if (isSomeString!(typeof(fmt)))
{
import std.format : checkFormatException;
alias e = checkFormatException!(fmt, A);
static assert(!e, e.msg);
return this.writefln(fmt, args);
}
///
@safe unittest
{
OutBuffer b = new OutBuffer();
b.writefln!"a%sb"(16);
assert(b.toString() == "a16b\n");
}
/***************************************** /*****************************************
* At offset index into buffer, create nbytes of space by shifting upwards * At offset index into buffer, create nbytes of space by shifting upwards
* all data past index. * all data past index.