Skip to content

Proposal for lists of strings #268

Open
@arjenmarkus

Description

@arjenmarkus

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 and end, where head means insert the new string before the first one and end means insert it after the last one. Arithmetic with end 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 = end

    Indices 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 if head or end 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 and end have similar meanings as with insert.

  • 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 and delete. The replace 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 to index 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions