Skip to content

Commit

Permalink
Merge pull request #3027 from sinkuu/fix_14230
Browse files Browse the repository at this point in the history
Fix Issue 14230 - [REG2.067b2] std.array.join misses the first element which is empty string
  • Loading branch information
MartinNowak committed Feb 28, 2015
2 parents cf6fee9 + 43e56d5 commit b43a7b8
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions std/array.d
Expand Up @@ -1644,13 +1644,13 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep)

auto result = (() @trusted => uninitializedArray!(RetTypeElement[])(length))();
size_t len;
foreach(e; ror.front)
emplaceRef(result[len++], e);
ror.popFront();
foreach(r; ror)
{
if (len) // if not first time thru loop
{
foreach(e; sepArr)
emplaceRef(result[len++], e);
}
foreach(e; sepArr)
emplaceRef(result[len++], e);
foreach(e; r)
emplaceRef(result[len++], e);
}
Expand All @@ -1671,6 +1671,12 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep)
}
}

unittest // Issue 14230
{
string[] ary = ["","aa","bb","cc"]; // leaded by _empty_ element
assert(ary.join(" @") == " @aa @bb @cc"); // OK in 2.067b1 and olders
}

/// Ditto
ElementEncodingType!(ElementType!RoR)[] join(RoR, E)(RoR ror, E sep)
if(isInputRange!RoR &&
Expand Down Expand Up @@ -1710,10 +1716,12 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, E)(RoR ror, E sep)


size_t len;
foreach(e; ror.front)
emplaceRef(result[len++], e);
ror.popFront();
foreach(r; ror)
{
if (len)
emplaceRef(result[len++], sep);
emplaceRef(result[len++], sep);
foreach(e; r)
emplaceRef(result[len++], e);
}
Expand All @@ -1735,6 +1743,12 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, E)(RoR ror, E sep)
}
}

unittest // Issue 14230
{
string[] ary = ["","aa","bb","cc"];
assert(ary.join('@') == "@aa@bb@cc");
}

/// Ditto
ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror)
if(isInputRange!RoR &&
Expand Down

0 comments on commit b43a7b8

Please sign in to comment.