From ed6ede2e1cbaeb1df1e88da5e11de8f797f7426f Mon Sep 17 00:00:00 2001 From: jmdavis Date: Fri, 15 Jul 2011 23:22:25 -0700 Subject: [PATCH] Split out std.array.join into join and joinImpl. The idea is to make the template constraint match exactly what the user needs to instantiate it rather than seeing the more complex template constraints that the various overloads of join have. Not that the resulting template constraint is altogether short either, but it's now the bare minimum which all join overloads must match rather than including the specifics necessary to distinguish each overload. --- std/array.d | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/std/array.d b/std/array.d index d907ba2d8..62cc7f596 100644 --- a/std/array.d +++ b/std/array.d @@ -1171,6 +1171,7 @@ unittest } } + /++ Concatenates all of the ranges in $(D ror) together into one array using $(D sep) as the separator if present. @@ -1185,6 +1186,35 @@ assert(join([[1, 2, 3], [4, 5]]) == [1, 2, 3, 4, 5]); -------------------- +/ ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) + if(isInputRange!RoR && + isInputRange!(ElementType!RoR) && + isForwardRange!R && + is(Unqual!(ElementType!(ElementType!RoR)) == Unqual!(ElementType!R))) +{ + return joinImpl(ror, sep); +} + +/// Ditto +ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) + if(isInputRange!RoR && isInputRange!(ElementType!RoR)) +{ + return joinImpl(ror); +} + +//Verify Examples. +unittest +{ + assert(join(["hello", "silly", "world"], " ") == "hello silly world"); + assert(join(["hello", "silly", "world"]) == "hellosillyworld"); + + assert(join([[1, 2, 3], [4, 5]], [72, 73]) == [1, 2, 3, 72, 73, 4, 5]); + assert(join([[1, 2, 3], [4, 5]]) == [1, 2, 3, 4, 5]); +} + +// We have joinImpl instead of just making them all join in order to simplify +// the template constraint that the user will see on errors (it's condensed down +// to the conditions that are common to all). +ElementEncodingType!(ElementType!RoR)[] joinImpl(RoR, R)(RoR ror, R sep) if(isInputRange!RoR && isInputRange!(ElementType!RoR) && !isDynamicArray!(ElementType!RoR) && @@ -1210,7 +1240,7 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) return copy(iter, appender!(typeof(return))).data; } -ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) +ElementEncodingType!(ElementType!RoR)[] joinImpl(RoR, R)(RoR ror, R sep) if(isForwardRange!RoR && hasLength!RoR && isDynamicArray!(ElementType!RoR) && @@ -1245,7 +1275,7 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) return cast(RetType)result; } -ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) +ElementEncodingType!(ElementType!RoR)[] joinImpl(RoR, R)(RoR ror, R sep) if(isInputRange!RoR && ((isForwardRange!RoR && !hasLength!RoR) || !isForwardRange!RoR) && isDynamicArray!(ElementType!RoR) && @@ -1282,8 +1312,7 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) return result.data[0 .. $ - sep.length]; } -/// Ditto -ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) +ElementEncodingType!(ElementType!RoR)[] joinImpl(RoR)(RoR ror) if(isInputRange!RoR && isInputRange!(ElementType!RoR) && !isDynamicArray!(ElementType!RoR)) @@ -1303,7 +1332,7 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) return copy(iter, appender!(typeof(return))).data; } -ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) +ElementEncodingType!(ElementType!RoR)[] joinImpl(RoR)(RoR ror) if(isForwardRange!RoR && hasLength!RoR && isDynamicArray!(ElementType!RoR)) @@ -1327,7 +1356,7 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) return cast(RetType)result; } -ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) +ElementEncodingType!(ElementType!RoR)[] joinImpl(RoR)(RoR ror) if(isInputRange!RoR && ((isForwardRange!RoR && !hasLength!RoR) || !isForwardRange!RoR) && isDynamicArray!(ElementType!RoR)) @@ -1343,16 +1372,6 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) return result.data; } -//Verify Examples. -unittest -{ - assert(join(["hello", "silly", "world"], " ") == "hello silly world"); - assert(join(["hello", "silly", "world"]) == "hellosillyworld"); - - assert(join([[1, 2, 3], [4, 5]], [72, 73]) == [1, 2, 3, 72, 73, 4, 5]); - assert(join([[1, 2, 3], [4, 5]]) == [1, 2, 3, 4, 5]); -} - unittest { debug(std_array) printf("array.join.unittest\n"); @@ -1415,6 +1434,7 @@ unittest assert(join(cast(int[][])[]).empty); } + /++ Replace occurrences of $(D from) with $(D to) in $(D subject). Returns a new array without changing the contents of $(D subject).