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, string[int]
- Take a string and an integer and return the character at that
position in the string. A negative index specifies a position relative
to the end of the string: strindex ("ABCDE", 0) is A,
strindex ("ABCDE", -2) is D. strindex is a prefix
form of the bracketted string indexing operator: strindex (s, i)
== s[i].
substr, string[range(int)]
- Return a substring taken from the argument string. substr
takes a pair of indices and returns a string with characters between
indices lowIndex and highIndex - 1 (inclusive). string[range(int)]
can be called with indices in any order and returns a string between
those indices (inclusive).
strlen
- Return the length of a string.
@
- Concatenate two strings. The @ operator is also overloaded
to concatenate and string and an integer: "Hello " @ 10
returns "Hello 10". The integer can be either the left or
right argument.
b2s
- Take a boolean and return one of the strings "true" or "false"
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).
strchr, strrchr, strdchr, strstr, strrstr, strdstr
- String search routines. Searching for characters or strings in
either direction and starting at arbitrary indices.
In addition there are functions in the io library that read and write
strings.