I searched in Turbo Pascal 6 the complete reference for "rset" and "lset" - statements of QB 4.5 for justifying strings inside their variables bytes - equivalents in Turbo Pascal and found no result as if Turbo Pascal doesn't need these statements or Turbo Pascal is always justifying strings to left. Is this true?
-
`Turbo Pascal is always justifying strings to left` - in what context, when writing out to console or text file? – 500 - Internal Server Error Jan 04 '21 at 16:45
-
I'm searching for justifying in string variables bytes and files – Ahmed Crow Jan 04 '21 at 16:48
-
Left-padding a string with spaces to a certain width can be done with something like `function LeftPad(const s: string; len: Integer): string; begin Result := s; while length(Result) < len do Result := ' ' + Result; end;` (line-breaks not included). – 500 - Internal Server Error Jan 04 '21 at 16:53
-
But, we can't do it actually in bytes with moving the string to the last bytes of the variable or file buffer, we need to use spaces. – Ahmed Crow Jan 04 '21 at 18:04
-
I'm afraid I do not understand your comment. Can you rephrase? – 500 - Internal Server Error Jan 04 '21 at 20:44
-
I mean in QB 4.5 "rset" statement for example is justifying the string value to the most right bytes in the variable, I think it doesn't put spaces in left bytes, but maybe null character or something like that. – Ahmed Crow Jan 05 '21 at 17:07
1 Answers
In Turbo Pascal 6.0 the string
type reserves 256 bytes of memory.
s: string;
The memory layout is that the first byte (value 0..255) indicates the length of the string. The following bytes hold the characters, always left aligned.
A variation of the previous is that you can declare string variables with a maximum length, like f.ex.
s: string[10];
This example will reserve 11 bytes of memory. Again the first byte indicates length of the actual string (value in this case 0..10).
The memory content after a statement like
s := 'test';
will be
4, 't', 'e', 's', 't', #0, #0, #0, #0, #0, #0
There is no statement to modify the character allocation to right align the character data (like RSET in QBasic).
However, in theory, you may precede a string with a series of null characters, e.g s :=#0#0#0#0#0#0+'test';
, to achieve a similar effect, but the length will become the length of the string variable.
10, #0, #0, #0, #0, #0, #0, 't', 'e', 's', 't'
The null chars are in this case part of the string, and this might be problematic. A null char might be taken for the end of the string. Also, as the null chars might be omitted by e.g. a printing procedure in some case and not in other cases. This could lead to misalignment of data in printouts, or other.

- 20,312
- 8
- 37
- 54