Nevertheless, I consider it a perversion and illogical..... If "Copy" can NEVER be used for PCHAR, I can understand that, then it's easier to remove it for that type, otherwise it's a bug...
A
PChar in a
Copy is implicitly converted to a
AnsiString. You would see this if instead of doing a
Writeln of the
Copy's result you'd try to assign it to a
PChar variable instead which will fail. And due to this you must use 1-based indexing as mentioned in the documentation (and yes, this behavior is compatible with Delphi).
And the incomprehensible things with this function are still there.... Maybe there is a problem with its implementation for all strings after all?
There is nothing incomprehensible about this function. You just need to keep in mind that start indices smaller than 1 are clamped to 1 and counts that reach behind the length of the string are clamped accordingly as well.
The "bug", if it can be called that, is the conversion pchar->string silently done by the compiler but, it's probably needed for Delphi compatibility.
There is {$modeSwitch pCharToString‑} if you don’t like that. However, I can’t observe any difference. Does this mode switch have any effect? Show me some code.
As far as I can see that modeswitch isn't referenced anywhere inside the compiler. I don't know if it ever was or what it was originally intended for...
@beria,
What you showed in your example, quite likely is related to the problem discussed in the following thread: https://forum.lazarus.freepascal.org/index.php/topic,48133.msg346158.html#msg346158
You are right. But I still think it's a mistake, even if it's done for Delphi compatibility, it doesn't have to work that way outside of that compatibility...
It's less error prone to make the intrinsic behave the same across all modes. Also
now this behavior is also required for backwards compatibility.
I'm not saying it's a comfortable situation: now that a string's element zero is no longer directly equivalent to the length the distinction between Pascal-style strings and dynamic arrays is very unfortunate: however it's there, it's not likely to change, and it's best keeping your use of Pascal- and C-style strings separate.
One can always use
$ZeroBasedStrings On (though that will not change the behaviour of
Copy).