On 12.10.21 10:19, jfondren wrote:
```d
/+ Unfortunately, this isn't reliable.
  We could make this work if string literals are put
  in read-only memory and we test if s[] is pointing into
  that.

  /* Peek past end of s[], if it's 0, no conversion necessary.
  * Note that the compiler will put a 0 past the end of static
  * strings, and the storage allocator will put a 0 past the end
  * of newly allocated char[]'s.
  */
  char* p = &s[0] + s.length;
  if (*p == 0)
  return s;
  +/
```
[...]
As for whether it's a necessarily a good idea to patch toStringz, I'd worry that

1. someone will slice a string literal and pass the test while not having NUL where it's expected

The (commented-out) code checks if the NUL is there. Just make sure that it's also read-only.

2. people are probably relying by now on toStringz always allocating, to e.g. safely cast immutable off the result.

It doesn't matter if the result is freshly allocated. Casting away immutable is only allowed as long as you don't use it to actually change the data (i.e. it remains de-facto immutable).

Reply via email to