xxxxxxxxxx
module test;
import std : writeln, writefln, assumeUnique;
import std.conv : to;
void main()
{
string str = "abc;def;ab".dup; // allocates the string in the heap
char* w = cast(char*)str;
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
// problem is with this next line:
// the line works fine using DMD on windows - but crashes if using LDC on windows
// On Linux, both DMD and LDC -> sigsegv
str[i] = ch2;
// seems simple enough
// - just replace the char currently in element str[i]) with 'X'
}
}
return assumeUnique(str[0..len]); // you can use str[0..len].idup but it allocates although is safe
}