dChar Error
Salih Dincer
salihdb at hotmail.com
Fri Dec 30 04:43:48 UTC 2022
Hi All,
What is causing the error in the code snippet below?
```d
void replaceRight(S)(ref S[] str, S[] slc)
{
size_t len1 = slc.length,
len2 = str[len1..$].length;
assert(len1 == len2);
str[len1..$] = slc;
}
import std.stdio;
void main()
{
// example one:
char[] str1 = "cur:€_".dup;
str1.length.write(": "); // 8:
str1[4..$].writefln!"[%s]"; // [€_]
str1.replaceRight("$ _".dup);
str1.writefln!"[%s]"; // [cur:$ _]
// example two:
dchar[] str2 = cast(dchar[])"cur:€_"d;
str2.length.write(": "); // 6:
str2[4..$].writefln!"[%s]"; // [€_]
str2.replaceRight(cast(dchar[])"$ _"d);
str2.writefln!"[%s]"; // Error--^
} /* Prints:
8: [€_]
[cur:$ _]
6: [€_]
Error: program killed by signal 11
*/
```
Why does replaceRight() work fine with a char array, but not a
dchar array? Whereas, rvalue and lvalue lengths are equal to
each other!
SDB at 79
More information about the Digitalmars-d-learn
mailing list