On Monday, 13 December 2021 at 11:09:18 UTC, drug wrote:
>On 13.12.2021 13:49, forkit wrote:
>On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote:
>....
char* w = cast(char*)str.toStringz; // this seems to be the solution
class has ended ;-)
That's because str
is initialized by a literal and you can not change it by definition. When you call toStringz
it duplicates that literal (adding terminating zero at the end) and the duplicate is mutable. I would recommend do not use toStringz
and just make duplicate of the literal - https://run.dlang.io/is/vaosW0
important: toStringz may do a copy with the current implementation but nothing in the docs states it actually does so. In fact there is commented out code where it in the past just dereferenced the memory after the string and checked if it was 0.
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)
pro-tip for bugs like this: just slap @safe:
at the start of every file, the compiler will tell you everything that is risky and the bug will 9/10 times just sort itself out by fixing what the compiler complains about. (just recently helped someone with this again, was a 3 minute fix for a big code-base where manually searching the issue would have taken much longer)