Skip to content

Commit

Permalink
Merge pull request #5128 from BBasile/issue-16342
Browse files Browse the repository at this point in the history
fix issue 16342 - add std.algorithm.mutation.fill fallback for mutable strings
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
  • Loading branch information
dlang-bot authored Mar 13, 2017
2 parents 9872247 + bab801c commit c6aa7b8
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions std/algorithm/mutation.d
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ T2=$(TR $(TDNW $(LREF $1)) $(TD $+))
module std.algorithm.mutation;

import std.range.primitives;
import std.traits : isArray, isBlitAssignable, isNarrowString, Unqual;
import std.traits : isArray, isBlitAssignable, isNarrowString, Unqual, isSomeChar;
// FIXME
import std.typecons; // : tuple, Tuple;

Expand Down Expand Up @@ -543,8 +543,9 @@ See_Also:
$(LREF uninitializedFill)
$(LREF initializeAll)
*/
void fill(Range, Value)(Range range, Value value)
if (isInputRange!Range && is(typeof(range.front = value)))
void fill(Range, Value)(auto ref Range range, auto ref Value value)
if ((isInputRange!Range && is(typeof(range.front = value)) ||
isSomeChar!Value && is(typeof(range[] = value))))
{
alias T = ElementType!Range;

Expand Down Expand Up @@ -573,6 +574,69 @@ if (isInputRange!Range && is(typeof(range.front = value)))
assert(a == [ 5, 5, 5, 5 ]);
}

// issue 16342, test fallback on mutable narrow strings
@safe unittest
{
char[] chars = ['a', 'b'];
fill(chars, 'c');
assert(chars == "cc");

char[2] chars2 = ['a', 'b'];
fill(chars2, 'c');
assert(chars2 == "cc");

wchar[] wchars = ['a', 'b'];
fill(wchars, wchar('c'));
assert(wchars == "cc"w);

dchar[] dchars = ['a', 'b'];
fill(dchars, dchar('c'));
assert(dchars == "cc"d);
}

@nogc @safe unittest
{
const(char)[] chars;
static assert(!__traits(compiles, fill(chars, 'c')));
wstring wchars;
static assert(!__traits(compiles, fill(wchars, wchar('c'))));
}

@nogc @safe unittest
{
char[] chars;
fill(chars, 'c');
assert(chars == ""c);
}

@safe unittest
{
shared(char)[] chrs = ['r'];
fill(chrs, 'c');
assert(chrs == [shared(char)('c')]);
}

@nogc @safe unittest
{
struct Str(size_t len)
{
private char[len] _data;
void opIndexAssign(char value) @safe @nogc
{_data[] = value;}
}
Str!2 str;
str.fill(':');
assert(str._data == "::");
}

@safe unittest
{
char[] chars = ['a','b','c','d'];
chars[1 .. 3].fill(':');
assert(chars == "a::d");
}
// end issue 16342

@safe unittest
{
import std.conv : text;
Expand Down

0 comments on commit c6aa7b8

Please sign in to comment.