On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote:
On Mon, Dec 13, 2021 at 08:04:24PM +0000, forkit via
Digitalmars-d-learn wrote:
On Monday, 13 December 2021 at 12:06:53 UTC, WebFreak001 wrote:
>
> You should really use `.dup` if you want to mutate your
> string. (You would need to duplicate anyway if you don't
> want an unsafe cast)
(this produces an unpredictable result??)
char* w = cast(char*)str.dup;
Shouldn't you be using:
char* w = str.dup.ptr;
instead??
T
so here are all the possible options I've tried. only 2 of these
actually produce the expected result.
// ------
module test;
import std : writeln, writefln;
import std.conv : to;
import core.stdc.string : strdup;
import std.string : toStringz;
void main()
{
string str = "abc;def;ab";
//char* w = cast(char*)str; // NOPE! A string constant is
immutable, so expect undefined behaviour.
char* w = strdup(cast(char*)str); // ok
//char* w = cast(char*)str.toStringz; // also ok
// all these below result in an extra character from
'somewhere' appearing in the writeln output
//char* w = cast(char*)str.dup; // nope
//char* w = str.dup.ptr; // nope
//char* w = &dup(cast(const(char)[])str)[0]; // nope
writeln(replaceChar(w, str.length, ';', 'X'));
}
immutable(char)[] replaceChar(char* str, ulong len, char ch1,
char ch2)
{
for (ulong i = 0; i < len; i++)
{
if (str[i] == ch1)
{
writefln("Found %c at str[%d]", ch1, i); // fine
str[i] = ch2;
}
}
return to!(immutable(char)[])(str);
}
// ----------------