format: Add format string tests

This commit is contained in:
Eugen Wissner 2017-11-25 15:24:45 +01:00
parent 02d1d8218b
commit b612e978bf

View File

@ -1637,7 +1637,7 @@ pure nothrow @nogc
return buf[0 .. tlen + cast(int) (bf - buf.ptr)]; return buf[0 .. tlen + cast(int) (bf - buf.ptr)];
} }
package(tanya) char[] format(string fmt)(return char[] buf, ...) char[] format(string fmt)(return char[] buf, ...)
nothrow nothrow
{ {
va_list va; va_list va;
@ -1758,56 +1758,13 @@ private nothrow unittest
assert(format!"%%k"(buffer) == "%k"); assert(format!"%%k"(buffer) == "%k");
} }
private template FmtSpec(string spec) private struct FormatSpec
{ {
template getModifier(char modifier)
{
static if (modifier == '+')
{
enum Modifier getModifier = Modifier.leadingPlus;
}
else static if (modifier == ' ')
{
enum Modifier getModifier = Modifier.leadingSpace;
}
else static if (modifier = '#')
{
enum Modifier getModifier = Modifier.leading0x;
}
else static if (modifier == '\'')
{
enum Modifier getModifier = Modifier.tripletComma;
}
else static if (modifier == '0')
{
enum Modifier getModifier = Modifier.leadingZero;
}
else
{
static assert(false, "Unknown modifier " ~ modifier);
}
} }
template parseModifiers(string modifiers) // Returns the position of `tag` in `fmt`. If `tag` can't be found, returns the
{ // length of `fmt`.
static if (modifiers.length == 0) private size_t specPosition(string fmt, char tag)()
{
enum uint parseModifiers = 0;
}
else
{
enum uint parseModifiers = getModifier!(modifiers[0])
| parseModifiers!(modifiers[1 .. $]);
}
}
struct FmtSpec
{
enum uint modifiers = parseModifiers!spec;
}
}
private enum size_t specPosition(string fmt, char tag)()
{ {
foreach (i, c; fmt) foreach (i, c; fmt)
{ {
@ -1819,7 +1776,7 @@ private enum size_t specPosition(string fmt, char tag)()
return fmt.length; return fmt.length;
} }
private template ParseFmt(string fmt) private template ParseFmt(string fmt, size_t pos = 0)
{ {
static if (fmt.length == 0) static if (fmt.length == 0)
{ {
@ -1827,33 +1784,40 @@ private template ParseFmt(string fmt)
} }
else static if (fmt[0] == '{') else static if (fmt[0] == '{')
{ {
enum size_t pos = specPosition!(fmt[1 .. $], '}'); static if (fmt.length > 1 && fmt[1] == '{')
static if (pos < fmt.length - 1)
{ {
alias ParseFmt = AliasSeq!(FmtSpec!(fmt[1 .. pos + 1]), enum size_t pos = specPosition!(fmt[2 .. $], '{') + 2;
ParseFmt!(fmt[pos + 2 .. $])); alias ParseFmt = AliasSeq!(fmt[1 .. pos],
ParseFmt!(fmt[pos .. $], pos));
}
else
{
enum size_t pos = specPosition!(fmt[1 .. $], '}') + 1;
static if (pos < fmt.length)
{
alias ParseFmt = AliasSeq!(FormatSpec(),
ParseFmt!(fmt[pos + 1 .. $], pos + 1));
} }
else else
{ {
static assert(false, "Enclosing '}' is missing"); static assert(false, "Enclosing '}' is missing");
} }
} }
}
else else
{ {
enum size_t pos = specPosition!(fmt, '{'); enum size_t pos = specPosition!(fmt, '{');
alias ParseFmt = AliasSeq!(fmt[0 .. pos], ParseFmt!(fmt[pos .. $])); alias ParseFmt = AliasSeq!(fmt[0 .. pos],
ParseFmt!(fmt[pos .. $], pos));
} }
} }
void fmt(string format)() @nogc nothrow pure @safe unittest
{
foreach (e; ParseFmt!format)
{
static if (is(typeof(e) == string))
{ {
} static assert(ParseFmt!"".length == 0);
else
{ static assert(ParseFmt!"asdf".length == 1);
} static assert(ParseFmt!"asdf"[0] == "asdf");
}
static assert(ParseFmt!"{}".length == 1);
} }