Understanding ISO/IEC 1539-2:2002 – Fortran Part 2: Varying-Length Character Strings

A technical deep dive into the standard for dynamic string handling in Fortran

Scope of ISO/IEC 1539-2:2002

ISO/IEC 1539-2:2002, formally titled “Information technology — Programming languages — Fortran — Part 2: Varying-length character strings”, defines a mechanism for handling character strings whose length can change at runtime. It is an extension to the fixed-length character handling defined in the core Fortran standard (ISO/IEC 1539-1). The standard specifies a derived data type, VARYING_STRING, within a module named ISO_VARYING_STRING, along with a comprehensive set of operations and procedures for manipulating such strings. This part is essential for applications that require dynamic string processing, such as text parsing, data input, and report generation.

Technical Requirements for Varying-Length Character Strings

Data Type and Module

All varying-length string entities must be declared with the VARYING_STRING type, accessible by including the statement USE ISO_VARYING_STRING in the program unit. The standard mandates that the type implementation must store the current length and the character sequence. The module also provides overloaded operators for assignment, concatenation, relational comparisons, and a set of intrinsic-like procedures.

Operations

The standard specifies the following operations for VARYING_STRING:

  • Assignment: Strings can be assigned from fixed-length characters, from string literals, or from other varying-length strings.
  • Concatenation: The // operator is overloaded to work between varying and fixed strings.
  • Relational Operators: ==, /=, <, <=, >, >= are defined.
  • Substring Selection: Using parentheses (v(i:j)) to extract substrings.

Intrinsic Functions in the Module

The ISO_VARYING_STRING module supplies the following functions, among others:

FunctionDescription
LEN(v)Returns the length of the string (same as LEN for fixed but dynamic).
TRIM(v)Returns the string with trailing blanks removed.
REPEAT(v, n)Repeats the string n times.
INDEX(v, pattern)Returns position of substring pattern.
REPLACE(v, pat, rep)Replaces occurrences of pattern with replacement string.
SPLIT(v, delim)Returns an array of substrings separated by delimiter.

Note that these functions may have slight variations in interfaces; the standard ensures consistent semantics across implementations.

Example Usage

USE ISO_VARYING_STRING TYPE(VARYING_STRING) :: text text = 'Hello, World!' text = text // ' Welcome to Fortran.' WRITE(*,*) LEN(text), TRIM(text) 
Tip: For best performance, use varying-length strings only when needed; fixed-length strings are generally faster.

Implementation Highlights

Compilers implementing ISO/IEC 1539-2:2002 typically implement VARYING_STRING as a derived type with a character pointer or allocatable array. The module must manage memory for each string, often using deferred-length allocation or reference counting. Operators and procedures must be exposed through a .mod file when the module is compiled.

Key implementation challenges include ensuring thread safety, handling assignment with automatic (re)allocation, and conforming to the required interfaces.

Warning: Not all Fortran compilers provide the ISO_VARYING_STRING module. Verify compiler support before relying on Part 2 features.

Compliance Notes

For Compiler Vendors

A compiler claiming compliance with ISO/IEC 1539-2:2002 must provide the ISO_VARYING_STRING module exactly as specified. The module must include all required operator interfaces and procedures. Conformance tests exist to validate the implementation.

For Application Developers

To ensure portability, use only the documented operations from the standard. Avoid relying on specific internal representation details. It is recommended to include the standard conformance statement in user documentation.

Compliance Success: Adhering to the standard simplifies migration between different Fortran environments that support Part 2.
Danger: Mixing ISO_VARYING_STRING and deferred-length character variables (introduced in Fortran 2003) can lead to incompatibility if the interfaces differ.

FAQs

Q: What is the difference between fixed-length and varying-length character strings in Fortran?
A: Fixed-length strings have a constant length declared at compile time, while varying-length strings (defined in ISO/IEC 1539-2) allow dynamic length changes during execution through operations like assignment and concatenation.
Q: Is the ISO_VARYING_STRING module automatically available?
A: No, the program must explicitly USE the ISO_VARYING_STRING module to access the VARYING_STRING type and its associated operations.
Q: Can I use varying-length strings with standard Fortran intrinsic functions?
A: Some intrinsic functions accept varying-length arguments, but the module provides equivalent functionality specifically for the VARYING_STRING type.
Q: How does ISO/IEC 1539-2:2002 relate to later versions of Fortran?
A: Part 2 was largely integrated into the core Fortran 2003 standard, which introduced deferred-length character strings. However, for environments requiring strict conformance to the 2002 standard, Part 2 remains relevant.

📥 Standard Documents Download

🔒
Please wait 10 seconds, the download links will appear after the ad loads

Leave a Reply

Your email address will not be published. Required fields are marked *