Russell Whitaker wrote:
and I will work. However, I think that the correct way is:
setlength(s, s.capacity); fillchar(s[1..length(s)], length(s), ' ');
shouldn't that be fillchar(s[1], length(s), ' '); ?
That was the first alternative Waldek gave (WRT the 1st parameter). It's formally a bit cleaner, though currently (and as I wrote, I don't plan to change it), the second alternative works as well and does the same.
In the future we may teach the compiler that fillchar is special, so it applies to it relaxed range checking rules, but that would require non-trivial changes in the compiler.
perhaps something like: if ( start+count ) GT s.capacity then out-of-range-error if ( start+count ) GT length(s) then setlength( s, start+count)
You mean for FillChar? Well, FillChar is independent of SetLength. In fact, it's a low-level routine that doesn't know about strings at all and can be applied to any kind of data.
Also, if the fillchar line is written as fillchar( s, count, Afillchar ); it should default to fillchar( s[1], count, Afillchar );
No, it shouldn't. Again, FillChar is low-level, and FillChar (s, ...) will just fill the memory starting at s. That's usually pointless and dangerous, just as many other applications of FillChar are (such as applying it to file types or records containing various types, ...).
current behavior fillchar( s, ... overwrites length (and capacity?) fields.
Yes, that's what it's supposed to do.
Therefore, such low-level routines should be used with great caution.
BTW, a cleaner way (though involving a runtime function call) to achieve the same is:
uses GPC;
[...]
s := StringOfChar (' ', s.Capacity);
Perhaps you were really looking for something like this from the start.
Frank