Irie Pascal supports fixed length strings as defined in Standard Pascal (i.e. fixed length strings, which are simply packed arrays of char, having a lower bound of one and an upper bound of two or greater). Fixed length strings have special properties in addition to the properties they share with other arrays. Fixed length strings, unlike other arrays, can be compared with the relational operators. Also fixed length strings, unlike other arrays, can be read from and written to text files as a unit using the built-in procedures read, readln, write, and writeln.
As an extension to Standard Pascal, Irie Pascal supports variable length strings defined using the built-in types string and cstring. Variable length strings define sequences of characters. The maximum number of characters allowed in a variable length string is fixed and is specified when the string type is defined.
Variable length strings defined with string are stored as length prefixed strings (i.e. the actual number of characters in the string is stored in a byte or integer in front of the string). A byte is used to store the actual length of the string, if the maximum number of characters allowed in the string is small enough to be stored in a byte (i.e. <= 255). An integer is used to store the actual length of the string, if the maximum number of characters allowed in the string is too big to fit in a byte (i.e. > 255).
Variable length strings defined with cstring are stored as NULL-terminated strings.
For example to create a variable length string type called name with a maximum length of 80 use
name = string;
name = string(80);
To create a variable length string type called phone with a maximum length of 255 use
phone = string;
phone = string;
phone = string(255);
In most case it is best to use the built-in type string to create variable length string types. However variable length string types defined with cstring are useful when the values of a variable length string are going to be passed to or from external functions/procedures written in the C programming language. For example this simple hello world program uses two variables of cstring types to pass values to the Windows API function MessageBox.
program winhello; (*$I winuser.inc *) const NULL_HANDLE = 0; CR = 13; var csText, csCaption : cstring; RetVal : integer; begin csText := 'Hello world!!'+chr(CR)+'Press Cancel when done'; csCaption := 'Irie Pascal Windows Hello World Program'; repeat RetVal := MessageBox(NULL_HANDLE, addr(csText), addr(csCaption), MB_OKCANCEL + MB_ICONEXCLAMATION); until RetVal = IDCANCEL; end.
The syntax for defining new variable string types is given below (see array types for the sytax for defining new fixed length strings):
(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):
string-type = pascal-string-type | c-string-type c-string-type = 'cstring' [ max-string-length ] max-string-length = '[' integral-constant ']' | '(' integral-constant ')' pascal-string-type = 'string' [ max-string-length ]
where max-string-length is between 1 and 1048576, and specifies the maximum length of the string. If max-string-length is not specified, the default maximum length (i.e. 255) is used.