I am not sure if that is
- documented behaviour
From docs
AnsiStrings: They are reference counted and are
guaranteed to be null terminated.
Result:
ShortString - does not null terminated. Cannot be casted to PChar.
AnsiString - always null terminated. Can be casted to PChar.
UnicodeString - always null terminated. Can be casted to PUnicodeChar.
WideString (it differs from UnicodeString in Windows) - always null terminated. Can be casted to PWideChar.
When cast strings to PChar, you also need to be careful for empty strings. An empty string is equal to nil, and PChar(S) acts as a function, which in this case returns a pointer to an empty string that ends with #0, but not nil. Some external functions expect nil for empty strings, in this case, instead of PChar(S), you need to use Pointer(S).
Any strings always know their length, and even if they contain #0 inside, they are processed correctly. But if you cast to PChar and use functions with null terminated strings, then the processing will be incorrect.
Use the converted PChar value only if the original string does not change and remains in scope.