Description
Last tuesday, during the december call, it was mentioned that support for a list of strings might be useful. This is a straightforward proposal for such a feature in the standard library.
Fortran has supported variable-length strings since the 2003 standard, but it does not have a native type to handle collections of strings of different lengths. Such collections are quite useful though and the language allows us to define a derived type that can handle such collections.
This proposal considers the features that would be useful for this derived type, a list of strings. It does not prescribe the methods for implementing the type. Given the ease by which arrays are handled in Fortran, it is possible to use allocatable arrays, but perhaps a linked list may prove to be more efficient, at least in certain scenarios of use. Therefore the proposal concentrates on the usage.
A further limitation of the proposal: there is no provision for nested strings or for storing data with the string.
Methods for the list of strings:
-
Method
insert
- insert a new string after the given index.
Special index values:head
andend
, wherehead
means insert the new string before the first one andend
means insert it after the last one. Arithmetic withend
is possible:end-1
means insert before the last element and so on.Note: besides a string you can also insert another list or an ordinary array of strings (in the latter case all inserted strings will be of the same length as the array)
Convenience method:
append
- same as insert with index = endIndices beyond the bounds of the list:
Should they cause an error? Or should they be interpreted as insert at the start or at the end as ifhead
orend
were given? Or should the list simply grow to that length? This potentially causes holes. -
Method
delete
- delete a single string or a range of strings from the list.
head
andend
have similar meanings as withinsert
. -
Method
replace
- replace a string with a new value at the indicated location.Note: should we support replacement with multiple strings? My initial take at this is: no, if you want to do that, it can be done with a combination of
insert
anddelete
. Thereplace
method is a convenient way to deal with individual strings. -
Method:
get
- get one element from the list and return it as a string -
Method:
range
- get all strings in a given range and return them as a new list. -
Method:
index
- find the index of the first occurrence of a string in the list. Returns 0 (zero) if there is no such string. May also look for the last. -
Method:
index_sub
- similar toindex
but rather than the entire string, a matching substring is sought for. -
Method:
destroy
- destroy the list -
Assignment: you can use lists in the context:
list1 = list2
where list1 gets a copy of the entire list of strings.
Some methods will be subroutines, others will be functions. Special attention should be paid to error conditions. A guiding principle: no surprises. The philosophy might be: the list is - conceptually - infinitely long and if an element has not been set, then it is an empty string (a string of length 0).