diff options
| author | Eugen Wissner <belka@caraus.de> | 2017-07-19 07:58:48 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2017-07-19 07:58:48 +0200 |
| commit | 922c8bf7a3a2395a62052a8b967f61f6e2afa030 (patch) | |
| tree | 0956ba80b63cf6fa976ed04b41bf0085f9bd4712 | |
| parent | a0a28c76f75f16b0adbc7caae167fe87e3ed99e8 (diff) | |
| download | tanya-922c8bf7a3a2395a62052a8b967f61f6e2afa030.tar.gz | |
Fix assigning a ByCodeUnit to the String slice
std.algorithm.mutation copy is unable to copy a char range into a char array slice.
| -rw-r--r-- | source/tanya/container/string.d | 39 |
1 files 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). * |
