Dynamic string library - using User Defined Operators
James Moores, Computing Lab, University of Kent, UK
26th March 1997
This document is intended to provide a guide to using the dynamic string library supplied as an example usage of the new user defined operators.
Obvious warning: The occam compiler cannot check the usage and aliasing of these dynamically created data types -- they live in the "C" world.
The library first defines a new data type --
To declare a new string, all that is required is:
DSTRING x:This variable, however does not refer to anything at the moment, so a function must be called to create a string (allocate its memory dynamically) which takes an array of bytes (a standard occam string) and returns a reference to the string created:
DSTRING x: SEQ x := new.dstring("Hello World*c*n")
Assigning variables of type
DSTRING x,y: SEQ x := new.dstring("Hello World*c*n") y := xboth
DSTRING x,y: SEQ x := new.dstring("Hello World*c*n") y := copy.dstring(x)x and y are now independent.
Strings can be deallocated (destroyed and the memory returned to the heap) using:
DSTRING a: SEQ a := new.dstring("Hello") delete.string(a)which should always be done when strings are no longer useful, or the variables holding string pointers are about to go out of scope - otherwise the memory will be ``lost''.
There will often be the need to convert a
DSTRING a: BYTE buf: SEQ a := new.dstring("Hello") oc.string(a, buf) -- buf now contains "Hello\0\0\0\0..." (in C speak)
The library provides all of the boolean comparison operators on strings. Comparison is done on the basis of lexical equivalence:
DSTRING x,y: SEQ x := new.dstring("aaaaaa") y := new.dstring("aaaaab") IF (exp)
where the exp and its values for the above
they are based on the result of the call strcmp(s1,s2) in the C world.
There is a reference equivalence operator
DSTRING a,b,c: SEQ a := new.dstring("Hello") b := new.dstring("Hello") -- could be copy.dstring(a) c := a -- c is now an alias for a IF (exp) ...
where exp and its values for the above program are:
The concatenation operator is
DSTRING a,b,c: SEQ a := new.dstring("Hello") b := new.dstring(" world") c := a ++ b
DSTRING a,b: SEQ a := new.dstring("Hello") b := new.dstring(" world") a := a ++ b
This is legal occam, but the string that
DSTRING a,b,c,d: SEQ a := new.dstring("Hello") b := new.dstring(" world") c := new.dstring(" at large") d := (a ++ b) ++ c
The resulting intermediate string reference from evaluating
INT length: DSTRING a: SEQ a := new.dstring("Hello") length := dstring.length(a) -- length now equals 5
it will return the length as defined by the C function strlen(s), which is "the number of characters in s, not including the null-terminating character.".
A simple printing function is provided for convenience:
PROC test (CHAN OF BYTE in,out,err) DSTRING a: SEQ a := new.dstring("Hello") print.dstring(out,a)
which will place the string referred to by
The example directory examples/udo contains the source for the library:
These can be built using the given Makefile - just type ``make''. This will compile and build a native code library libdstring.a and an occam module dstring.tco.
If a ``make install'' is done, the library elements will be copied into the default KROC library area and in that case programs can be compiled using:
kroc -X2 prog.occ -ldstring
the -X2 flag turns on the experimental user defined operators.
Note that if you don't want to install the library you will have to use the -L flag of kroc to indicate the directory containing the library. The -L flag can be dropped if the environment variable OCSEARCH is set to include this directory.
At the start of programs using this library you must include the
#INCLUDE "dstring.inc" #INCLUDE "dstring.oci" #USE "dstring.tco" PROC main.code (...) ... use DSTRING here :
Please report bugs to:
email@example.com please note that we cannot guarantee support.
Dynamic string library - an example of User Defined Operators
The command line arguments were:
The document was then edited by Dave Beckett to be better HTML.
Page last modified on 7th July 2003
Pages © WoTUG, or the indicated author. All Rights Reserved.
Comments on these web pages should be addressed to: www at wotug.org