| # Section 8.16.1.4: $FSTRINGS |

###### abstract class $FSTRINGS < $IS_EQ, $ELT, $HASH

#### Formal Definitions

This abstract class defines a state component which is a set of all instantiations of objects of any class sub-typing from this class in addition to the vdm model types used wherever this class name is used. Note that SAME has to be an instantiated class, not an abstract one.

types
SAME = object_type ;
$FSTRINGS = set of object_type
state
multi : $FSTRINGS
inv multi_types ==
forall obj in set multi_types & sub_type($FSTRINGS,obj)

NOTE | See the important note about vdm state in the notes on vdm-sl usage in this specification.

This abstract class defines the concept of a mutable string of any kind which may be imported or exported from/to the program environment. Because it is mutable it is possible to create and manipulate strings of this kind with lower overheads than for a corresponding immutable string.

#### size

This feature returns the size of the string as a count of elements contained.

size | : CARD

##### Formal Signature

size(self : SAME) res : CARD

##### Pre-condition

Note that in contrast to a pre-condition written in Sather source code, in vdm-sl the type SAME is not an optional type and therefore self must exist, leading to the vacuous pre-condition.

pre true

##### Post-condition

post res = card dom self

This feature returns the number of elements in the string.

NOTE | This is not necessarily directly related to storage size occupied (see the definition of the octets feature below).

#### octets

This feature provides the facility to determine how many storage octets are occupited by the string, irrespective of kind or size of string element.

octets | : CARD

##### Formal Signature

octets(self : SAME) res : CARD

##### Pre-condition

Note that in contrast to a pre-condition written in Sather source code, in vdm-sl the type SAME is not an optional type and therefore self must exist, leading to the vacuous pre-condition.

pre true

##### Post-condition

This post-condition is much weaker than that for the size feature. This arises from the necessarily implementation-defined (possibly even dynamic) relation between element count and storage octets occupied.

post res >= card dom self

This feature returns the number of storage units (octets) occupied by the string.

#### array_ptr

This feature is provided to enable a program to use external environment services in relation to a string. It returns an environment-specific reference to the string.

array_ptr | : REFERENCE

##### Formal Signature

array_ptr(self : SAME) res : REFERENCE

##### Pre-condition

Note that, in contrast to a pre-condition written in Sather source code, in vdm-sl the type SAME is not an optional type and therefore self must exist, leading to the vacuous pre-condition.

pre true

##### Post-condition

Since the result of this feature is implementation-dependent no post-condition can be specified (except to state here that the reference is required to be a valid handle for self.

This feature provides an environment-defined handle for the contents of self (for use in external input or output).

#### head

This feature is provided to enable a program to manipulate independently a sub-string starting at the beginning (head) of the string.

head | (
| cnt : CARD
| ) : SAME

##### Formal Signature

head(self : SAME, cnt : CARD) res : SAME

##### Pre-condition

This feature is required to operate successfully whatever the value of cnt may be. The pre-condition is therefore vacuously true.

##### Post-condition

post let rest : SAME be st res ^ rest = self in
((cnt <= size(self))
and (size(res) = cnt))
or (size(rest) = 0)

This feature provides either cnt elements from the head of self or the contents of self if that contains fewer elements.

#### tail

This feature corresponds to the head feature above - this time returning the last portion of the string.

tail | (
| cnt : CARD
| ) : SAME

##### Formal Signature

tail(self : SAME, cnt : CARD) res : SAME

##### Pre-condition

This feature is required to operate successfully whatever the value of cnt may be. The pre-condition is therefore vacuously true.

##### Post-condition

post let rest : SAME be st rest ^ res = self in
((cnt <= size(self))
and (size(res) = cnt))
or (size(rest) = 0)

This feature provides either cnt elements from the tail of self or the contents of self if that contains fewer elements.

#### substring

This feature enables a program to extract an arbitrary number of string elements in string form.

substring | (
| beg : CARD,
| num : CARD
| ) : SAME

##### Formal Signature

substring(self : SAME, beg : CARD, num : CARD) res : SAME

##### Pre-condition

pre (beg + num) <= size(self)

##### Post-condition

post let begin : SAME, end : SAME be st begin ^ res ^ end = self in
(size(begin) = beg)
(and (size(res) = num)

This feature provides the substring of num elements of self beginning with the one whose index is one greater than beg.

NOTE | Indices used in Sather programs start with zero, those in vdm-sl are ordinal numbers starting from 1.

#### loc

This feature returns the index in the string at which the next element to be added will be placed. Note that this is expected to be equal to the string size.

loc | : CARD

##### Formal Signature

loc(self : SAME) res : CARD

##### Pre-condition

Note that in contrast to a pre-condition written in Sather source code, in vdm-sl the type SAME is not an optional type and therefore self must exist, leading to the vacuous pre-condition.

##### Post-condition

post res = card dom self

This feature provides the location where the next following string insertion will take place.

#### loc

This feature is the 'writer' routine corresponding to the above 'reader'. Note that this could be implemented as a publicly visible attribute of the defining class.

loc | (
| nuval : CARD
| )

##### Formal Signature

loc(self : SAME, nuval : CARD)

##### Pre-condition

pre nuval <= loc(self)

##### Post-condition

post loc(self) = nuval

This feature provides the facility for altering the location for next insertion, provided that it is either immediately after the current end of the string or an arbitrary location within the body of the current string.

| Language Index |  | Library Index |  | String Index

Comments or enquiries should be made to Keith Hopper. Page last modified: Thursday, 20 July 2000. |