Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 14230 - [REG2.067b2] std.array.join misses the first element which is empty string #3027

Merged
merged 1 commit into from Feb 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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