The LARD Strings Package
LARD now incorporates a reasonably comprehensive strings manipulation
package. Unlike the previous arangement this now makes efficient use
of memory and supports string comparisons. Unfortunately these
improvements come at the cost of some additional complexity.
Declarations
String variables and parameters can be declared in the same way as any
other types:
foo(s1:var(string),s2:val(string)):expr(void)=(
a_string:var(string).
Initialisation and Finalisation
String variables and parameters have to be initialised and finalised
at the start and end of their scope. (In some future version of LARD
I hope to incorporate automatic type constructor and destructor
functions, which will remove the need for this and also for channel
initialisation. But this won't be for some time I'm afraid.)
Here are some examples showing the use of these initialisation and
finalisation functions:
foo1:expr(void)=(
s:var(string).
newstringvar(s);
...
endstringvar(s)
).
foo2(s:val(string)):expr(void)=(
newstringvalparam(s);
...
endstringvalparam(s)
).
foo3(s:var(string)):expr(void)=(
newstringvarparam(s);
...
endstringvarparam(s)
).
Returning a string as the result of a function
You may return a string as the result of a function. If the string is
a literal string, the result of a function call, or a variable or
parameter whose scope does not end at the end of this function you can
return it in the normal way by writing it as the last line of the
function, for example:
foo4:expr(string)=(
...
"hello"
).
foo5:expr(string)=(
...
"hello"@i2s(a)
).
s:var(string). /* a global variable, initialised somewhere */
foo6:expr(string)=(
...
s
).
foo7(s:val(string)):expr(void)=(
foo8:expr(string)=(
...
s
).
newstringvalparam(s);
...
endstringvalparam(s)
).
If the string is a local variable or parameter to the function you
need to call a special return function. In this case you must not
call the endstring function.
foo9:expr(string)=(
s:var(string).
newstringvar(s);
...
returnstringvar(s)
).
foo10(s:val(string)):expr(string)=(
newstringvalparam(s);
...
returnstringvalparam(s)
).
Strings in records and arrays
Strings that are elements of records and arrays are not excused from
the need for initialisation and finalisation. I haven't yet thought
about what will happen if you try to return such a structure as the
result of a function.
Furthermore since the assignment function is overloaded to do special
things for strings it is not allowed to do ordinary assignment on
records that contain stings. The best thing to do in this case is to
write an overloaded assignment function for your type that assigns
each element in turn. This will pick up the special string assignment
for string fields.
Assignment on arrays of strings is allowed as that is also provided by
an overloaded function.
Operations on strings
The following functions operate on strings:
:=
- The assignment function is overloaded to work with strings and
arrays of strings.
c2s
- Take a character and return a singleton string containing that character.
strindex
- Take a string and an integer and return the character at that
position in the string.
strlen
- Return the length of a string.
@
- Concatenate two strings.
i2s
- Take an integer and return a string representation of the number
in decimal.
i2hex
- Take an integer and return a string representation of the number
in hex.
strcmp
- Take two strings and return a value less than, equal to or greater
than zero if the first string is lexicographically less than, equal to
or greater than the second.
=, !=, <, >, <=, >=
- Relation operations overloaded to work on strings (implemented
using strcmp).
In addition there are functions in the io library that read and write
strings.