# Section 8.16.1.2:
$SEARCH

###### abstract class $SEARCH{ELT < $IS_EQ} < $STRINGS

#### 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 ;
$SEARCH_ELT = set of object_type
state
multi : $SEARCH_ELT
inv multi_types ==
forall obj in set multi_types & sub_type($SEARCH_ELT,obj)

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

This abstract class models the concept of a searchable immutable string of any object kind which inherits from $IS_EQ. The features include forward and backward searching as well as matching and prefix operations.

#### is_prefix

It is frequently necessary to be able to recognise a key of some kind which indicates the start of some known sequence of data. This feature is provided to enable this to be done as needed.

is_prefix | (
| try : SAME
| ) : BOOL

##### Formal Signature

is_prefix(self : SAME, try : SAME) res : BOOL

##### Pre-condition

pre len try <= len self

##### Post-condition

post let test be st test ^ rest = self in
res = (test = try)

This predicate returns true if and only if try is identical to the elements starting at the beginning of self.

#### search

This feature enables the first occurrence of some individual element value to be found in the string.

search | (
| try : ELT
| ) : CARD

##### Formal Signature

search(self : SAME, try : ELT) res : CARD

##### Pre-condition

Since the domain of the result is larger than that of the string length, the pre-condition is vacuously true.

##### Post-condition

post (self(res + 1) = try
and not exists elem in set elems self(1, ..., res) &
elem = try)
or (res = CARD.maxval)

This feature returns the index in self at which the first occurrence of try is to be found. If there is no such occurrence then CARD::maxval shall be returned.

#### search

This feature enables the first occurrence of some individual element value to be found in the string, starting the search at the given index.

search | (
| try : ELT
| start : CARD
| ) : CARD

##### Formal Signature

search2(self : SAME, try : ELT, start : CARD) res : CARD

##### Pre-condition

pre

##### Post-condition

post (self(res + 1) = try
and not exists elem in set elems self((start + 1), ..., res) &
elem = try)
or (res = CARD.maxval)

This feature returns the index in self at which the first occurrence of try is to be found, starting to search at the given start index. If there is no such occurrence then CARD::maxval shall be returned.

#### search

This feature returns the index of the first element of the first occurrence of try in self.

search | (
| try : SAME
| ) : CARD

##### Formal Signature

search3(self : SAME, try : SAME) res : CARD

##### Pre-condition

Since the domain of the result is larger than that of the string length, the pre-condition is vacuously true.

##### Post-condition

post let fndidx = res + 1 in
(self(fndidx, ...,(fndidx + len try - 1)) = try
and forall idx in set inds self(1, ..., res) &
not substr(idx, ..., (idx + len try - 1)) = try)
or (res = CARD.maxval)

This feature returns the index in self at which the first occurrence of try is to be found. If there is no such occurrence then CARD::maxval shall be returned.

#### search

This feature enables finding the first occurrence of the start of the first occurrence of try to be found in the string, starting the search at the given index.

search | (
| try : SAME
| start : CARD
| ) : CARD

##### Formal Signature

search4(self : SAME, try : ELT, start : CARD) res : CARD

##### Pre-condition

pre

##### Post-condition

post let fndidx = res + 1 in
(self(fndidx, ...,(fndidx + len try - 1)) = try
and forall idx in set inds self((start + 1), ..., res) &
not substr(idx, ..., (idx + len try - 1)) = try)
or (res = CARD.maxval)

This feature returns the index in self at which the first occurrence of try is to be found, starting to search at the given start index. If there is no such occurrence then CARD::maxval shall be returned.

#### search_backwards

This feature enables the first occurrence of some individual element value to be found in the string starting to search backwards from the end of the string.

search_backwards | (
| try : ELT
| ) : CARD

##### Formal Signature

search_backwards(self : SAME, try : ELT) res : CARD

##### Pre-condition

Since the domain of the result is larger than that of the string length, the pre-condition is vacuously true.

##### Post-condition

post (self(res + 1) = try
and not exists elem in set elems self(res + 2), ..., len self) &
elem = try)
or (res = CARD.maxval)

This feature returns the index in self at which a backward search starting from the end first finds try. If there is no such occurrence then CARD::maxval shall be returned.

#### search_backwards

This feature enables the next occurrence of try is to be found starting a backward search from the given index. Note that if the element is found at the starting index that is returned.

search_backwards | (
| try : ELT
| start : CARD
| ) : CARD

##### Formal Signature

search_backwards2(self : SAME, try : ELT, start : CARD) res : CARD

##### Pre-condition

pre

##### Post-condition

post (self(res + 1) = try
and not exists elem in set elems self((res + 2), ..., (start + 1)) &
elem = try)
or (res = CARD.maxval)

This feature returns the index in self at which the next occurrence of elem is to be found, searching backwards from the given start index. If there is no such occurrence then CARD::maxval shall be returned.

#### mismatch

This feature returns the index of the first element of the two strings which is different.

search | (
| try : SAME
| ) : CARD

##### Formal Signature

mismatch(self : SAME, other : SAME) res : CARD

##### Pre-condition

pre

##### Post-condition

post (is_prefix(other,self)
and res = CARD.maxval)
or (res = len other)
or ((forall idx in set inds other(1, ..., res) &
self(idx) = other(idx))
and self(res + 1) <> other(res + 1))

Matching elements at the same index in self and other, this feature returns the index of the first element of self and other which have different element values; CARD::maxval is returned if self is a prefix of other.

| Language Index |  | Library Index |  | String Index

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