From 922c8bf7a3a2395a62052a8b967f61f6e2afa030 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 19 Jul 2017 07:58:48 +0200 Subject: [PATCH] Fix assigning a ByCodeUnit to the String slice std.algorithm.mutation copy is unable to copy a char range into a char array slice. --- source/tanya/container/string.d | 39 ++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d index b17ac06..1cbc169 100644 --- a/source/tanya/container/string.d +++ b/source/tanya/container/string.d @@ -933,8 +933,8 @@ struct String * Slicing assignment. * * Params: - * R = Type of the assigned slice. - * value = New value (single value, range or string). + * R = $(D_KEYWORD char). + * value = Assigned character, range or string. * i = Slice start. * j = Slice end. * @@ -955,8 +955,9 @@ struct String } body { - copy(value, this.data[i .. j]); - return opSlice(i, j); + auto target = opSlice(i, j); + copy(value, target); + return target; } /// Ditto. @@ -1435,13 +1436,13 @@ struct String } /** - * Assigns a range or a string. + * Slicing assignment. * * Params: - * R = Value type. - * value = Value. + * R = $(D_KEYWORD char). + * value = Assigned character, range or string. * - * Returns: Assigned value. + * Returns: Range over the string. * * Precondition: $(D_INLINECODE length == value.length). */ @@ -1451,18 +1452,40 @@ struct String return opSliceAssign(value, 0, length); } + private unittest + { + auto s1 = String("Buttercup"); + auto s2 = String("Cap"); + s2[] = s1[6 .. $]; + assert(s2 == "cup"); + } + /// Ditto. ByCodeUnit!char opIndexAssign(const char value) pure nothrow @safe @nogc { return opSliceAssign(value, 0, length); } + private unittest + { + auto s1 = String("Wow"); + s1[] = 'a'; + assert(s1 == "aaa"); + } + /// Ditto. ByCodeUnit!char opIndexAssign(const char[] value) pure nothrow @safe @nogc { return opSliceAssign(value, 0, length); } + private unittest + { + auto s1 = String("รถ"); + s1[] = "oe"; + assert(s1 == "oe"); + } + /** * Remove all characters beloning to $(D_PARAM r). *