Peter Gerwinski wrote:
s:='' should do the same. (BP optmizes this to just setting the length to 0, I hope gpc does as well.)
It does. (But better check it, too - I already was wrong once with statements of this type.;-)
Yes, it does. I checked it. It even does "s:='...'" where '...' is a constant string with length<=14 without any loops or calls.
I think it should (at least with {$X+} or such), because it's the only way to translate some kinds of procedures -- unless one wants to limit such procedures to borland-like short strings.
Long strings: "S.length:= ...". Short strings: "S [ 0 ]:= chr ( ... )".
^^^^^^ ...or rather CurrentLength, as you said before. Then perhaps it would be useful to allow the same syntax also for ShortStrings (additionally), just as if ShortString[n] was declared as:
record case boolean of false:(CurrentLength:byte); true:(string:array[0..n] of char); end;
Also, it could be good to provide a (read-only) "pseudo field" Capacity. (BTW: LongStrings' Capacity is always read-only, isn't it?) BP 7.0 allows this via High(s) -- is something like BP's High and Low functions (to give the lower and upper bounds of arrays and subrange types) present or planned for gpc? Then the compiler could just translate "s.Capacity" to "High(s)" in the case of ShortStrings.
These two things would make ShortStrings more similar to (and often exchangeable with) LongStrings, without giving up BP compatibility.
The question is, should the #0 be added for short strings as well? If Length=Capacity, it's impossible, but otherwise???
We could increase the space allocated for a short string by 1, but this would break compatibility to UCSD and Borland Pascal. So better ignore this and live with "String [ 255 ]" having only a capacity of 254 in fact.
No, I guess this could break some programs, too.
BTW: The problem is also there for Length=Capacity<=255: either we'd have Sizeof(String[n])=n+2 or (Capacity of String[n])=n-1 -- both could lead to problems. So, perhaps better forget about the #0 with ShortStrings, and add it when converting to other strings...
Isn't it good when non-standard compilers (BP) use non-standard syntax? :-)
I would not call this "non-standard" since this - like many other things, e.g. Units - was not invented by Borland but by UCSD.
But UCSD was non-standard, too, wasn't it? Just out of curiosity, it doesn't really matter to me.