Fix assigning a ByCodeUnit to the String slice

std.algorithm.mutation copy is unable to copy a char range into a char array slice.
This commit is contained in:
Eugen Wissner 2017-07-19 07:58:48 +02:00
parent a0a28c76f7
commit 922c8bf7a3

View File

@ -933,8 +933,8 @@ struct String
* Slicing assignment. * Slicing assignment.
* *
* Params: * Params:
* R = Type of the assigned slice. * R = $(D_KEYWORD char).
* value = New value (single value, range or string). * value = Assigned character, range or string.
* i = Slice start. * i = Slice start.
* j = Slice end. * j = Slice end.
* *
@ -955,8 +955,9 @@ struct String
} }
body body
{ {
copy(value, this.data[i .. j]); auto target = opSlice(i, j);
return opSlice(i, j); copy(value, target);
return target;
} }
/// Ditto. /// Ditto.
@ -1435,13 +1436,13 @@ struct String
} }
/** /**
* Assigns a range or a string. * Slicing assignment.
* *
* Params: * Params:
* R = Value type. * R = $(D_KEYWORD char).
* value = Value. * value = Assigned character, range or string.
* *
* Returns: Assigned value. * Returns: Range over the string.
* *
* Precondition: $(D_INLINECODE length == value.length). * Precondition: $(D_INLINECODE length == value.length).
*/ */
@ -1451,18 +1452,40 @@ struct String
return opSliceAssign(value, 0, length); return opSliceAssign(value, 0, length);
} }
private unittest
{
auto s1 = String("Buttercup");
auto s2 = String("Cap");
s2[] = s1[6 .. $];
assert(s2 == "cup");
}
/// Ditto. /// Ditto.
ByCodeUnit!char opIndexAssign(const char value) pure nothrow @safe @nogc ByCodeUnit!char opIndexAssign(const char value) pure nothrow @safe @nogc
{ {
return opSliceAssign(value, 0, length); return opSliceAssign(value, 0, length);
} }
private unittest
{
auto s1 = String("Wow");
s1[] = 'a';
assert(s1 == "aaa");
}
/// Ditto. /// Ditto.
ByCodeUnit!char opIndexAssign(const char[] value) pure nothrow @safe @nogc ByCodeUnit!char opIndexAssign(const char[] value) pure nothrow @safe @nogc
{ {
return opSliceAssign(value, 0, length); return opSliceAssign(value, 0, length);
} }
private unittest
{
auto s1 = String("ö");
s1[] = "oe";
assert(s1 == "oe");
}
/** /**
* Remove all characters beloning to $(D_PARAM r). * Remove all characters beloning to $(D_PARAM r).
* *