mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 21:51:40 +03:00
Add compile-time checked variants of writef and writefln to std.outbuffer.
This commit is contained in:
parent
ccecbda25f
commit
68c9430f19
1 changed files with 39 additions and 0 deletions
|
@ -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.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue