Re: A debug class has started

2021-12-14 Thread forkit via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 08:07:43 UTC, WebFreak001 wrote: The best way would be not doing this at all - when you manipulate strings/arrays in D you can do so by just assigning the elements like this: ```d immutable(char)[] replaceChar(char[] str, char ch1, char ch2) { for (ulong

Re: A debug class has started

2021-12-14 Thread WebFreak001 via Digitalmars-d-learn
On Monday, 13 December 2021 at 22:43:14 UTC, forkit wrote: [...] //char* w = cast(char*)str; // nope. a pointer to a string constant is // (supposed to be) immutable, so expect undefined behaviour. note: //char* w = cast(char*)str.toStringz; // also

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 10:43:14PM +, forkit via Digitalmars-d-learn wrote: > On Monday, 13 December 2021 at 21:13:25 UTC, H. S. Teoh wrote: > > > > What you should be doing is: > > > > return to!string(str[0 .. len]); > > > > Or just: > > > > return str[0 .. len].idup; [...] >

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 21:13:25 UTC, H. S. Teoh wrote: What you should be doing is: return to!string(str[0 .. len]); Or just: return str[0 .. len].idup; T oh.. so many different ways...(to both produce the same bug, and also to produce the correct output). ...

Re: A debug class has started

2021-12-13 Thread Stanislav Blinov via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:58:42 UTC, forkit wrote: immutable(char)[] replaceChar(char* str, ulong len, char ch1, char ch2) //snip return to!(immutable(char)[])(str); } You're calling a `to` on a char pointer, which, ostensibly, would look for null terminator. Which there may

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 08:58:42PM +, forkit via Digitalmars-d-learn wrote: [...] > 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]",

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 08:47:12PM +, forkit via Digitalmars-d-learn wrote: > On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: > > On Mon, Dec 13, 2021 at 08:04:24PM +, forkit via Digitalmars-d-learn > > wrote: [...] > > > (this produces an unpredictable result??) > > > char*

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: On Mon, Dec 13, 2021 at 08:04:24PM +, 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

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 20:28:26 UTC, H. S. Teoh wrote: On Mon, Dec 13, 2021 at 08:04:24PM +, 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

Re: A debug class has started

2021-12-13 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 13, 2021 at 08:04:24PM +, 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) > >

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
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; (but this seems to

Re: A debug class has started

2021-12-13 Thread WebFreak001 via Digitalmars-d-learn
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

Re: A debug class has started

2021-12-13 Thread drug via Digitalmars-d-learn
On 13.12.2021 14:26, ag0aep6g wrote: On 13.12.21 12:09, drug wrote: 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

Re: A debug class has started

2021-12-13 Thread ag0aep6g via Digitalmars-d-learn
On 13.12.21 12:09, drug wrote: 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

Re: A debug class has started

2021-12-13 Thread drug via Digitalmars-d-learn
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

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
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 ;-)

Re: A debug class has started

2021-12-13 Thread forkit via Digitalmars-d-learn
On Monday, 13 December 2021 at 09:49:05 UTC, forkit wrote: oh... Windows - dmd version is 2.098.0-dirty - ldc2 version is 1.28 (based on dmd v2.098.0) Linux - dmd version is 2.098 - ldc2 version is 1.20.1 (based on dmd v2.090.1)