Skip to content

Commit

Permalink
Constrain isSomeString
Browse files Browse the repository at this point in the history
  • Loading branch information
monarchdodra committed May 25, 2014
1 parent 0ae323d commit e2e3a70
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -4260,7 +4260,7 @@ unittest
Reads a string and returns it.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isInputRange!Range && isSomeString!T && !is(T == enum))
if (isInputRange!Range && is(StringTypeOf!T) && !isAggregateType!T && !is(T == enum))
{
if (spec.spec == '(')
{
Expand Down Expand Up @@ -4336,7 +4336,7 @@ unittest
Reads an array (except for string types) and returns it.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isInputRange!Range && isArray!T && !isSomeString!T && !is(T == enum))
if (isInputRange!Range && isArray!T && !is(StringTypeOf!T) && !isAggregateType!T && !is(T == enum))
{
if (spec.spec == '(')
{
Expand Down
2 changes: 1 addition & 1 deletion std/range.d
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ $(D ElementType).
*/
template ElementEncodingType(R)
{
static if (isNarrowString!R && is(R : E[], E))
static if (is(StringTypeOf!R) && is(R : E[], E))
alias ElementEncodingType = E;
else
alias ElementEncodingType = ElementType!R;
Expand Down
32 changes: 22 additions & 10 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -4857,11 +4857,8 @@ template ArrayTypeOf(T)
static assert(0, T.stringof~" is not an array type");
}

unittest
{
}

/*
Always returns the Dynamic Array version.
*/
template StringTypeOf(T)
{
Expand All @@ -4874,7 +4871,10 @@ template StringTypeOf(T)
}
else static if (is(T : const char[]) || is(T : const wchar[]) || is(T : const dchar[]))
{
alias StringTypeOf = ArrayTypeOf!T;
static if (is(T : U[], U))
alias StringTypeOf = U[];
else
static assert(0);
}
else
static assert(0, T.stringof~" is not a string type");
Expand Down Expand Up @@ -4904,6 +4904,11 @@ unittest
}
}

unittest
{
static assert(is(StringTypeOf!(char[4]) == char[]));
}

/*
*/
template AssocArrayTypeOf(T)
Expand Down Expand Up @@ -5133,12 +5138,18 @@ unittest

/**
Detect whether $(D T) is one of the built-in string types.
The built-in string types are $(D Char[]), where $(D Char) is any of $(D char),
$(D wchar) or $(D dchar), with or without qualifiers.
Static arrays of characters (like $(D char[80]) are not considered
built-in string types.
*/
enum bool isSomeString(T) = is(StringTypeOf!T) && !isAggregateType!T;
enum bool isSomeString(T) = is(StringTypeOf!T) && !isAggregateType!T && !isStaticArray!T;

unittest
{
foreach (T; TypeTuple!(char[], dchar[], string, wstring, dstring, char[4]))
foreach (T; TypeTuple!(char[], dchar[], string, wstring, dstring))
{
static assert( isSomeString!( T ));
static assert(!isSomeString!(SubTypeOf!(T)));
Expand All @@ -5148,16 +5159,17 @@ unittest
static assert(!isSomeString!(int[]));
static assert(!isSomeString!(byte[]));
static assert(!isSomeString!(typeof(null)));
static assert(!isSomeString!(char[4]));

enum ES : string { a = "aaa", b = "bbb" }
static assert( isSomeString!ES);
}

enum bool isNarrowString(T) = (is(T : const char[]) || is(T : const wchar[])) && !isAggregateType!T;
enum bool isNarrowString(T) = (is(T : const char[]) || is(T : const wchar[])) && !isAggregateType!T && !isStaticArray!T;

unittest
{
foreach (T; TypeTuple!(char[], string, wstring, char[4]))
foreach (T; TypeTuple!(char[], string, wstring))
{
foreach (Q; TypeTuple!(MutableOf, ConstOf, ImmutableOf)/*TypeQualifierList*/)
{
Expand All @@ -5166,7 +5178,7 @@ unittest
}
}

foreach (T; TypeTuple!(int, int[], byte[], dchar[], dstring))
foreach (T; TypeTuple!(int, int[], byte[], dchar[], dstring, char[4]))
{
foreach (Q; TypeQualifierList)
{
Expand Down

0 comments on commit e2e3a70

Please sign in to comment.