Home | Conferences | Links | Reference | About | Search |
|
Dynamic string library - using User Defined OperatorsJames 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. OverviewThe library first defines a new data type -- Declaration and creation of stringsTo 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") Assignment and copying stringsAssigning variables of type DSTRING x,y: SEQ x := new.dstring("Hello World*c*n") y := xboth x and y now refer to the same string -- containing
"Hello world" . This means that if an operation is
performed on string x , string y will also have
been changed. To make an independent copy of a string, use copy.dstring :
DSTRING x,y: SEQ x := new.dstring("Hello World*c*n") y := copy.dstring(x)x and y are now independent. Deallocation of stringsStrings 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''. Conversion to occam stringsThere will often be the need to convert a DSTRING a: [50]BYTE buf: SEQ a := new.dstring("Hello") oc.string(a, buf) -- buf now contains "Hello\0\0\0\0..." (in C speak) Boolean comparison operatorsThe 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. Reference vs Lexical equivalenceThere 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 Length functionThe 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 printing functionA 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 Installation and useThe 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 : BugsPlease report bugs to: ofa-bugs@kent.ac.ukalthough please note that we cannot guarantee support. About this document ...Dynamic string library - an example of User Defined Operators This document was originally generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds. 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