The African Chief wrote:
No, it shouldn't. Maybe we are at cross purposes here. My original meaning was: if I write "str1 := str2", that is precisely what I should get - no more, no less. I should get in "str1" the exact number of characters that are in "str2", in the exact order - no more, no less. If an optimiser gives me that, I don't really care what else it does ...
Exactly. The number of characters is given in the length. Anything beyond the length (such as the #0 terminator in your first version) is undefined.
Okay, I understand that now ;-). However, this solution will not work if there is a maximum length for the string, and the string that is passed to the function has already reached that length.
True. For (long) string, we can solve this by implicitly making the schema bigger by 1 char than declared, but for short strings, this is not possible without breaking BP compatibility, so we need to copy them. (OTOH, it's not so bad there, since they can be only 255 chars long, so there isn't too much overhead.)
Str2pChar := StrNew ( StrCopy ( @s [1], @s[1] ) );
I don't understand this line. What do you expect "StrCopy ( @s [1], @s[1] )" to do? AFAICS, it copies a string into itself!?
More or less. Since it is already itself, nothing really should happen, but the side effect is that it provides the input for StrNew to work with,
You could just write "Str2pChar := StrNew ( @s[1] );" which gives the same (wrong) result in BP.
OTOH, if you're using the strings unit anyway, is there anything wrong with StrPCopy?
If I used this routine in the system unit, I would not be using the strings unit there - but I can import "strcpy" from the C library, and call it "StrCopy".
Perhaps something like (NOT tested):
Function Str2pChar ( s : String ) p : pChar; begin GetMem (p, Length(s) + 1); Move (s[1], p^, Length(s)); p[Length(s)] := #0 end;
-- Frank Heckenbach, Erlangen, Germany heckenb@mi.uni-erlangen.de http://home.pages.de/~fjf/links.htm